473,889 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Mastering C Pointers"....

Hey guys, I'm new here, just a simple question.

I'm learning to Program in C, and I was recommended a book called,
"Mastering C Pointers", just asking if any of you have read it,
and if it's worth the $25USD.

I'm just looking for a book on Pointers, because from what I've
read it's one of the toughest topics to understand.

thanks in advanced.

sincerely ... Andy
Nov 13 '05
388 21994
"Roose" <no****@nospam. nospam> writes:
[...]
OK, suppose you want to serialize a group of structs, which contain pointersn
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.)


If all the structs are part of a single object (e.g., a single chunk
of memory allocated by malloc()), the kind of thing you describe can
be done portably. For each pointer, cast it to char* and subtract the
base address of the enclosing object to get the byte offset of the
object referenced by the pointer.

On the other hand, if it's not practical for all your structs to be
part of the same object (e.g., you want to malloc() them individually)
the kind of mapping and serialization you describe would depend on the
details of how malloc() allocates memory. I don't know enough about
the inner workings of typical heap implementations to know whether
this would be practical, but I suspect there's enough variation to
make it difficult.

You mention subtracting the base address; where does this base address
come from?

If non-portable code really is the best solution for your application,
by all means write non-portable code. Wherever possible, keep it
isolated in a small number of modules, and write the majority of your
application as portably as you can. This will make your job easier
when you need to port it to another platform; the code you have to
modify will be small and contained.

I don't think anybody here has claimed that all C code must be
strictly conforming, or that non-portable code is evil. Non-portable
code is sometimes necessary. But I've found that clean code tends to
be portable, and vice versa. If you develop the right habits, writing
portable code really isn't all that difficult. But when you write
code that depends on the characteristics of a particular system,
you've left the scope of this newsgroup, and if you have any questions
about it, you're more likely to get correct answers in a newsgroup
devoted to that system.

[...]
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?


Not specifically, but imagine a program that operates on a pointer in
a manner that only works if pointers are just integers. When the
program is ported to a system where that's not the case, it generates
pointer values that point to random locations in memory. If it writes
a value through such a pointer, it can clobber some arbitrary
variable. Sometimes this may be harmless, but sometimes, depending on
the circumstances, the result can be drastic.

Failure to check against buffer overflow is actually a much more
common way for this kind of thing to happen, but anything that might
generate an invalid pointer value can cause problems like this.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #161
"Roose" <no****@nospam. nospam> wrote:

[top-posting fixed]

"Irrwahn Grausewitz" <ir*******@free net.de> wrote:

If you are not interested in correct information you can get from
the other posters here, take this Roose troll with you and move over
to alt.comp.lang.c . Virtually nobody else is posting there, so maybe
it's a good place for you two to stay.
[Please snip signatures when replying. Do it manually if your
news-reader is defective.]
Again, this is completely transparent.


It was meant to be transparent. I'm not into playing hide and seek,
neither here nor anywhere else. If you can't deal with this - well,
follow your own advice and killfile me.
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #162
"Roose" <no****@nospam. nospam> wrote:

[top-posting fixed]
"Irrwahn Grausewitz" <ir*******@free net.de> wrote:
Alan Connor <zz****@xxx.yyy > wrote:
What Roose posted was very helpful to me.

I am not going to dismiss an obviously intelligent and learned person on
your say so.
You enjoy to be fed by an obvious troll? Happy proceedings.


[Please snip signatures when relying.]

