Quote:
> I've long been wondering what the reason is that the Standard does not
> guarantee that sizeof(type*) would be equal to sizeof(type**). How can
> these two possibly be different?
You must not have thought about this problem for very long.
Suppose that a pointer is 4 bytes and must be aligned on a 4-byte
boundary, whereas a char is 1 byte and need be aligned only on a
1-byte boundary. (The latter is a requirement of the C
standard.) On a "word-addressed" machine the char pointer will
quite possibly need an additional "offset within word" field and
thus void * might be, say, 8 bytes, whereas void ** is only 4
bytes.
Quote:
> Moreover, I can see assumptions that the two are the same size in
> other people's code, for instance, this short excerpt from a widely
> used open source application shows it:
> static char **argument_vector;
> ..............
> argument_vector_size = 1;
> argument_vector = (char **) malloc (argument_vector_size * sizeof
> (char *));
I must be missing something, because I don't see any such
assumption here. But it is a fairly common assumption in code
meant for Unix-like operating systems that all pointers have the
same size.
On the other hand, I'd object to the form of the malloc()
invocation. I don't recommend casting the return value of
malloc():
* The cast is not required in ANSI C.
* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.
* If you cast to the wrong type by accident, odd failures can
result.
When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:
int *x = malloc (sizeof (int) * 128); /* Don't do this! */
Instead, write it this way:
int *x = malloc (sizeof *x * 128);
There's a few reasons to do it this way:
* If you ever change the type that `x' points to, it's not
necessary to change the malloc() call as well.
This is more of a problem in a large program, but it's still
convenient in a small one.
* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
So I'd write the malloc call as
argument_vector = malloc (argument_vector_size
* sizeof *argument_vector);
Quote:
> Is there a compiler out there that has different storage size for
> pointers and pointers to pointers?
Probably, but someone else will have to point out specific
examples.
--
"I should killfile you where you stand, worthless human." --Kaz