Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not perform a sizeof against a static char[] of another class?

Tags:

c++

static

sizeof

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

like image 809
Alan Avatar asked Dec 08 '09 10:12

Alan


2 Answers

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.
like image 169
deft_code Avatar answered Nov 09 '22 22:11

deft_code


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_and_incomplete_types

like image 3
Fox Avatar answered Nov 09 '22 21:11

Fox