I think it depends on how much memory has been used . See malloc() allocates a
definite amount of memory(4 in your case), but you are storing more characters
than allocated through malloc(). I don;t think it is a case of memory
corruption. malloc() would return a pointer to the memory it has allocated in
the 'heap' region. Now in your case it may be that the subsequent memory
locations are also free. So that it isn't creating problems. But it is not
recommendable, since you cannot know how much of memory is free. I too tried
this program....and not giving any errors. It depends upon whether the
subsequent location after your allocated bytes are free.
Still I am not certain what actually happened.
Regards,
Debasish
Sumit Chawla <[EMAIL PROTECTED]> wrote: On 07
Mar 2007 18:36:56 -0800, Chetan Nanda <[EMAIL PROTECTED]> 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 do not crash. I am using
> GCC as compiler.
>
> ~nandac
>
You are crossing the memory bounds and corrupting the memory block which
might have been allocated to other parts of your program or some other
program. It might crash the other programs or applications. So behavior is
undefined.
--
Regards
Sumit Chawla ("eNetra : Hail the Invention")
"Computers are useless. They can only give you answers.":Pablo Picasso
[Non-text portions of this message have been removed]
Karmennevaya Dhikaraste, Maaphaaleshu Kadaachanah
---------------------------------
Heres a new way to find what you're looking for - Yahoo! Answers
[Non-text portions of this message have been removed]