"Mastering C Pointers"....

K

Keith Thompson

Alan Connor said:
After reading the first 1/3 carefully, it seems to me that a pointer is very
much like a symlink.

That's not a bad analogy. Like any analogy, it can be carried too far
if you're not careful, but it's a good start.
 
R

Randy Howard

Actually, I have a nasty suspicion that Roose is talking to himself,
although I could be quite wrong. Its just that . I noticed with
interest an exchange not dissimilar to this just now:

Roose said: something incorrect
Richard replied: no, thats wrong
Alan said: oh, thats what I meant.

Note the use of the personal pronoun in the last response.

I didn't notice that, but I will say that my "sockpuppet radar"
went off full blast reading throught this thread.
 
R

Roose

After reading the first 1/3 carefully, it seems to me that a pointer is
very
much like a symlink.

Uh, I'm not exactly sure what a symlink is, but if it is anything like a
Windows shortcut, there is definitely an analogy there.

The windows shortcut just stores the location of a file. A "pointer" to it.
An address. The address is the _path name_. In C, the "address" is
basically an integer, indicating where in a huge array of memory the
variable is.

You can have as many shortcuts as you want that point to the file. A
shortcut takes a small amount of storage, but not as much as a (typical)
file. You can have whole directories of shortcuts that point to files all
over the place, in order to organize them. Likewise, you can have
collections of pointers, that point to various different things _scattered_
all over memory.

For example, on my hard disk, I have a whole bunch of (legal) MP3s organized
by artist and by album. Now, some of them were ripped poorly and have
clicks. So I *could* just copy those files to another folder, in order to
note that I need to rip them again.

However, it is *much* easier and efficient to just copy shortcuts those bad
files into another folder -- called "bad". That way I don't duplicate any
data. And I have all the bad files organized in one directory, and can
access them easily.

You could do the exact same thing with pointers. Suppose I had a whole
bunch of strings in memory, that listed a ton of song names. I could create
an array of pointers called "theBeatles" that pointed to every single song
by the Beatles. Then I could have an array of pointers called "badFiles"
that pointed to all the bad files, _some of which_ may be by the Beatles.
Note that I haven't incurred the storage cost of repeating the strings,
*just* pointers to them. Likewise I could create another array called
"before1960" that points to all songs stored before 1960 -- you get the
idea.

Hm, I'm surprised this didn't come up earlier. This is an easy analogy.
 
K

Keith Thompson

Roose said:
How about, "At least some bits of the binary representation of a
pointer can be interpreted as an integer." I didn't say anything
about casting pointers to integers.

I don't believe that's an improvement. In some implementations, the
underlying representation of a pointer is the same as that of an
integer. But if you want to understand pointers in general, and use
them in portable code, it's best not to worry about any equivalence
between pointers and integers.

To take it to a perhaps ridiculous extreme, if you think of a pointer
as being a finger that literally points to an object (I'm using term
"object" the way the C standard uses it), with certain operations you
can perform on it (move the finger N objects forward or backward, grab
what the finger points to, etc.), you won't go far wrong.

If you think of a pointer as just an integer, it will be difficult to
understand why, for example, you can add a pointer and an integer, but
you can't add two pointers.

As I've said elsethread, there's nothing wrong with understanding the
underlying representation, especially if you understand the variations
across differing implementations. But the idea that parts of a
pointer can be interpreted as an integer is actually one of the least
important things to understand about pointers.

[...]
This is a straw man. Any program, of critical nature or not, is tested, and
these types of errors don't even take testers to figure out. The code
simply won't work when you try it for the first time. If you want to make
an error-free program for one device, it is probably safer to find out the
specifics of your hardware, and program to that. Rather than coding in 100%
"standard ANSI C" and hoping the compiler does the right thing. If not,
then it will at least save you a lot of time.

Testing can only demonstrate the presence of bugs; it can't prove
their absence. There are plenty of bugs that only appear under
unusual circumstances. Pointer bugs can easily cause sporadic
failures that might be missed by testing. Imagine a bug that only
shows up when an airplane does a 10 degree left bank between 28,000
and 30,000 feet during a daylight saving time transition in a location
where true and magnetic north differ by 2 degrees. (That scenario is
purely a product of my imagination).
 
