Re: Pointer dereference rather than sizeof?
- From: "Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx>
- Date: Tue, 26 Aug 2008 00:48:17 GMT
Keith Thompson <kst-u@xxxxxxx> wrote in message
news:ln8wulczzu.fsf@xxxxxxxxxxxxxxxxxx
Micheal Smith <xulfer@xxxxxxxxxxxx> writes:
evenI recently read an article containing numerous gripes about common C
practices. One of them contained a gripe about the use of the sizeof
operator as an argument to malloc calls. The supposed "right" way to go
about calling malloc instead is to dereference a pointer; apparently
Isif said pointer is undefined.
E.g.
ptr = malloc(sizeof(struct some_struct)); = wrong
ptr = malloc(*ptr); = right
The method works on my platform, but doesn't really sit right with me.
comethe code portable? Standard? I've been looking myself, but haven't
across anything forbidding it as of yet.
As you acknowledged later in the thread, the correct form is
ptr = malloc(sizeof *ptr);
or, if you're uncomfortable with using sizeof without parentheses:
ptr = malloc(sizeof(*ptr));
Or if you want to allocate an array:
ptr = malloc(N * sizeof *ptr);
As "fjblurt" pointed out, this has the advantage that it's obvious
you're using the correct type; if during later maintenance you change
ptr from a foo* to a bar*, the malloc call will continue to be
correct.
The trick here is that the pointer isn't actually dereferenced. The
operand of a sizeof operator is not evaluated; it's only used to
determine the type of the expression. (Variable-length arrays, or
VLAs, a new features in C99, are an exception to this.) So the
expression ``sizeof *ptr'' is guaranteed not to attempt to dereference
ptr. It doesn't even evaluate the pointer value itself.
Your discomfort over what looks like deferencing a possibly invalid
pointer is reasonable, but it's perfectly safe in this case.
Of course it looks like it is de-referencing the pointer, because that's
what de-referencing a pointer looks like everywhere else in "C" code.
At the risk of repeating myself, I'm going to repeat myself:
"It's completely friggin' impossible to learn all the bass-ackwards
and semi-sideways inconsistent methods of declaring and
dereferencing pointers in "C". NO non-lunatic has ever been able
to grasp the total non-logic of the topic, which is just one
incomprehensible aspect of an ostensible "programming language"
that was obviously actually designed as a cruel joke on anybody
who would attempt to use it (I can just hear the uber-techno-trolls
K&R sniggering about it now)..."
If you DO choose to use this so-called "language", NEVER blame
yourself if you get massively confused by this type of inconsistent
syntax, IT'S NOT YOUR FAULT, YOU'VE BEEN "KERNIGHANED
AND RITCHIED", PUNK...
---
William Ernest Reid
.
- References:
- Pointer dereference rather than sizeof?
- From: Micheal Smith
- Pointer dereference rather than sizeof?
- Prev by Date: Re: gdb vs C standard
- Next by Date: Re: P142 K&R
- Previous by thread: Re: Pointer dereference rather than sizeof?
- Next by thread: Re: Pointer dereference rather than sizeof?
- Index(es):