Consider the following code:
int *p;
p = malloc(1);//p can point to 1 byte memory area
Why can p
point to many memory areas like below?
printf("%p %p %p %p %p",p,p+1,p+2,p+3,p+4);
Consider the following code:
int *p;
p = malloc(1);//p can point to 1 byte memory area
Why can p
point to many memory areas like below?
printf("%p %p %p %p %p",p,p+1,p+2,p+3,p+4);
The cases p+1
, p+2
and so on, cause undefined behaviour. As described by that page, your program doesn't comply with the rules of the C language so any behaviour you might get is meaningless.
C doesn't do any memory address checking. It's one of the things that makes c incredibly efficient, but also more difficult to program. A pointer is just a variable like any other, storing binary data, and distinguished only by the fact that the data is expected to be a memory address. Pointer arithmetic is perfectly valid in the case of, for example, c arrays.
You could also set a pointer to a random or arbitrary value. But unless you engineer it as such, dereferencing any address not from the compiler or malloc will result in either access of some of your own programs memory space, or a segmentation fault.