H
Herbert Rosenau
Dear All,
access a memory location until that memory has been allocated either by
assiging the address of a variable
or either through malloc i.e.
ptr = &somevariable;
or
ptr = malloc ( sizeof ( data ) );
However, what I discovered with the following program has made me a bit
uneasy.
Not only can I read the memory pointed to by pointers that still have
not been allocated any
memory, I can write as well.
Can somebody please explain, why pointers p1 and p2 can read from and
read to memory
that they have not been allocated?
Thanks in advance for all your help.
#include <stdio.h>
int main ( void )
{
int *ptr1, *ptr2;
/* Get read access to the memory pointed to by p1 and p2 */
printf ( "*p1 = %d\n", *p1 );
printf ( "*p2 = %d\n", *p2 );
undefined behavior
/* Write to the memory pointed to by p1 and p2 */
*p1 = 1;
*p2 = 2;
again undefined behavior
/* Confirm the write operation */
printf ( "*p1 = %d\n", *p1 );
printf ( "*p2 = %d\n", *p2 );
undefined behavior
return 0;
}
undefined behavior behaves undefined in any possible case. That
includes that it may or may not work as expected either sometimes or
ever or never maybe depending on sunshine or wether, it can produce
maximum damage after showing maximum harmless during debug.....
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!