Incomplete types in C

6.2.5

At different points within the translation unit, there may be an incomplete type of object (lack of sufficient information to determine the size of objects of this type).

Besides

6.2.5 19) The void type contains an empty set of values; this is an incomplete type of object that cannot be completed.

and

6.5.3.4 The sizeof operator does not apply to an expression that has a function type or an incomplete type,

But Visual Studio 2010 prints 0 for

 printf("Size of void is %d\n",sizeof(void)); 

My question is "What are incomplete types "?

 struct temp { int i; char ch; int j; }; 

Is temp here incomplete? If so, why is it incomplete (we know the temp size)? Do not get a clear idea of incomplete types . Any piece of code that explains this will be helpful.

+4
source share
3 answers

Your struct temp is incomplete until the moment the closure occurs:

 struct temp { int i; char ch; int j; };// <-- here 

The structure is declared (appears) after the temp character, but it is incomplete until the actual definition is complete. This is why you can have things like:

 struct temp { int i; char ch; struct temp *next; // can use pointers to incomplete types. }; 

without getting syntax errors.

C makes a distinction between a declaration (declaring that something exists) and a definition (actually determining what it is).

Another incomplete type (declared, but not yet defined):

 struct temp; 

This case is often used to provide opaque types in C where a type exists (so you can declare a pointer to it) but not defined (so you may not find out what is there). The definition is usually limited to the code that implements it, while the header used by clients has only an incomplete declaration.

+8
source

No, your struct temp example is definitely complete; Assuming int is 4 bytes and char is 1, I can easily count 9 bytes in this structure (ignoring padding).

Another example of an incomplete type:

 struct this_is_incomplete; 

This tells the compiler: "Hey, this structure exists, but you still don't know what's in it." This is useful for hiding information, but when you need to pass a pointer to a type:

 int some_function(struct this_is_incomplete* ptr); 

The compiler can correctly generate calls to this function because it knows that the pointer has 4 (or 8) bytes, although it does not know how big the thing the pointer points to.

+2
source

A type may be incomplete if its name is declared, but not its definition. This happens when you forward an ad to a header file.

Let's say record.h contains:

 struct record_t; void process_record(struct record_t *r); 

And the .c entry contains:

 struct record_t { int data; }; 

If in another module, say, "usage.c", you do this:

 #include "record.h" const int rec_size = sizeof(struct record_t); // FAIL 

The record_t type is incomplete inside the compilation block "usage.c", because it knows only the name of record_t , and not what this type consists of.

+1
source

Source: https://habr.com/ru/post/1501586/


All Articles