sizeof and incomplete types

A

aegis

sizeof could not possibly evaluate to some size for a type, unless
two things for a type occur:

a) the type is complete and sizeof will evaluate to appropriate size
b) type is not complete and any sizeof use with incomplete type
results in undefined behavior.

Is 'b' so? If so, where in the standard does it say this?

I only bring this up because of the following:

Breakpoint 1, main () at malloc.c:9
9 foo = malloc(sizeof foo);
(gdb) n
11 return 0;
(gdb) p foo
$1 = 0x80495d8
(gdb) p sizeof foo
$2 = 4
(gdb) list malloc.c:1,15
1 #include <stdlib.h>
2
3 typedef struct foo *FOO;
4
5 int main(void)
6 {
7 FOO foo;
8
9 foo = malloc(sizeof foo);
10
11 return 0;
12 }
(gdb)
 
K

Keith Thompson

aegis said:
sizeof could not possibly evaluate to some size for a type, unless
two things for a type occur:

a) the type is complete and sizeof will evaluate to appropriate size
b) type is not complete and any sizeof use with incomplete type
results in undefined behavior.

Is 'b' so? If so, where in the standard does it say this?

I only bring this up because of the following:

Breakpoint 1, main () at malloc.c:9
9 foo = malloc(sizeof foo);
(gdb) n
11 return 0;
(gdb) p foo
$1 = 0x80495d8
(gdb) p sizeof foo
$2 = 4
(gdb) list malloc.c:1,15
1 #include <stdlib.h>
2
3 typedef struct foo *FOO;
4
5 int main(void)
6 {
7 FOO foo;
8
9 foo = malloc(sizeof foo);
10
11 return 0;
12 }
(gdb)

foo is a pointer object. struct foo is an incomplete type, but
struct foo* (aliased as FOO) isn't.

Your use of the identifier foo both as a struct tag and as the name of
a pointer object to that struct is potentially misleading.
 
E

Eric Sosman

aegis said:
sizeof could not possibly evaluate to some size for a type, unless
two things for a type occur:

a) the type is complete and sizeof will evaluate to appropriate size
b) type is not complete and any sizeof use with incomplete type
results in undefined behavior.

Is 'b' so? If so, where in the standard does it say this?

By 6.5.3.4/1 it is a constraint violation to apply `sizeof'
to an incomplete type. By 5.1.1.3/1 the constraint violation
must elicit a diagnostic message.
I only bring this up because of the following:

Breakpoint 1, main () at malloc.c:9
9 foo = malloc(sizeof foo);
(gdb) n
11 return 0;
(gdb) p foo
$1 = 0x80495d8
(gdb) p sizeof foo
$2 = 4
(gdb) list malloc.c:1,15
1 #include <stdlib.h>
2
3 typedef struct foo *FOO;
4
5 int main(void)
6 {
7 FOO foo;
8
9 foo = malloc(sizeof foo);

`foo' has the type `pointer to struct foo'. While
`struct foo' is an incomplete type, `pointer to struct foo'
is not: it is a "complete" type whose size is known, even
though the size of what it points to is not known. Hence,
`sizeof' can be applied to it.

However, the allocation is very likely wrong. If the
intent is to allocate memory for `foo' to point at, this
works only if an actual `struct foo' turns out to be no
larger than a pointer. If you had used the recommended style

foo = malloc(sizeof *foo);

.... you would have received a diagnostic because `*foo' has
an incomplete type.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top