Archived

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

Why does it work ?

This topic is 5304 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

Hi all i''m just curious why the following works and doesnt just blowup ? #include <stdio.h> #include <stdlib.h> int main(void) { char *p; p = malloc(10); strncpy(p,"Hello World\n",12); printf("%s\n",p); return 0; } shouldnt this give some kind of error ? sinse I''m only asking for 10 bytes but yet im writing 12 bytes into this memory location ? I''f someone can clear this up I will really apprecaite it. http://www.phusnikn.net

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It most likely works because most data on a 32bit OS like windows is word aligned meaning that when you allocated 10 bytes it physically allocated 32bytes of data so that the data would be aligned and processed faster than non aligned data. So you''ve got a little bit of a buffer there, however you can not rely on this being the case all of the time. You''ve created a situation where you are recieving unexpected results and these types of things typically lead to a crash.

Share this post


Link to post
Share on other sites
If you''re compiling and running in MSVC debug mode it usually pads your memory allocations with some extra bytes to prevent nasty crashes, for example if you make an off-by-one mistake and overwrite the end of an array.

Share this post


Link to post
Share on other sites
quote:
Original post by Dobbs
If you''re compiling and running in MSVC debug mode it usually pads your memory allocations with some extra bytes to prevent nasty crashes, for example if you make an off-by-one mistake and overwrite the end of an array.

So... MSVC hides subtle bugs from you until you decide that you''re done with your mission-critical project that''s due tomorrow and compile in release mode?

Share this post


Link to post
Share on other sites
quote:
Original post by micepick
quote:
Original post by Dobbs
If you''re compiling and running in MSVC debug mode it usually pads your memory allocations with some extra bytes to prevent nasty crashes, for example if you make an off-by-one mistake and overwrite the end of an array.

So... MSVC hides subtle bugs from you until you decide that you''re done with your mission-critical project that''s due tomorrow and compile in release mode?


No. VC debug builds should scream out loud if you do something like that. That is because it initializes the added bytes with a certain value (forgot which one exactly) and if you free a block and that value is modified, it knows you messed up.

Share this post


Link to post
Share on other sites
For a more generic answer, I believe this is undefined behavior. That is, the standard doesn''t say one way or the other what will happen. Therefore, it can work "as expected" (which varies depending on what you expect) or it can do something else.

Share this post


Link to post
Share on other sites
quote:
Original post by BitMaster
No. VC debug builds should scream out loud if you do something like that. That is because it initializes the added bytes with a certain value (forgot which one exactly) and if you free a block and that value is modified, it knows you messed up.

Ah, true. I remember it complaining about stack corruption at one point, so I guess it must do the same for malloced memory.



[edited by - micepick on June 4, 2003 2:30:56 PM]

Share this post


Link to post
Share on other sites