digitalmars.D.learn - Error while trying to allocate memory (malloc)
- "CrudOMatic" <crudomatic gmail.com> Aug 06 2012
- "Minas Mina" <minas_mina1990 hotmail.co.uk> Aug 06 2012
- "CrudOMatic" <crudomatic gmail.com> Aug 06 2012
- "Eyyub" <eyyub.pangearaion gmail.com> Aug 06 2012
- "CrudOMatic" <crudomatic gmail.com> Aug 06 2012
- "Eyyub" <eyyub.pangearaion gmail.com> Aug 06 2012
- Andrej Mitrovic <andrej.mitrovich gmail.com> Aug 06 2012
- "CrudOMatic" <crudomatic gmail.com> Aug 06 2012
- "CrudOMatic" <crudomatic gmail.com> Aug 06 2012
- Andrej Mitrovic <andrej.mitrovich gmail.com> Aug 06 2012
- Marco Leise <Marco.Leise gmx.de> Aug 07 2012
The following code:
import std.stdio, std.cstream, std.c.stdlib;
private {
//import rescache.cache;
//import mm.mempool;
}
void main(string[] args)
{
// press <Return> before program begins
din.getc();
memoryPool memPool = new memoryPool(1024);
writeln("Block Allocated!");
// press <Return> to end program
din.getc();
//return cast(int)0;
}
// Growable/shrinkable memory pool, for speedy memory allocation
class memoryPool {
private:
// Allocation header, count and keep track of allocs and keep
first, last info
struct s_allocHeader {
int m_allocCount; // count number of allocations from pool
s_allocInfo* p_first; // pointer to first allocation info
struct
s_allocInfo* p_last; // pointer to last alloc info struct
(should be null if only one)
}
// Keep track of allocations from the memory pool
struct s_allocInfo {
align(4) // align on 4-byte boundaries
void* p_data; // pointer to data chunk allocated out
void* p_inBlock; // pointer to block this allocation was made
in
int m_dataSize; // size of allocated chunk
s_allocInfo* p_next; // pointer to next allocation list entry
}
// Block header, count and keep track of system allocs and
keep first, last info
struct s_blockHeader {
int m_blockCount; // count number of allocations from system
s_blockInfo* p_first; // pointer to first block info struct
s_blockInfo* p_last; // pointer to last block info struct
(should be null if only one)
}
// Keep track of block allocations
struct s_blockInfo {
align(4) // align on 4-byte boundaries
void* p_block; // pointer to the memory allocated from the
system
int m_blockSize; // size of block allocated from the system
bool m_poolMember; // whether or not the block was allocated
for the memory pool
s_blockInfo* p_next; // pointer to next block list entry
}
int m_CHUNK_SIZE = 128; // n-byte allocations from the pool
int m_BLOCK_SIZE = 4096; // pool block size
public:
// Allocator
//void* alloc(int numChunks = 1, bool useMemPool = true) { }
// Deallocator
void free() { }
// Grow the memory pool
void growPool(int numBlocks = 1) { }
// Shrink the memory pool
void shrinkPool(int numBlocks = 1) { }
// Constructor; pass it the initial memory pool size in MB
this(int initialPoolSize) {
// set up allocation list
s_allocHeader* m_allocList;
// set up block list
s_blockHeader* m_blockList;
// create initial block info reference, and set block count
to 1
m_blockList.p_first = malloc(s_blockInfo.sizeof);
//m_blockList.m_blockCount = 1;
// allocate initial block of memory and store it in block
info reference
//s_blockInfo* tmp = m_blockList.p_first;
//tmp.p_block = malloc(cast(size_t)initialPoolSize);
//tmp.m_blockSize = initialPoolSize;
//tmp.m_poolMember = true;
//tmp.p_next = null;
}
// Deconstructor
~this() { }
} // END of c_memoryPool class
when compiled, gives the error:
Error: cannot implicitly convert expression (malloc(16u)) of type
void* to s_blockInfo*
on line (75):
m_blockList.p_first = malloc(s_blockInfo.sizeof);
What is going on here?
I even tried using new, but got an Access Violation
Any help would be appreciated.
Aug 06 2012
Maybe you need a cast before malloc to convert it to a "s_blockInfo*"?
Aug 06 2012
On Monday, 6 August 2012 at 13:22:08 UTC, Minas Mina wrote:Maybe you need a cast before malloc to convert it to a "s_blockInfo*"?
Just did, and compiled. Errored out with (from my IDE):(9:54:32 AM) AccessViolation-Exception (9:54:32 AM) c0000005 (9:54:32 AM) Exception in debugger client (9:54:32 AM) IDebugEventCallbacks::Exception (9:54:32 AM) callback. (9:54:32 AM) PC: (9:54:32 AM) 09239f05 (9:54:32 AM) VA: (9:54:32 AM) 00000000 (9:54:32 AM) R/W: (9:54:32 AM) 0 (9:54:32 AM) Parameter: (9:54:32 AM) 00000000 (9:54:32 AM) (9:54:32 AM) Program execution halted...
Running it directly gives me: object.Error: Access Violation ---------------- I:\Prog\Projects\D\resource-cache\resource-cache\main.d(13): D main ----------------
Aug 06 2012
On Monday, 6 August 2012 at 13:59:35 UTC, CrudOMatic wrote:On Monday, 6 August 2012 at 13:22:08 UTC, Minas Mina wrote:Maybe you need a cast before malloc to convert it to a "s_blockInfo*"?
Just did, and compiled. Errored out with (from my IDE):(9:54:32 AM) AccessViolation-Exception (9:54:32 AM) c0000005 (9:54:32 AM) Exception in debugger client (9:54:32 AM) IDebugEventCallbacks::Exception (9:54:32 AM) callback. (9:54:32 AM) PC: (9:54:32 AM) 09239f05 (9:54:32 AM) VA: (9:54:32 AM) 00000000 (9:54:32 AM) R/W: (9:54:32 AM) 0 (9:54:32 AM) Parameter: (9:54:32 AM) 00000000 (9:54:32 AM) (9:54:32 AM) Program execution halted...
Running it directly gives me: object.Error: Access Violation ---------------- I:\Prog\Projects\D\resource-cache\resource-cache\main.d(13): D main ----------------
You get an Access Violation error because `m_blockList` is null.
Aug 06 2012
On Monday, 6 August 2012 at 14:14:12 UTC, Eyyub wrote:On Monday, 6 August 2012 at 13:59:35 UTC, CrudOMatic wrote:On Monday, 6 August 2012 at 13:22:08 UTC, Minas Mina wrote:Maybe you need a cast before malloc to convert it to a "s_blockInfo*"?
Just did, and compiled. Errored out with (from my IDE):(9:54:32 AM) AccessViolation-Exception (9:54:32 AM) c0000005 (9:54:32 AM) Exception in debugger client (9:54:32 AM) IDebugEventCallbacks::Exception (9:54:32 AM) callback. (9:54:32 AM) PC: (9:54:32 AM) 09239f05 (9:54:32 AM) VA: (9:54:32 AM) 00000000 (9:54:32 AM) R/W: (9:54:32 AM) 0 (9:54:32 AM) Parameter: (9:54:32 AM) 00000000 (9:54:32 AM) (9:54:32 AM) Program execution halted...
Running it directly gives me: object.Error: Access Violation ---------------- I:\Prog\Projects\D\resource-cache\resource-cache\main.d(13): D main ----------------
You get an Access Violation error because `m_blockList` is null.
So, do I use new or what? In C it was just declare and go...
Aug 06 2012
On Monday, 6 August 2012 at 14:55:05 UTC, CrudOMatic wrote:In C it was just declare and go...
Really ? I don't think, because : s_blockHeader* m_blockList; Here, m_blockList is a pointer to s_blockHeader struct, and so the default value is `null`. So, trying to access to(?) a struct member whereas the pointer is set to null, is a nonsense. Tell me if I'm wrong. (I did not programming in C since 2 years)So, do I use new or what?
you can use `new` or `malloc`.(it depends on what you need) May be someone can help you better than me.
Aug 06 2012
On 8/6/12, Eyyub <eyyub.pangearaion gmail.com> wrote:Tell me if I'm wrong. (I did not programming in C since 2 years)
You're not wrong. m_blockList is a pointer and OP needs to allocate an s_blockHeader instance before he uses it: s_blockHeader* m_blockList = cast(s_blockHeader*)malloc(s_blockHeader.sizeof);
Aug 06 2012
On Monday, 6 August 2012 at 15:36:03 UTC, Andrej Mitrovic wrote:On 8/6/12, Eyyub <eyyub.pangearaion gmail.com> wrote:Tell me if I'm wrong. (I did not programming in C since 2 years)
You're not wrong. m_blockList is a pointer and OP needs to allocate an s_blockHeader instance before he uses it: s_blockHeader* m_blockList = cast(s_blockHeader*)malloc(s_blockHeader.sizeof);
oops. Thanks. brainfart I guess, been a while since I've used any C like languages.
Aug 06 2012
On Monday, 6 August 2012 at 15:36:03 UTC, Andrej Mitrovic wrote:On 8/6/12, Eyyub <eyyub.pangearaion gmail.com> wrote:Tell me if I'm wrong. (I did not programming in C since 2 years)
You're not wrong. m_blockList is a pointer and OP needs to allocate an s_blockHeader instance before he uses it: s_blockHeader* m_blockList = cast(s_blockHeader*)malloc(s_blockHeader.sizeof);
another quick question - are these allocations automatically entered into the GC heap? If so then I can just disable garbage collection?
Aug 06 2012
On 8/6/12, CrudOMatic <crudomatic gmail.com> wrote:another quick question - are these allocations automatically entered into the GC heap? If so then I can just disable garbage collection?
No, there is the standard C malloc/free in std.c.stdlib which you're using, and then there's the GC.malloc and GC.free in core.memory if you want to allocate from the GC heap.
Aug 06 2012
Am Mon, 6 Aug 2012 18:25:01 +0200 schrieb Andrej Mitrovic <andrej.mitrovich gmail.com>:On 8/6/12, CrudOMatic <crudomatic gmail.com> wrote:another quick question - are these allocations automatically entered into the GC heap? If so then I can just disable garbage collection?
No, there is the standard C malloc/free in std.c.stdlib which you're using, and then there's the GC.malloc and GC.free in core.memory if you want to allocate from the GC heap.
And in the latter case write: auto m_blockList = new s_blockHeader; -- Marco
Aug 07 2012









"Minas Mina" <minas_mina1990 hotmail.co.uk> 