Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Another question related to pointers.

 
 
Herbert Rosenau
Guest
Posts: n/a
 
      01-06-2006
On Thu, 5 Jan 2006 10:32:31 UTC, "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?
>
> 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!
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-06-2006
"anonymous" <> writes:
> Richard Heathfield wrote:
>> 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.

>
> You are absolutely alright and I have explained in the other post.


I don't see that other post you referred to, either on my server or on
groups.google.com. (That doesn't imply that you didn't post it, only
that it didn't get through.)

It's not a big deal; if you say it was an honest mistake, I'll take
your word for it.

--
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
 
 
 
 
Herbert Rosenau
Guest
Posts: n/a
 
      01-06-2006
On Fri, 6 Jan 2006 16:08:32 UTC, (Kenny
McCormack) wrote:


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


You're absolutely wrong. I have written an complete OS using 100% ANSI
C whereas 2% of all were 100% assembly for performance and direct
hardware access. But at least not a single C statement was not ANSI
compilant.

Our quality management had required to use ANSI compilance AND coding
C wherever halfways possible. The 2% NON C was absolutely needed to
get the real work done reliable to make direct hardware access C is
unable to do in any case.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-06-2006
Herbert Rosenau wrote:
> (Kenny McCormack) wrote:
>
>> 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".

>
> You're absolutely wrong. I have written an complete OS using
> 100% ANSI C whereas 2% of all were 100% assembly for performance
> and direct hardware access. But at least not a single C
> statement was not ANSI compilant.

.... snip ...
_____________________
/| /| | F
||__|| | Please do not |
/ O O\__ | feed the |
/ \ | Trolls |
/ \ \|_____________________|
/ _ \ \ ||
/ |\____\ \ ||
/ | | | |\____/ ||
/ \|_|_|/ | _||
/ / \ |____| ||
/ | | | --|
| | | |____ --|
* _ | |_|_|_| | \-/
-- _--\ _ \ | ||
/ _ \\ | / `
/ \_ /- | | |
* ___ c_c_c_C/ \C_c_c_c____________

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-06-2006
In article <wmzsGguTDN6N-pn2->,
Herbert Rosenau <> wrote:
....
>> 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".

>
>You're absolutely wrong. I have written an complete OS using 100% ANSI
>C whereas 2% of all were 100% assembly for performance and direct
>hardware access. But at least not a single C statement was not ANSI
>compilant.


Thank you for making my case for me. I assume that you meant 98% ANSI C,
as in, "my OS was written in a combination of C and assembler, in an
approximate ratio of 49 to 1".

Then we can consider the matter closed.

 
Reply With Quote
 
Vadivel
Guest
Posts: n/a
 
      01-07-2006
According to my understanding of the program. It will give compilation
error for the pointer variable's p1 and p2, which is not declared in
the given program code.

If you considered that p1 as ptr1 and p2 as ptr2, the happening in the
Enviornment is as follows,

When we declaring a variable (either it is pointer or not pointer ) the
memory will be alloted for that variable in the memory register. While
allocating the memory the Enviornment will store some maximum or
minimum value in that corresponding alocation as a Garbage Value in
accordance to the Data Type.

So, when we refer to the variable it will interrupt the Memory Register
and fetch the value stored in it, because of this process you can able
to execute the program after correcting the variables as ptr1 and ptr2
instead of p1 and p2 respectively.

And also you should remain that the Function Printf will carry only the
memory address of the variable which is passed to it. It won't carry
the values to execute the fuction.

If any one having Controversy in my explain please mail me your
explaination to

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-07-2006
anonymous a écrit :
>>From my understanding of pointers, a pointer should not be able to

> access a memory location until that memory has been allocated


This is not the point.

> either by
> assiging the address of a variable
> or either through malloc i.e.


This is the point. To be dereferenced, a pointer must hold the address
of a valid memory zone, unless, the behaviour is undefined. Period.

> Not only can I read the memory pointed to by pointers that still have
> not been allocated any


Of course, it is technically possible to invoke an Undefined Behaviour
(or shot a bullet in your foot), but it's still a bug. Don't do that.

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


A pointer is nothing but a variable. If its content is undefined, or the
address of an undefined memory zone, nasty things are going to happen.

> int *ptr1, *ptr2;
>
> /* Get read access to the memory pointed to by p1 and p2 */
> printf ( "*p1 = %d\n", *p1 );


what is p1 ? You defined ptr1 ... Please compile before posting. And
don't retype, but copy & paste.

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-07-2006
anonymous a écrit :
> 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.


Don't try to find any logic in an undefined behaviour. Your code is
wrong by construct, fix it and don't waste your time.

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      01-08-2006
Kenny McCormack wrote:
> In article < .com>,
> <> wrote:
> >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.

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


I know, I know. Don't feed the troll. But let me first say that writing
an OS is what C was originally designed to do.

Was the OS in question (Unix) written entirely in C? Probably not. But
was the 'core' of the OS (the kernel) written entirely in C? Yes. And
before anyone points out that it was not 'standard' C let me point out
that that 'C' was what Dennis Ritchie recognised as C (so at least it
was K&R C). Let me quote Dennis Ritchie on this:

In 1993 Dennis M. Ritchie wrote:
By early 1973, the essentials of modern C were complete. The language
and
compiler were strong enough to permit us to rewrite the Unix kernel
for the
PDP-11 in C during the summer of that year.
( taken from http://cm.bell-labs.com/cm/cs/who/dmr/chist.html )

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      01-08-2006
Kenny McCormack wrote:
> In article < .com>,
> <> wrote:
> >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.

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


I know, I know. Don't feed the troll. But let me first say that writing
an OS is what C was originally designed to do.

Was the OS in question (Unix) written entirely in C? Probably not. But
was the 'core' of the OS (the kernel) written entirely in C? Yes. And
before anyone points out that it was not 'standard' C let me point out
that that 'C' was what Dennis Ritchie recognised as C (so at least it
was K&R C). Let me quote Dennis Ritchie on this:

In 1993 Dennis M. Ritchie wrote:
By early 1973, the essentials of modern C were complete. The language
and
compiler were strong enough to permit us to rewrite the Unix kernel
for the
PDP-11 in C during the summer of that year.
( taken from http://cm.bell-labs.com/cm/cs/who/dmr/chist.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