Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Another question related to pointers.

Reply
Thread Tools

Another question related to pointers.

 
 
anonymous
Guest
Posts: n/a
 
      01-05-2006
Dear All,

>From my understanding of pointers, a pointer should not be able to

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 );

/* 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;
}

 
Reply With Quote
 
 
 
 
olaf_giezenaar@hotmail.com
Guest
Posts: n/a
 
      01-05-2006
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

 
Reply With Quote
 
 
 
 
usenet@zevv.nl
Guest
Posts: n/a
 
      01-05-2006
anonymous <> wrote:
> Dear All,
>
>>From my understanding of pointers, a pointer should not be able to

> 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?


You have not yet allocated any storage, but pointers always point
*somewhere*. Since you declare the pointers p1 and p2 as automatic
variables (on the stack), their value (the memory location they are
pointing to, not their *content*) is uninitialized, and just contain
the value that happened to be in that spot of memory.

What you are doing by accessing the pointers is just reading and - even
worse, writing - to *some* memory, but you don't know where. Blindfold
yourself, take a gun, turn around 20 times and shoot. You might hit
nothing at all, you might hit the ground, or you might hit the gastank
you are standing next to. Just don't do it.

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
anonymous
Guest
Posts: n/a
 
      01-05-2006

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;
}

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-05-2006
anonymous wrote:
> [...]
> However, what I discovered with the following program has made me a bit
> uneasy. [...]


I'd be uneasy, too, if the compiler actually accepted
the program without issuing a diagnostic as the Standard
requires.

When will people learn to post the actual code whose
behavior mystifies them, instead of typing in something
with a sketchy resemblance to that code? If a program's
behavior baffles you, you are the LEAST qualified to make
a paraphrase that preserves all the important points --
you don't know what's going on, so you don't know what
matters and what doesn't, what to leave in and what to
remove. Solution: Take out nothing, add nothing, and
post the actual code. Period.

In this particular case it's easy to work backwards
and see what you probably meant, but please Please PLEASE
don't make a habit of this nonsense! Next time you may not
be so lucky, and the combined Great Minds of c.l.c. will
happily debug the errors you introduced in the process of
making your paraphrase, leaving undiagnosed the actual error
that's bothering you but that vanished in translation.

Harrumph. Consider your wrist officially slapped.

--
Eric Sosman
lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2006
"anonymous" <> writes:
> From my understanding of pointers, a pointer should not be able to
> 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.


You *can*, but you may not.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      01-05-2006
anonymous wrote:
> Dear All,
>
> From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
> access a memory location until that memory has been allocated either by
> assiging the address of a variable or either through malloc i.e.
> (note: emphasis mine)


Actually this is not true. A pointer SHOULD NOT BE USED to access
memory location until that memory has been allocated. C, being a system
programming language SHOULD BE ABLE to do this though. C was intended
to expose as much of the hardware as possible so that one can write an
OS in it.

If C can't do this how are you supposed to write a malloc function in C
(for example the gnu malloc)? Another example use of an unallocated
pointer is in embedded systems where you often don't have an OS
running. In such cases it is common to access memory mapped hardware by
simply pointing to it. Say for example you have an I/O card at memory
location 0xffff10. You can simply access it by declaring int * io_card
= 0xffff10. Of course, on a memory protected OS this doesn't work and
segfaults. But C makes no assumption about the OS which is good since
sometimes one needs to use it where an OS is not running.

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      01-05-2006
anonymous wrote:
> Dear All,
>
> From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
> access a memory location until that memory has been allocated either by
> assiging the address of a variable or either through malloc i.e.
> (note: emphasis mine)


Actually this is not true. A pointer SHOULD NOT BE USED to access
memory location until that memory has been allocated. C, being a system
programming language SHOULD BE ABLE to do this though. C was intended
to expose as much of the hardware as possible so that one can write an
OS in it.

If C can't do this how are you supposed to write a malloc function in C
(for example the gnu malloc)? Another example use of an unallocated
pointer is in embedded systems where you often don't have an OS
running. In such cases it is common to access memory mapped hardware by
simply pointing to it. Say for example you have an I/O card at memory
location 0xffff10. You can simply access it by declaring int * io_card
= 0xffff10. Of course, on a memory protected OS this doesn't work and
segfaults. But C makes no assumption about the OS which is good since
sometimes one needs to use it where an OS is not running.

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      01-05-2006
anonymous wrote:
> Dear All,
>
> From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
> access a memory location until that memory has been allocated either by
> assiging the address of a variable or either through malloc i.e.
> (note: emphasis mine)


Actually this is not true. A pointer SHOULD NOT BE USED to access
memory location until that memory has been allocated. C, being a system
programming language SHOULD BE ABLE to do this though. C was intended
to expose as much of the hardware as possible so that one can write an
OS in it.

If C can't do this how are you supposed to write a malloc function in C
(for example the gnu malloc)? Another example use of an unallocated
pointer is in embedded systems where you often don't have an OS
running. In such cases it is common to access memory mapped hardware by
simply pointing to it. Say for example you have an I/O card at memory
location 0xffff10. You can simply access it by declaring int * io_card
= 0xffff10. Of course, on a memory protected OS this doesn't work and
segfaults. But C makes no assumption about the OS which is good since
sometimes one needs to use it where an OS is not running.

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-06-2006
On 5 Jan 2006 03:14:07 -0800, "anonymous" <> wrote
in comp.lang.c:

>
> 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;
> }


"Undefined behavior" has a specific meaning in C, as defined in the C
standard. Once a program generates undefined behavior, the C standard
no longer places any requirements on it. No requirement to do what
you want, no requirement to do something you did not want, no
requirement to crash the program. No requirement to do the same thing
twice. No requirements at all.

Once you know that your program causes undefined behavior, as you have
been told, than the reasons why any particular result happens or does
not happen is not a language issue. There is no C answer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another OS related thread Shane NZ Computing 1 09-17-2007 07:51 PM
How should multiple (related) projects be arranged (structured) and configured so that they can share code, have a related package structure and enable proper unittesting, and ensuring no namespace collisions ToddLMorgan@gmail.com Python 14 04-21-2006 04:03 PM
How should threads be terminated? (related to 'Help with thread related tracebacks') Maxwell Hammer Python 7 06-18-2005 04:20 PM
Another IPSec VPN related question Richard Graves Cisco 3 05-13-2005 01:28 AM
Yet another pointer related problem sugaray C Programming 7 03-04-2004 10:51 AM



Advertisments