<snip> It's completely transparent. If I were a troll, then you just let common
sense run its course. It's because you think I actually something valuable
to say, that competes with your jack-off C standard knowledge, that you must
repeatedly, vehemently insist that I'm a troll.
No, it's simply because you keep dispensing (at least partly) incorrect
information.
And, to which I would respond, as in the previous thread -- if I'm a troll,
then follow your own netiquette and killfile me. I don't care to be
responded to by people who don't think that I have something valuable to
say.
If you really don't care, as you say, then why not plonk me in the first
place?
Note that you can killfile me, safe with the knowledge that there will be
plenty of others to nitpick. Really.
Not so: if everybody would do so, there would be nobody left to correct
your errors. Ever heard of Kant?
I'm not just addressing Grausewitz here, but everyone who is flaming Alan
for killfiling one of your Gods.
You're most certainly referring to Alan killfiling Richard Heathfield
here? Well, AFAICT Heathfield is neither a god, nor even god-like.
*But* he has an impressive knowlegde of C and I like his style of
explaining difficult matters to others.

And I'm not going to mention all the other regular and/or knowledgeable
contributors to this group, but only for the sake of brevity...

A big thank-you to all of you for providing help and information, and
for patiently pointing out errors and correcting misconceptions. In the
last twelve weeks I learned more about ISO C than in the ten years
before.
P.S. by jack-off, I don't mean technically wrong or even uninteresting, just
that it's obviously not helping the bigger picture. It just serves to show
off to everyone how much you know.


