wrote:
> Hello
>
>
> This is luck that it works.
>
> The declared pointers have a value, C doesn't zero memory on
> declaration!!!
>
> What actual is happening is that you are reading/writeing on a random
> place in memory.
>
> the behaivior of this is undefined.
>
> All things can happen.
>
> Greetings Olaf
That is what I also think i.e. the pointers are pointing to *any*
memory as per garbage
values stored in p1 and p2 and, in turn, garbage value in the pointed
memory is being
accessed. But just try this program below. According to above
hypothesis, this should
work again, but it does not. You get a segmentation fault even on a
read operation of an
arbitrary memory location. Why cannot I access this memory now if I
could access the
memory pointed to by *garbage* address in p1. In fact, I tried with
various input values
of address and it would not read or write to any other memory location
that I could think of. May be try generating all possible addresses and
try to read and write all memory
locations right from 0000 0000 to ffff ffff and then figure out any
result.
#include <stdio.h>
int main ( void )
{
int *p1, address;
/* You can do read and write with the garbage address in p1 */
printf ( " *p1 = %d\n", *p1 );
*p1 = 0x12345678;
/* Now get some address from the user */
scanf ( "%x", &address );
/* Initialize your pointer with this address */
p1 = ( int * ) address;
/* Try reading from memory pointed to by the address */
/* You get a segmentation fault this point onwards */
printf ( " *p1 = %d\n", *p1 );
/* Try writing to memory pointed to be the address */
*p1 = 0x12345678;
/* Confirm the write operation */
printf ( " *p1 = %d\n", *p1 );
return 0;
}