473,659 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6180
"aegis" <ae***@mad.scie ntist.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_Keit h) 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
2456
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
2201
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" and "Process". They may be instantiated by a "Factory". Here is a simplified, but compilable version: // Abstract Interface enum OsSelector {
1
3198
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. generic<typename T> void CopyArrayFromArray(array<T>^ % vVal, int size
28
6734
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 in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
11
2955
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
8854
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
3848
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...) -- William http://williamxu.net9.org
16
15648
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
6393
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 "right" way to go about calling malloc instead is to dereference a pointer; apparently even if said pointer is undefined. E.g. ptr = malloc(sizeof(struct some_struct)); = wrong ptr = malloc(*ptr); = right
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.