--- In [email protected], 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.

You *might* get an access violation (or some other) error - the
standard doesn't say that you definitely will, just that the behaviour
is undefined. Just because your code worked once, or perhaps 10 or
100000 times, it doesn't mean it will work next time.

Even if you disassembled your compiler and proved that it generates
code that will always work (which is unlikely), the next version of
the compiler might generate code that always fails.

BTW just as a matter of interest, using gcc on my linux box, the
following code which is similar to yours appears to run ok, but gives
the 'wrong' answer (but it is doing what the standard says it should):

#include <malloc.h>
#include <stdio.h>

int main(void)
{
    int *i = malloc(sizeof *i);
    *i = 30;
    free(i);
    printf("*i=%d\n", *i);
    return 0;
}

Output:

*i=0

John

Reply via email to