tags:

views:

285

answers:

4

Why does the following code generate a compile error?


Edit: My original code wasn't clear - I've split the code up into separate files...


First.h

class First
{
public:
    static const char* TEST[];

public:
    First();
};

First.cpp

const char* First::TEST[] = {"1234", "5678"};

First::First()
{
    uint32_t len = sizeof(TEST); // fine
}

Determining the size within the First class seems fine, however...

Second.h

class Second
{
public:
    Second();
};

Second.cpp

#include "First.h"

Second::Second()
{
    uint32_t len = sizeof(First::TEST); // error
    uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
}

I get the following error: 'const char *[]': illegal sizeof operand

+1  A: 

For me even the first sizeof(TEST) fails to compile as TEST has not been declared with a size (that is only resolved at link time).

If I change TEST[] to, say, TEST[10], then both cases compile.

Both results are what I would expect.

Which compiler are you using?

Phil Nash
+2  A: 

It gets clearer when you expand Second.cpp. This is the whole thing that is being compiled (1 compilation unit):

class First
{
public:
    static const char* TEST[];

public:
    First();
};


class Second
{
public:
    Second();

    Second::Second()
    {
       uint32_t len = sizeof(First::TEST); // error
       uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
    }
}

If you look here, First::TEST clearly has no size, and sizeof(FIRST::TEST) is meaningless.

Why not just have a method that returns the length of TEST?

moogs
+2  A: 

Primarily because the compilation of First.cpp and Second.cpp are independent of each other.

sizeof() in Second is (generally) resolved at compile time, when only the array's declaration is known, and space for the static hasn't been allocated cannot be calculated. See http://en.wikipedia.org/wiki/Sizeof#sizeof%5Fand%5Fincomplete%5Ftypes

Fox
sizeof is ALWAYS calculated at compile time.
caspin
@Caspin - http://www.comeaucomputing.com/techtalk/c99/#variablelengtharrays
Fox
+1  A: 

sizeof only works on complete types. const char* TEST[] is not a complete type until it is defined in First.cpp.

sizeof(char*[10]) == sizeof(char*) * 10 == 40
sizeof(short[10]) == sizeof(short) * 10 == 20

// a class Foo will be declared
class Foo;
sizeof(Foo) == //we don't know yet

// an array bar will be defined.
int bar[];
sizeof(bar) == sizeof(int) * ? == //we don't know yet.

// actually define bar
int bar[/*compiler please fill this in*/] = { 1, 2, 3 };
sizeof(bar) == sizeof(int) * 3 == 12
// note bar is exactly the right size

// an array baz is defined.
int baz[4];
sizeof(baz) == sizeof(int) * 4 == 16

// initialize baz
int baz[4] = { 1, 2, 3 };
sizeof(bar) == sizeof(int) * 4 == 16
// note baz is still 4 big, the compiler doesn't control its size

To get this to work as you wish, you can:

  • add the size of the First::TEST array to its declaration (static const char* TEST[2];)
  • add a new static method that returns the sizeof First::TEST. The method cannot be inline, it would have to be defined in First.cpp.
caspin