A
anonymous
Dear All,
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 );
/* Write to the memory pointed to by p1 and p2 */
*p1 = 1;
*p2 = 2;
/* Confirm the write operation */
printf ( "*p1 = %d\n", *p1 );
printf ( "*p2 = %d\n", *p2 );
return 0;
}
access a memory location until that memory has been allocated either byFrom my understanding of pointers, a pointer should not be able to
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 );
/* Write to the memory pointed to by p1 and p2 */
*p1 = 1;
*p2 = 2;
/* Confirm the write operation */
printf ( "*p1 = %d\n", *p1 );
printf ( "*p2 = %d\n", *p2 );
return 0;
}