struct Entry
{
void *ptr;
struct Entry *prev, *next;
};
void somefunction()
{
// This code allocates less memory than needed - not always but only in some cases (resulting in a read access exception)
Entry *e = new Entry;
// However, the same happens here:
Entry *e = (Entry*)malloc(sizeof(Entry));
// So I thought it must be an alignment problem because the following code works:
Entry *e = (Entry*)malloc(sizeof(Entry)+32);
}
new operator allocates too little memory
I'm using gcc 3.3.1 (mingw special 20030804-1) and got a weird problem creating structures:
Now what am I doing wrong?
try in main:
int s = sizeof(Entry);
on 32 bit platform it should give 12 bytes and it already aligned. access exception is probably there becuse you forgot to do:
e->next = new Entry;
int s = sizeof(Entry);
on 32 bit platform it should give 12 bytes and it already aligned. access exception is probably there becuse you forgot to do:
e->next = new Entry;
Yes it's actually 12 bytes. And I do not have any mistakes in the code as you suggested... It's some kinde of alignment problem.
Quote:Original post by AndiDog
And I do not have any mistakes in the code as you suggested...
Famous last words...
How about posting the actual code instead of just a few lines from here and there?
I assure you new works correctly.
The problem lies elsewhere in your code, likely you are trying to use the pointers in Entry without properly initializing them or you're doing something nasty and illegal with that void pointer.
This is C++, so why not use templates instead of void*?
The problem lies elsewhere in your code, likely you are trying to use the pointers in Entry without properly initializing them or you're doing something nasty and illegal with that void pointer.
This is C++, so why not use templates instead of void*?
In addition your first mistake is not to add a constructor to your "Entry" structure. Many problems arise from unitialised variables, so you'd best introduce a few ctors:
Cheers,
Pat
template<class Payload>struct Entry{ typedef Payload value_type; typedef Payload* value_pointer; Entry() : prev(0), next(0), ptr(0) { } Entry(Payload * ptr, Entry * prev = 0, Entry * next = 0) : ptr(ptr), prev(prev), next(next) { } Payload *ptr; Entry *prev; Entry *next; };
Cheers,
Pat
I don't use a constructor because I zero the struct before usage.
My code is far too complex to post it here.
The program does not crash if I allocate more memory than "needed" - and that's what I don't understand. Anyway I'm sure it's my fault (as always).
My code is far too complex to post it here.
The program does not crash if I allocate more memory than "needed" - and that's what I don't understand. Anyway I'm sure it's my fault (as always).
1) If you can use 'new', you are in C++. Use std::list. Better yet, choose appropriately from std::list, std::vector, and std::deque.
2) What's your evidence for "less memory than needed" being allocated? How are you measuring the allocation? Or does it just crash?
3) If you can use 'new', you are in C++. Don't use void*'s; template stuff.
4) You are aware, of course, that a void* is still a pointer? I.e. it's not a magical provider-of-storage-for-things-unknown; it still has to be pointed at something. What are you pointing it at? Did you make an allocation for the pointed-at thing as well? Do you need to?
5) You aren't, by any chance, (at least temporarily) ignorant of all of the above, and writing to the .ptr member as if it were a value rather than a pointer? (Don't laugh; I've seen it happen.) Is it possible the sizeof() the type you're using is something greater than sizeof(void*) but less than or equal to sizeof(void*) + 32? :/
6) Even if you know what you're doing, I can still assure you with great confidence that 'new' works properly. But anyway, those of us who could create and use this 'Entry' properly the first time, and have everything just work, still use std::list. You should too.
2) What's your evidence for "less memory than needed" being allocated? How are you measuring the allocation? Or does it just crash?
3) If you can use 'new', you are in C++. Don't use void*'s; template stuff.
4) You are aware, of course, that a void* is still a pointer? I.e. it's not a magical provider-of-storage-for-things-unknown; it still has to be pointed at something. What are you pointing it at? Did you make an allocation for the pointed-at thing as well? Do you need to?
5) You aren't, by any chance, (at least temporarily) ignorant of all of the above, and writing to the .ptr member as if it were a value rather than a pointer? (Don't laugh; I've seen it happen.) Is it possible the sizeof() the type you're using is something greater than sizeof(void*) but less than or equal to sizeof(void*) + 32? :/
6) Even if you know what you're doing, I can still assure you with great confidence that 'new' works properly. But anyway, those of us who could create and use this 'Entry' properly the first time, and have everything just work, still use std::list. You should too.
Sounds quite logical :) but why should I use C++ std::list if I am too stupid to code my own *simple* list template class?
I just got another idea: If there's a problem with a read access exception, why don't I look for the code accessing the memory??? Gonna do that right now...
Thanks
I just got another idea: If there's a problem with a read access exception, why don't I look for the code accessing the memory??? Gonna do that right now...
Thanks
Quote:Original post by AndiDog
Sounds quite logical :) but why should I use C++ std::list if I am too stupid to code my own *simple* list template class?
Because coding your own list is not simple. [wink]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement