Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Another question related to pointers.

 
 
suresh
Guest
Posts: n/a
 
      01-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
>
> 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

 
Reply With Quote
 
 
 
 
anonymous
Guest
Posts: n/a
 
      01-06-2006

Eric Sosman wrote:
> 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?

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

>
> --
> Eric Sosman
> (E-Mail Removed)lid


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-06-2006
"anonymous" <(E-Mail Removed)> writes:
> Eric Sosman wrote:
>> 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?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
 
Richard Heathfield
Guest
Posts: n/a
 
      01-06-2006
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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      01-06-2006
suresh wrote:
> (E-Mail Removed) 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.

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


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.

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-06-2006
suresh wrote:
> (E-Mail Removed) wrote:
>

.... snip ...
>>
>> 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)


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:
news:news.announce.newusers
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

 
Reply With Quote
 
anonymous
Guest
Posts: n/a
 
      01-06-2006

Keith Thompson wrote:
> "anonymous" <(E-Mail Removed)> writes:
> > Eric Sosman wrote:
> >> 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.

>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
 
anonymous
Guest
Posts: n/a
 
      01-06-2006

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.

> So, in short, you lied.


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

>
> > 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?
>
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999
> http://www.cpax.org.uk
> email: rjh at above domain (but drop the www, obviously)


 
Reply With Quote
 
Markus Becker
Guest
Posts: n/a
 
      01-06-2006
anonymous <(E-Mail Removed)> schrieb:

> 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
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-06-2006
In article <(E-Mail Removed) .com>,
(E-Mail Removed) <(E-Mail Removed)> 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.

(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.)

 
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