Static arrays defined with undefined size, empty brackets?

For the C ++ code snippet below:

class Foo { int a[]; // no error }; int a[]; // error: storage size of 'a' isn't known void bar() { int a[]; // error: storage size of 'a' isn't known } 

why does a member variable also throw an error? and what is the meaning of this member variable?

I am using gcc version 3.4.5 (special version of mingw-vista) through CodeBlocks 8.02.

In Visual Studio Express 2008 - Microsoft (R) C / C ++ Compiler Optimization 15.00.30729.01 for 80x86, I received the following messages:

 class Foo { int a[]; // warning C4200: nonstandard extension used : zero-sized array in struct/union - Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array }; int a[]; void bar() { int a[]; // error C2133: 'a' : unknown size } 

Now this requires some explanation.

+7
c ++ compiler-construction class
source share
4 answers

C99 supports something called a "flexible" array element, which is allowed to be the last member of the structure. When you dynamically allocate such a structure, you can increase the requested amount from malloc() to provide memory for the array.

Some compilers add this as an extension to C90 and / or C ++.

So you can have the following code:

 struct foo_t { int x; char buf[]; }; void use_foo(size_t bufSize) { struct foo_t* p = malloc( sizeof( struct foo_t) + bufSize); int i; for (i = 0; i < bufSize; ++i) { p->buf[i] = i; } } 

You cannot define a structure with a flexible array element directly (as a local or global / static variable), because the compiler will not know how much memory is allocated for it.

I honestly don't know how you can easily use such a thing with the C ++ new operator - I think you will need to allocate memory for the object using malloc() and use the new location. Perhaps you can use some operator new type overloading for the class / structure ...

+6
source share

C ++ language allows to exclude the size of the array only in non-defining declarations

 extern int a[]; // non-defining declaration - OK in C++ int a[]; // definition - ERROR in C++ int a[5]; // definition - OK, size specified explicitly int a[] = { 1, 2, 3 }; // definition - OK, size specified implicitly 

Non-static decations of class members are always necessary to indicate the size of an array.

 struct S { int a[]; // ERROR in C++ }; 

while static bits of class members can omit size

 struct S { static int a[]; // OK in C++ }; 

(the definition of the same element, of course, should indicate the size).

Any deviations from this behavior can only be explained by the extended non-standard behavior of your compiler. Perhaps you should specify some additional compiler options to make it behave more pedantically.

+11
source share
 class Foo { int a[]; // OK in C, invalid in C++. Does not work with inheritance. }; // Idea is that structure is "extended" indefinitely by an array. // May work on your compiler as an extra feature. int a[]; // error in C and C++: storage size of 'a' isn't known void bar() { int a[]; // error in C and C++: storage size of 'a' isn't known } extern int a[]; // OK: storage size may be declared later. int a[5]; // declaration of size before use. 

An array type with an undefined size is incomplete. 8.3.4 / 1:

If the constant expression is omitted, then the identifier type D is an "array from the derived-declarator-list-type of an unknown boundary T", an incomplete object type.

It must be filled in to participate in the definition, that is, the definition of a must contain a size specification or initialization with an array of the specified size.

+2
source share

We used this to indicate variable length records. Something like a header file that contains information about how many structures follow them, and then the data itself. This is a variable-length array, and I found that it is not supported between compilers. Some people need an array []; and some want array [0]; (old style).

0
source share

All Articles