Why does it work ?

Started by
6 comments, last by phusnikn 20 years, 10 months ago
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
http://www.phusnikn.net
Advertisement
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.
Ahh this clears up alot thanks for your response.
http://www.phusnikn.net
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.
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?
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.
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.
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]

This topic is closed to new replies.

Advertisement