Archived

This topic is now archived and is closed to further replies.

atlnewbie

char memory allocation question

Recommended Posts

Hi all, Probably a stupid question - oh well here goes... This is memory before the allocation: 00990EA0 DD DD DD DD DD DD DD DD ........ 00990EA8 DD DD DD DD DD DD DD DD ........ 00990EB0 DD DD DD DD DD DD DD DD ........ 00990EB8 DD DD DD DD DD DD DD DD ........ 00990EC0 DD DD DD DD DD DD DD 00 ........ 00990EC8 00 03 00 00 31 01 00 00 ....1... This is the allocation statement: unsigned char *buffer = (unsigned char*)malloc(32); This is the memory after the allocation: 00990EA0 CD CD CD CD CD CD CD CD ÍÍÍÍÍÍÍÍ 00990EA8 CD CD CD CD CD CD CD CD ÍÍÍÍÍÍÍÍ 00990EB0 CD CD CD CD CD CD CD CD ÍÍÍÍÍÍÍÍ 00990EB8 CD CD CD CD CD CD CD CD ÍÍÍÍÍÍÍÍ 00990EC0 FD FD FD FD DD DD DD 00 ýýýý.... 00990EC8 51 00 00 00 31 01 00 00 Q...1... As far as I understand only the first 32 bytes should have been set to CD (undefined, right?) - so what''s going on in the memory?!? thnx, atlnewbie

Share this post


Link to post
Share on other sites
If you want the first 32 bytes to be CD I can''t see the problem here. The first 32 bytes are CD (count the CDs, each CD is one byte). Maybe you counted C as one byte and D as another?

-Benny-

Share this post


Link to post
Share on other sites
yes you're right, sorry for being unclear.
i was interested in why the other bytes after the first 32
bytes (bytes 33-37, 41-42) were changed as well.

thnx,
atlnewbie

Edited by - atlnewbie on November 13, 2001 8:56:36 AM

Share this post


Link to post
Share on other sites
if you are running this as a debug version in VC++ then it will probably be becuase VC++ allocates more memory than you request so that it can check if you go past the memory you allocate.

Share this post


Link to post
Share on other sites
Thanks for the help!

Can i use the debug mode for stepping through
the code and viewing the memory - but without
the compiler doing extra fancy stuff like that?
(i'm using msvc++6.0 enterprise)

atlnewbie

Edited by - atlnewbie on November 13, 2001 10:27:20 AM

Share this post


Link to post
Share on other sites
quote:
Original post by NewDeal
Why are the values changed from DD to CD when the memory is allocated ? I wouldnt expect them to be altered at all.

Just wondering



VC++ has a bunch of difference constants that it uses to fill memory depending on the type of memory (unallocated, uninitialized heap memory, uninitialized stack memory, etc.). Filling it helps you figure out programming bugs. For example, if a for loop in your code isnt working right, and you set a breakpoint in the debugger and look at your local variables, and you see your loop-termination-variable has the hex value CD, you can be *pretty sure* the reason your loop isnt working is because you forgot to initialize the variable.

It just makes things easier to figure out. Anytime you see a variable with one of these pre-initialized values, it should throw up a warning to you that you *MIGHT* have forgot to initialize something.

Edited by - LordKronos on November 13, 2001 1:37:03 PM

Share this post


Link to post
Share on other sites
The MSVC++ malloc library places a small amount of data at the end of each allocated block. When you free the block, the library checks to make sure that the data hasn''t been changed: if it has then this is a likely indication that you''ve accidently overrun the end of the block.

If this happens, you''ll receive a damage after Normal block message upon freeing the memory.

All your bases belong to us (I know. It''s irony.)

Share this post


Link to post
Share on other sites