The program
I use Eclipse to write, compile, build and run this code. Both on Windows and Linux.
Card.h
class Card {
private:
static int _palette[][3];
public:
static int (*palette())[3];
};
Card.cpp
#include "Card.h"
int Card::_palette[][3]= {
{168, 0, 32},
{228, 92, 16},
{248, 216, 120},
{88, 216, 84},
{0, 120, 248},
{104, 68, 252},
{216, 0, 204},
{248, 120, 248}
};
main.cpp
#include <iostream>
#include "Card.h"
int main(int argc, char **argv) {
int uniqueColors= sizeof(Card::palette());
std::cout << uniqueColors << std::endl;
return 0;
}
This prints 4
on my Windows10 OS, 8
on Debian 8.2 Jessie.
Windows build log
Here's Eclipse's console on 64bit Win10 when I build with MinGW GCC toolchain and CDT Internal Builder:
16:53:09 **** Rebuild of configuration Debug for project sizeOf-test ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o Card.o "..\\Card.cpp"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp"
g++ -o sizeOf-test.exe Card.o main.o -lmingw32
16:53:11 Build Finished (took 1s.934ms)
When I run the program, it prints 4
.
Linux build log
Here's the Eclipse console on 64bit Debian 8.2 Jessie, using the Linux GCC toolchain and CDT Internal Builder:
17:17:57 **** Incremental Build of configuration Debug for project cpp-sizeof-test ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o ../main.cpp
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o Card.o ../Card.cpp
g++ -o cpp-sizeof-test Card.o main.o
17:17:57 Build Finished (took 327ms)
Questions
- Why is there a difference?
- How do I change the code so it prints on each operating system the correct number of arrays in member variable
_palette
? - OPTIONAL: Is there a more concise way to achieve my goal without multidimensional arrays? Either in C++98 or C++11?