Chetan Nanda wrote:
> Hi All,
> Acn anybody explain me the behavior of the following program.
> 
> #include <stdio.h>
> int main()
> {
>     char *dst = (char *) malloc(4);
>     strcpy(dst, "This is a nice palace");
> 
>      exit(0);
> }
> 
> This is a clear case of memory corruption. But it does not crash. I am using
> GCC as compiler.
> 
> ~nandac

Read the chapter entitled "Memory" in my book Safe C++ Design Principles 
(available to c-prog members for free - see the Books section of the 
website).  It describes how memory is allocated on many systems and most 
likely explains why your program didn't crash.

And you should not be writing C code.  Write at least C++ code or, 
preferably, Safe C++ (or at least using the concepts of Safe C++ in C++ 
code).

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* VerifyMyPC 2.2
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.

Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/

Reply via email to