Another question related to pointers.

A

anonymous

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

olaf_giezenaar

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
 
U

usenet

anonymous said:
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?

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.
 
A

anonymous

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

Eric Sosman

anonymous said:
[...]
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.
 
K

Keith Thompson

anonymous said:
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.
 
S

slebetman

anonymous said:
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.
 
S

slebetman

anonymous said:
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.
 
S

slebetman

anonymous said:
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.
 
J

Jack Klein

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.
 
S

suresh

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

Well, I don't have to implement malloc because it is provided by
standard C. (I assume the standard C libraries provided need not be
implemented in standard C)

-suresh
 
A

anonymous

Eric said:
anonymous said:
[...]
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.

I agree with all of the above that you have written. BUT, how do you
ASSUME that I have not posted the *actual code*. Though it would
be OT to describe all this, but the fact is I was just refreshing my
knowledge reading a book and tried the above program on a
thought to test pointers. Nothing more. There is no other *actual*
code. So, who should LEARN in this case, what to post and what
not to post?
Harrumph. Consider your wrist officially slapped.

Though what I have described above is fact of matter. You may
take this as an official reply :)

Thanks for your help.
 
K

Keith Thompson

anonymous said:
Eric said:
anonymous said:
[...]
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.

I agree with all of the above that you have written. BUT, how do you
ASSUME that I have not posted the *actual code*. Though it would
be OT to describe all this, but the fact is I was just refreshing my
knowledge reading a book and tried the above program on a
thought to test pointers. Nothing more. There is no other *actual*
code. So, who should LEARN in this case, what to post and what
not to post?

Your question was:

] Can somebody please explain, why pointers p1 and p2 can read from
] and read to memory that they have not been allocated?

And here's the "actual code" that you posted:

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

You declare two variables called ptr1 and ptr2. You then refer to two
variables called p1 and p2, which were not declared anywhere. Your
question makes it clear that you were able to compile the program, and
you were wondering about its run-time behavior. The code that you
posted could not possibly have compiled.

That's how we know you didn't post the actual code. Or am I missing
something?
 
R

Richard Heathfield

anonymous said:
BUT, how do you ASSUME that I have not posted the *actual code*.

Because you implied that you had run the code (otherwise how could you
observe its behaviour?), and yet the code contained errors that would have
prevented its compilation.

So, in short, you lied.
Though it would
be OT to describe all this, but the fact is I was just refreshing my
knowledge reading a book and tried the above program on a
thought to test pointers.

How can you try it without running it?
How can you run it without compiling it?
How can you compile it without getting rid of the errors in it?
 
S

slebetman

suresh said:
Well, I don't have to implement malloc because it is provided by
standard C. (I assume the standard C libraries provided need not be
implemented in standard C)

You don't HAVE to implement malloc. But it is nice to have the power to
implement your own malloc if needed. Again my example is the gnu
malloc, see http://www.faqs.org/docs/securing/gnumaloc.html for why you
may want this.
 
C

Chuck F.

suresh said:
(e-mail address removed) wrote:
.... snip ...

Well, I don't have to implement malloc because it is provided by
standard C. (I assume the standard C libraries provided need
not be implemented in standard C)

You need not bend the standard very much to write a malloc. In the
case of my nmalloc for DJGPP (ref below) I think all that is needed
is the assumption that pointer to/from int casts are meaningful,
that alignment can be measured against those casts, and that memory
can be secured from the OS with the (non-standard) sbrk call.

<http://cbfalconer.home.att.net/download/nmalloc.zip>

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
A

anonymous

Keith said:
anonymous said:
Eric said:
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.

I agree with all of the above that you have written. BUT, how do you
ASSUME that I have not posted the *actual code*. Though it would
be OT to describe all this, but the fact is I was just refreshing my
knowledge reading a book and tried the above program on a
thought to test pointers. Nothing more. There is no other *actual*
code. So, who should LEARN in this case, what to post and what
not to post?

Your question was:

] Can somebody please explain, why pointers p1 and p2 can read from
] and read to memory that they have not been allocated?

And here's the "actual code" that you posted:

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

You declare two variables called ptr1 and ptr2. You then refer to two
variables called p1 and p2, which were not declared anywhere. Your
question makes it clear that you were able to compile the program, and
you were wondering about its run-time behavior. The code that you
posted could not possibly have compiled.

That's how we know you didn't post the actual code. Or am I missing
something?

Oh. Now I see the point and understand what is meant by *actual code*.
Very right, the code I posted was not the same file ptr.c that I tested
on
my computer which I do not use for the Internet. I could have
probably taken the file to the other machine, but rather just typed it
instead
in my mail. Apart from the two erros, rest of the code is the same,
*otherwise*
( I think this would convince me and others as well ).

I understand posting the file gets me better help which you all
provide.

Many thanks,

A.
 
A

anonymous

Richard said:
anonymous said:


Because you implied that you had run the code (otherwise how could you
observe its behaviour?), and yet the code contained errors that would have
prevented its compilation.

You are absolutely alright and I have explained in the other post.
So, in short, you lied.

I regret the mistake, but don't you think that "lie" is a bit harsh
word as used here?
 
M

Markus Becker

anonymous said:
int main ( void )
{
int *ptr1, *ptr2;

/* Write to the memory pointed to by p1 and p2 */

*p1 = 1;
*p2 = 2;

Hey, don't you ever do that again! One of these pointers actually
pointed to my bank account and corrupted it.

;-)

Markus
 
K

Kenny McCormack

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.

According to the "experts" in this ng, you *can't* write an OS in C.

(This is taking the religious dogma as expressed by some that anything that
isn't "standard C" isn't "C". Yes, as hard as it is to believe, I've seen
exactly that thought expressed here in just those words.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top