473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sizeof and incomplete types

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)
--
aegis

Dec 4 '05 #1
2 6155
"aegis" <ae***@mad.scientist.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 4 '05 #2
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 *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.

--
Eric Sosman
es*****@acm-dot-org.invalid

Dec 4 '05 #3

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

Similar topics

2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
1
by: marco_segurini | last post by:
Hi, I like to know if is it possible to use sizeof(T) inside a generic function. I don't know why the following function compiles while if I define ON_LINE_STATEMENT it does not. ...
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
11
by: nevergone | last post by:
Hello Everybody In <<Modern C++ Design>Compile-Time Assertions there is : template <boolstruct CompileTimeChecker { CompileTimeChecker(...); }; template <struct CompileTimeChecker<false{ };
7
by: David Resnick | last post by:
I'm faced with a header with anonymous structures declared inside a union like this: union msg { struct { int a; int b; } s1; struct {
56
by: William Xu | last post by:
I test it with char, short, int, long, float, double and struct pointers, all return 4 bytes. Does the standard say anything about sizeof a pointer? (I didn't find it in the standard...) --...
16
by: Alex Vinokur | last post by:
Does it have to be? : sizeof (size_t) >= sizeof (pointer) Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
98
by: Micheal Smith | last post by:
I 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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.