Your program works fine because even if you have freed the memory,
it has not been reused/re-allocated yet, so the pointer pointing to this
portion
still holds the same data and you can work on it.

However this might fail when you free the memory and try some new
allocations,
then now try to access the memory (probably you wont get the same data,
as it might have been reused).
    Now the pointer starts misbehaving. So it is a common practice to
NULLify the
pointer which you have freed, so that the program doesn't make any more
access
to the freed portion.


Santosh Vernekar
Engineer

  - "They say that if you play a microsoft disc backwards you hear satanic
messages,
    thats nothing: if you play it forwards it installs windows"


On Tue, Nov 11, 2008 at 12:19 AM, Gopi Krishna Komanduri <
[EMAIL PROTECTED]> wrote:

>   Hi All,
>   I have a very basic query. I know that after deallocating memory for a
> pointer , when we try to access it , we will get access violation error.
> From top of my memory , even I tried it and worked on it. But now I dont
> know why but my code is working in wrong manner now. Can any one of you
> please suggest it please. I am using VisualStudio 2005 compiler. on windows
> xp.
>
> #include<stdio.h>
> #include<malloc.h>
>
> void main()
> {
>     int *i;
>     i=(int *)malloc(1*sizeof(int));
>     *i=30;
>     free(i); // freed the mem
>     *i=50; //  Tried to acces it again .. Ideally , it should fail . But
> it is not failing .. can any //one explain the reason.
> }
>
> Thx,
> --Gopi
>
> Connect with friends all over the world. Get Yahoo! India Messenger at
> http://in.messenger.yahoo.com/?wm=n/
>
> [Non-text portions of this message have been removed]
>
>  
>


[Non-text portions of this message have been removed]

Reply via email to