Jump to content
  • Advertisement
Sign in to follow this  
AndiDog

new operator allocates too little memory

This topic is 4312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using gcc 3.3.1 (mingw special 20030804-1) and got a weird problem creating structures:
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);
}

Now what am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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*?

Share this post


Link to post
Share on other sites
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:

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!