Not so. Looking at my (short) posting history it seems to me I showed
how much I still have to learn about writing correct C. (This is not
meant to be understatement, it's merely my impression.) Alas, I'm eager
to learn, and being corrected is part of the learning process. Sadly
(for you), it appears that you are neither able to deal with being
corrected by others nor able to accept the simple conventions that help
making c.l.c a very useful institution. You just don't know what you
are missing, honestly. The same applies for poor Alan Connor as well.

Finally, this is going to be my last post in this unholy thread.
Back to C now?!?

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #163
Alan Connor wrote:
What's a "register (qualified) object"?


"register" is a keyword.
Do you know what "keyword" means ?

--
pete
Nov 13 '05 #164
I've changed the title because I think Roose may have raised an
interesting subject. And we might get away from the "Did"-"Didn't"
style of discussion.

Roose, could you try and adhere to some usenet conventions?
1. don't top-post
2. leave in the attributions

"Roose" <no****@nospam. nospam> wrote in message news:<Z5******* ***********@new ssvr14.news.pro digy.com>...
Before responding, I'd appreciate it if you answer the questions you ignored
from my posts... otherwise I'm not going to bother responding to your post,
either.

[Richard Heathfield] wrote:
If one wishes to write /correct/ C programs, an understanding of the
principles laid out in the Standard is vital. The easiest way to achieve
this understanding is by reading and understanding the C Standard.
What percent of the total number of C programs in the world do you think are
correct in this sense?


I don't think anyone can hazard a guess at this. The percentage is
high in
clc. Probably wildly variable in the rest of the world.
Was your first C program correct in this sense?
if you mean a "hello world". Probably. A substantial (non-toy)
program? No.
Would it have taken more
time and effort to make it correct, knowing what you knew then?
yup, lots more. The main problem was not knowing what I didn't know. I
mean
I didn't even realise which areas I was ignorant in.

- void main()
- reliance on byte ordering
- a ptr *will* go in an int
- cast malloc()

etc.
[...] What do you
think the OP is interested in doing, _at this stage in the game_?
writing programs that work, and writing better programs in the future.
(you seem to be of the (common) school of thought that omits step 2).
[...] Do think
he is interested in reading the C standard, before having written anything
substantial or understanding pointers at a practical level?

No sarcasm there, just answer honestly.


why not try both? Why not write the program as best you can then
accept the constructive criticism of more experienced C programmers?
Hell this applies
to *everything* from programming to cooking to riding a bike. It's the
people who think they know it all that are the hardest to teach.

Maybe I got off to a bad start. The first programming language I
learnt was
Algol-60. I read the Revised Report. Maybe that spoilt me. I've always
tried to read the appropriate standard since (I couldn't manage the
Algol-68 Standard for some reason...).

<rest of post snipped off>
--
Nick Keighley

"ALGOL 60 was a language so far ahead of its time that it
was not only an improvement on its predecessors but also
on nearly all its successors".
--C.A.R. Hoare
Nov 13 '05 #165
On Mon, 03 Nov 2003 23:49:56 +0000
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Mon, 03 Nov 2003 00:29:05 GMT, in comp.lang.c , Alan Connor
<zz****@xxx.yyy > wrote:


<snip>
Mark: The limit for sigs on the Usenet is 4 lines and they need to be
immediately below a "-- " line.


I have no control over what alibis.com stick on after my sig, which is
three lines long.


<snip>
What Newsfeed.Com is doing amounts to nothing but spam. Ditch them.


Its that or ntl's servers. Believe me, you don't want to use ntl's
news servers for more than about ten seconds.


You could use news.individual .net (formerly news.cis.dfn.de ) for text
groups. They are free and provide a decent news feed. I used them as my
main news server for a while when I was connecting though NTL.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #166
"Roose" <no****@nospam. nospam> wrote in message news:<W0******* ************@ne wssvr21.news.pr odigy.com>...
Let me preface this with some meta-comments. If your goal is to learn C, by
all means go ahead and dive right into the C language. You only learn by
making mistakes


never get a job in bomb disposal

<snip>

--
Nick Keighley
Nov 13 '05 #167
Alan Connor wrote:
[snip]
Thanks. Dont' get it.

Please fix your sig, Lew. It's over 4 lines and I'll have to killfile you
for 30 days if I see it again.


Imagine how heartbroken I feel.

It's your loss, Alan, not mine.
--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #168
CBFalconer wrote:
Lew Pitcher wrote:
Alan Connor <zz****@xxx.yyy > wrote:


... snip ...
What's a "register (qualified) object"?


register int SomeThing;

as opposed to

int SomeThingElse;

Given
int *PointerToSomet hing;

you can
PointerToSometh ing = &SomeThingEl se;
but not
PointerToSometh ing = &SomeThing;
because SomeThing is a register qualified object,
but SomeThingElse is not.

Which doesn't answer his question. The above is a penalty for
using register qualification, which actually means "I think this
variable would be better handled in a register". It is only a
suggestion, and the compiler need not pay any attention to it,
other than to enforce the above penalty. With modern optimizers
it is usually better not used.


Rather than confuse Alan with extraneous information (Alan can get confused
at times), I attempted to answer his literal question. I decided that the
explanation that 'register' was a hint to the compiler that may have a
vestigal machine-language impact (and thus the language restrictions) was
perhaps going a bit too far.

Given that the 'register' keyword has a limited number of features wrt the C
language (as opposed to it's implementation on any given platform), and that
the most significant of these features is the restriction that the code
cannot take the address of a register variable, I thought that a simple
illustration would be in order.

Alan isn't known for reading complex explanations; he's likely to "killfile"
any respondant that attempts to answer in that manner.
--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #169


Mark McIntyre wrote:
<snip>
By the way, are you also known as roose? I'm suspicious of your sudden
appearance in this thread, and your evident agreement with a troll.
Apologies if I'm maligining you but you see my point?


Alan has been posting to the comp.unix.quest ions and comp.unix.shell
newsgroups for a while now, and I've never seen Roose in either. A
couple of months back Alan mentioned in one that he was interested in
learning C so I suggested he lurk in comp.lang.c for a few weeks to get
a feel for the group and its participants before posting anything. I've
never seen Roose in either of those UNIX NGs, and I'm certain Alan
doesn't know enough C to post the responses we see from Roose. The
postings from Alan here wrt interacting with the experts have been
precisely in-line with his interactions in those other NGs (frequent
flame-wars and threats of killfiling) whereas the postings from Roose
have not had the same tone.

So, I'm 99% sure they are different people. Alan - I hope you don't
tajke this the wrong way as I have nothing against you personally and I
don't think I'm saying anything you wouldn't agree with, I just wanted
to clear up the identify confusion.

Ed.

Nov 13 '05 #170

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.