|
From: <or...@vp...> - 2004-01-10 10:06:58
|
Siva Chandra wrote:
> I used the follwoing piece of code as a test:
>
> int *a;
> a = (int *)malloc(sizeof(int));
> *a = 5;
> free(a);
> printf("%d\n", *a);
>
> This prints 0 in the linux environment and 5 in the
> windows environment!!
>
> does anyone out there know whats happening??
> the same happens with new and delete!!
You should take the above printf() as "unpredictable". After you free or
delete some allocated memory, it is the operating system's task to do
whatever it want's with it. It seems, that Linux automatically fills
freed memory with zeros, while Windows does not.
Again, the above must be considered as unpredictable, so you shouldn't
care about freed memory (variables): they are no longer yours, they were
given back to the OS, so you theoretically "don't want" to do anything
with them any further.
--
Greetings,
Balázs
|