C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • sizeof and incomplete types

    2 answers - 781 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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 *F;
    4
    5 int main(void)
    6 {
    7 F foo;
    8
    9 foo = malloc(sizeof foo);
    10
    11 return 0;
    12 }
    (gdb)
  • No.1 | | 1079 bytes | |

    "aegis" <aegis@mad.scientist.comwrites:
    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 *F;
    4
    5 int main(void)
    6 {
    7 F 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 F) 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.
  • No.2 | | 1611 bytes | |

    aegis wrote:
    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 *F;
    4
    5 int main(void)
    6 {
    7 F 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.

Re: sizeof and incomplete types


max 4000 letters.
Your nickname that display:
In order to stop the spam: 7 + 6 =
QUESTION ON "C/C++"

EMSDN.COM