Variably modified struct/union and C99

F

Foo

Hello group. I don't know if the subject was already debated, but I
assumed C99 allowed variably modified arrays in structs and used them in
a project. GCC didn't complain, until I upgraded to a more recent
version (4.3.3 if it matters) which now outputs the following warning:

warning: a member of a structure or union cannot have a variably
modified type

The standard doesn't seem to mention them either so I guess I was wrong.

Anyway, I find those very useful when combined with sizeof to make the
code clearer, for example:

{
size_t a;
size_t b;
void *c;

/* [...] code that computes a and b */

{
struct {
char e[a];
int f;
} *d;

d = malloc(sizeof(*d));

/* [...] code that copies data into d */

d->f[0] = 42;

c = d;
}
}

Since C99 already allows variably modified arrays and using sizeof on
them, what technical reason prevents this syntactic sugar to work with
structs?
 
J

James Kuyper

Foo said:
Hello group. I don't know if the subject was already debated, but I
assumed C99 allowed variably modified arrays in structs and used them in
a project. GCC didn't complain, until I upgraded to a more recent
version (4.3.3 if it matters) which now outputs the following warning:

warning: a member of a structure or union cannot have a variably
modified type

The standard doesn't seem to mention them either so I guess I was wrong.

It does mention them: "A member of a structure or union may have any
object type other than a variably modified type." (6.7.2.1p8)

....
Since C99 already allows variably modified arrays and using sizeof on
them, what technical reason prevents this syntactic sugar to work with
structs?

A struct's layout is intended to be fixed; variably-modified types would
not allow that. Obviously, the layout doesn't have to be fixed, or gcc
wouldn't be able to support this as an option. However, allowing the
layout to be variable complicates the implementation of structs, and the
committee did not want to impose such a requirement.
 
F

Foo

It does mention them: "A member of a structure or union may have any
object type other than a variably modified type." (6.7.2.1p8)

Strangely I couldn't see it earlier. I need a new pair of glasses.
...

A struct's layout is intended to be fixed; variably-modified types would
not allow that. Obviously, the layout doesn't have to be fixed, or gcc
wouldn't be able to support this as an option. However, allowing the
layout to be variable complicates the implementation of structs, and the
committee did not want to impose such a requirement.

Seems logical. Thanks.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top