Sign in to follow this  
EL DUDE

Memory initialisation

Recommended Posts

Hi all, I have a weird problem: I stopped programming for some time (due to my studies), and now I that I try to run again some old programs i made almost a year ago, they won't run... After debugging I found out that every pointer I intialise to NULL is set to 0xCDCDCDCD instead and apparently new vars are never intialised to 0 but to odd values... This is strange as it perfectly worked before, I changed nothing to the program nor to the compiler?! Any idea where this comes from? I remarked the problem only shows up on DLL based projects (one homemade program using a homemade dll) Thanks!

Share this post


Link to post
Share on other sites
If you follow the function calls for new and especially delete, there's part where the system initializes unused memory to some special value: 0xbaadfood or 0xcdcdcdcd, as you said. And this is what you get from new or malloc.

This feature comes with system headers, so it might be you were using another OS(actually, developement environment) another time.

The case is, IMO, you were not initializing your variables properly, depending on the default values instead.
Sad but true, you'll have much code to change now...

Share this post


Link to post
Share on other sites
0xCDCDCDCD in particular shows areas of memory that have been alocated from the heap (ie: new'd) but have yet to be initialized to a sensible value. In other words - "int* i = new int; print(*i);" will print that value. Going "MyObject* o = new MyObject;" will cause all the members of MyObject to be equal to that value unless the MyObject constructor sets them otherwise (which is probably what you should be doing).

Half way down this page is a list of some other common numbers.

Don't worry - I had this problem once too. Basically - anything you don't initialize explicitly somewhere can be any value the compiler likes. MSVC happens to like making these values very weird and error-inducing during debugging (and will not do anything - giving you any value at all - in release mode). Don't worry - it's being helpful by identifying potential bugs in your program for you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I thought of the data still being uninitialised, but when I explicitly set a pointer to NULL it sometimes still points to 0xcdcdcdcd... The problem of unitialised data also shows up *sometimes* with non dynamically created vars.

Any idea of how I could spare time rewriting each var intitalisation code? Some kind of macro or maybe a compiler setting I could change?



Thanks for your previous answers!

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I thought of the data still being uninitialised, but when I explicitly set a pointer to NULL it sometimes still points to 0xcdcdcdcd... The problem of unitialised data also shows up *sometimes* with non dynamically created vars.

Any idea of how I could spare time rewriting each var intitalisation code? Some kind of macro or maybe a compiler setting I could change?

Thanks for your previous answers!
Setting a pointer to NULL, sets it to NULL - period. If it changes back to 0xCDCDCDCD then your program is copying that over the pointer afterwards. You may have more than unitialised variables to worry about in that case, e.g. buffer overflows as well.

If you use a number of classes or structs then you can fix many instances of them at once, to be initialised however you want, by changing (or writing) a constructor.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this