R

Roose

This is very sophisticated duelling, and somewhat entertaing, but I'd much
rather you were each expounding upon the fundamentals of C in your individual
and unique styles.

Well, I can do both : )
But I am curious, Roose, as to why you continue feeding the troll? Trolls
don't care about reason or truth or the subject at hand. They pretend to be
in order to create and win a game of dominance in which they can't increment
the value of SELF but must decrement the value of OTHER.

Well, I was the one who stopped in the previous flamewar (see the thread
about how you tell if a struct is 0, if you haven't seen it), and that will
likely be the case here.

It's all in good fun. I enjoy a good argument. Richard is at least fun to
argue with, because he grasps basic ideas and arguments (unlike some already
noted, who fail to understand the basic line, and respond with lame insults,
and who I *don't* respond to). Unfortunately, when you corner him, he
resorts to children's games, or simply refuses to answer, and then I stop.
No more fun then.
 
R

Richard Heathfield

Mark said:
Actually, I have a nasty suspicion that Roose is talking to himself,
although I could be quite wrong. Its just that . I noticed with
interest an exchange not dissimilar to this just now:

Roose said: something incorrect
Richard replied: no, thats wrong
Alan said: oh, thats what I meant.

Note the use of the personal pronoun in the last response.

Well, I thought that too, at first, but I now think it is unlikely, as a
little traffic analysis will show.

Firstly, Alan Connor first posted to this newsgroup in early October of this
year, well before Roose, and he showed every sign of being a thoughtful and
positive subscriber. This is clearly not the case with Roose, he shows
every sign of being another Tisdale (albeit marginally less entertaining).

Secondly, by 17th October Mr Connor's sig block had been changed to read:
"Posts with sigs of > 4 lines, or not in plain text, are dumped by my
filters.", which suggests (or at least hints) that he takes netiquette
seriously, or at least wishes others to believe that he does. Having said
that, this does make his decision to champion Roose The Top-Poster rather a
strange one, but that's entirely up to him.

Finally, this may be as appropriate a place as any to attempt to pour oil on
troubled waters. During this thread I have been repeatedly characterised as
a troll by Roose and Mr Connor. I attribute this to their ignorance, which
can be cured by education. I don't bear either of them a grudge, and I'm
perfectly prepared to play nice.

As I am sure I have said before, comp.lang.c is a mirror, and what you get
is probably what you are. I have therefore been most heartened by the
contributions of most of the posters in this thread. Thanks to you all.
 
A

Alan Connor

Roose, if you want to be a moderator of a newsgroup, why do you not go
start one and see how many sheep wish to join your flock?

That's completely unfair. You could just as well paint Richard with the
same brush.

This is transparent character assassination. Do you think we are stupid?
 
R

Roose

Keith Thompson said:
I don't believe that's an improvement. In some implementations, the
underlying representation of a pointer is the same as that of an
integer. But if you want to understand pointers in general, and use
them in portable code, it's best not to worry about any equivalence
between pointers and integers.

OK, suppose you want to serialize a group of structs, which contain pointers
to each other (a graph). In game programming, for getting data into the
game, it's a common idiom to subtract the base address in the PC tools,
store it on disk, and rebind the pointers at runtime in the game engine by
adding back a new base address. You can't do that without understanding the
equivalence, since of course you only read ints from binary files at first.

Of course this is not portable, but it works on 3 game consoles, and I would
contend that there is way to the exact same thing on ANY platform (with the
bit masking/arithmetic being slightly different, etc.)
To take it to a perhaps ridiculous extreme, if you think of a pointer
as being a finger that literally points to an object (I'm using term
"object" the way the C standard uses it), with certain operations you
can perform on it (move the finger N objects forward or backward, grab
what the finger points to, etc.), you won't go far wrong.

If you think of a pointer as just an integer, it will be difficult to
understand why, for example, you can add a pointer and an integer, but
you can't add two pointers.

Well, if you understand _what_ a pointer-as-an-int means, I think you would
understand already why it doesn't really mean much to add two pointers.
Testing can only demonstrate the presence of bugs; it can't prove
their absence. There are plenty of bugs that only appear under
unusual circumstances. Pointer bugs can easily cause sporadic
failures that might be missed by testing. Imagine a bug that only
shows up when an airplane does a 10 degree left bank between 28,000
and 30,000 feet during a daylight saving time transition in a location
where true and magnetic north differ by 2 degrees. (That scenario is
purely a product of my imagination).

Can you explain how an error in not writing standard C might result in such
a bug?

Roose
 
J

JQuest

void PrintObjectRepresentation(FILE *fp, void *p, size_t len)
{
unsigned char *objrep = p;
while(len--)
{
printf(fp, " %X", *objrep++); <=================== ?!
}
putc('\n', fp);
}

Is that a typo? I don't get this printf.
 
R

Richard Heathfield

Alan said:
That's completely unfair. You could just as well paint Richard with the
same brush.

But I am not seeking to change the existing behaviour of the newsgroup.
This is transparent character assassination. Do you think we are stupid?

No, but I do think you're misguided.
 
J

Joona I Palaste

Alan Connor said:
Why do you assume that I lack both eyes and common sense?
Richard and a few others have done almost nothing but attack Roos since
the thread began.

There's a reason for that. Could you imagine that the reason Richard
and everyone else says Roose is wrong is -- wait for it -- because
ROOSE IS WRONG?
That's the only troll-like behavior I have seen here.

That's *pedantry*, not *troll-like behaviour*. We like pedantry.
Everything Roos has told me has been verified by my research on the
Net and with local docs.

Which do not define the C language.
Roos has been helpful, much more so than Richard (although that one post
was indeed excellent)
Richard's buddies have mostly posted material that's way over my head and
just confuses me if I let it.

The fact that you don't understand it doesn't mean it's wrong, and the
fact that you understand it doesn't mean it's right.
At least Roos knows that a novice doesn't know the lingo yet.

He doesn't know it himself, so he frequently makes mistakes, and those
who he teaches will make the same mistakes too.
Yet you are telling me the opposite is true.

We are telling you Roose is spreading false information.
We'll see. But trolling gets anyone killfiled for a while. I don't care
if it's being done by Dennis Ritchie.

If Dennis Ritchie starts trolling in here, you can be certain we'll
killfile him too.
 
J

Joona I Palaste

Just engaging in some light wordplay, children's games if you will. You're
familiar with those. Remember that other thread?

This appears to be Roose's mantra for arguments he can't counter.

Stop quoting people's signatures, Roose.
 
J

Joona I Palaste

Is that a typo? I don't get this printf.

Neither do I. Methinks it should be fprintf.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
T

Thomas Stegen

Roose wrote:

Let me just chip in what I see is going on here.
Are you suggesting that I've tortured you? I didn't realize that forcing
all your inadequacies and defects of personality into the light -- oh god
the light -- would be so painful. So I understand the reluctance to answer
simple, straightforward questions.

The only one in pain here as I can see is Roose. And the
sheer terror he displays shines like a beacon is flooding
the place.

Just get over your fear of pain and just admit you are wrong.
This is just pathetic. I've seen people like you before, in the
end all you can think about is debunking your adversaries, and
when you can't you totally break down.

Why is it so bloody hard to point your newsreader to a different
newsgroup if you want to discuss matters outside the scope of
this newsgroup? Is it because you need your holy crusade to fill
your life with some meaning? If not, then _what_ is the problem
with discussing ISO C here and platform specific stuff elsewhere?

I am just asking because your behaviour is so totally beyond any
comprehensible reason.

Good bye.
 
R

Randy Howard

That's completely unfair.

Why? A large number of his posts seem to contain complaints about how
others answer questions here. IOW, he's acting like a moderator, albeit
not a very diplomatic one.
You could just as well paint Richard with the same brush.

I can't remember Richard doing much complaining about *how* people respond
to questions. When he finds fault, it is almost always on a technical
point, rather than a "religious" one.
This is transparent character assassination.

Actually, I am more and more convinced you are a transparent sockpuppet
for Roose.
Do you think we are stupid?

Please define "we".
 
M

Mark Gordon

Thanks. Dont' get it.

The register qualifier is a suggestion to the compiler that it stores
the object in a register rather than in memory. Since registers are
generally not memory mapped the C language makes the compilers job
easier by preventing you from attempting to take the address of a
register qualified object.
 
R

Richard Bos

Roose said:
Can you explain how an error in not writing standard C might result in such
a bug?

_That_ exact bug? No, obviously not, since Keith already said that that
scenario was a spur-of-the-moment invention. However, some bugs can be
pretty arcane, and only show up under unusual circumstances. For
example, search the Jargon File for "phase of the moon".

For an example where assuming that "integer" is equivalent to "pointer"
leads to a bug, assume that a pointer is six bytes and the best
available integer four.
Suppose, also, that for normal users, including developers, their
address space is in the lower part of the spectrum, so that they'll
never encounter the situation where a pointer converted to an integer
overflows, which means that for them, converting a (small) pointer to an
integer and back again apparently works.
Now suppose that your manager, who never uses the program but wants to
demonstrate it to a customer, happens to have a wheel bit set, causing
his address space to be in the upper reaches of memory, which means that
in his case the higher-order bytes of his pointers are _not_ all zero,
unlike the usual case...

Richard
 
M

Mark Gordon

So, can anyone explain to me why everyone is so insistent on not
top-posting, when they don't care about the long sigs?

I mean, an explanation other than hypocrisy.

<snip>

The 4 line rule is to prevent excessive use of band width in the days of
slow modems and also so that you did not have to page through a lot of
stuff after the main part of the message. In these days when most people
have at least a 33K connection (some cannot get more in the UK due to
limitations in the phone system) the band width is no longer such a big
issue so many people don't worry about an extra few lines if it does not
get out of hand. Others (including some here) still complain about sigs
longer than 4 lines, and they are within there rights to do so.

Top posting, on the other hand, makes it a real pain for everyone even
if a message has been snipped. For example, most news readers (even OE)
allow you to page through messages on a group by just hitting the space
bar. With bottom posting this works, but if a message is top posted then
to see the context you have to page down though the post then go back to
the top (I have so much else on the screen at the moment that there is
only space for half a dozen lines of the message to be displayed). So
ALL the arguments against top posting are still valid.
 
M

Mark Gordon

Uh, I'm not exactly sure what a symlink is, but if it is anything like
a Windows shortcut, there is definitely an analogy there.

The windows shortcut just stores the location of a file. A "pointer"
to it. An address. The address is the _path name_. In C, the
"address" is basically an integer, indicating where in a huge array of
memory the variable is.

You can have as many shortcuts as you want that point to the file. A
shortcut takes a small amount of storage, but not as much as a
(typical) file. You can have whole directories of shortcuts that
point to files all over the place, in order to organize them.
Likewise, you can have collections of pointers, that point to various
different things _scattered_ all over memory.

<snip>

On many platforms a pointer is basically *NOT* an integer. To provide
one concrete example, on the X86 in some of its modes (I'm not up to
date on all of its memory models) a pointer is a PAIR of integers with
more that one way of representing every location. Also, thinking of them
as integers can lead to alignment problems and various other nastiness.

The symlink (or shortcut) analogies are better in many ways since they
do not introduce a lot of this confusion. However, IMHO a symlink or
shortcut is most like a void* pointer since it can point to any type of
object, if you treat the object it points to as the wrong type (such as
treating a spreadsheet as a text document) you hit all sorts of
problems, and you can't do arithmetic on it.

However, if you have a pointer to a specific type (say integer) you can
also do things like ask for the pointer to the next integer, although
this only works if the integers are in a block because you are either
pointing to an array of integers or a block of allocated memory you are
using to store integers in.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top