0

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

  1. Why is there a difference?
  2. How do I change the code so it prints on each operating system the correct number of arrays in member variable _palette?
  3. OPTIONAL: Is there a more concise way to achieve my goal without multidimensional arrays? Either in C++98 or C++11?
Username
  • 3,463
  • 11
  • 68
  • 111
  • 1
    You don't say what size it is on Linux .. also, are you sure your MinGW is building in 64-bit instead of 32? `4` is the typical size of a pointer in a 32-bit application, and you're printing the `sizeof` a pointer – txtechhelp Feb 12 '16 at 22:35
  • 1
    AFAIK, mingw has no 64-bit support. – molbdnilo Feb 12 '16 at 22:42
  • @txtechhelp It prints `8` on Linux, `4` on Windows10 – Username Feb 12 '16 at 22:43
  • 1
    You probably want `std::vector`. – Jarod42 Feb 12 '16 at 22:45
  • @molbdnilo If I want to make .exe for Windows without using a `std::vector`, would I simply need to change compiler? I'll likely end up using a vector, but I'm curious if this is possible without it. – Username Feb 12 '16 at 22:47

1 Answers1

3

Your function palette returns a pointer; the sizeof is telling you the sizeof a pointer on the system. Apparently, the sizeof a pointer on your Linux and Windows machines are different, which is why you get the different results. sizeof cannot track the amount of memory attached to a pointer, you have to keep track of that yourself manually.

R_Kapp
  • 2,818
  • 1
  • 18
  • 32
  • Thankfully, `_palette`'s size can be manually set for this program, but what if in future I need it to vary during runtime? – Username Feb 12 '16 at 22:39
  • 2
    Store the size in a variable and keep track of it manually, or use `std::vector`, which will do that for you. – R_Kapp Feb 12 '16 at 22:39
  • Separate question, but how can vectors replicate multidimensional arrays, or at least 2D arrays? – Username Feb 12 '16 at 22:40
  • 1
    @Username: `std::vector > _pallete` for example. Or, if you know the second array size is guaranteed to be `3`, `std::vector _pallete[3]`. – R_Kapp Feb 12 '16 at 22:42
  • 2
    @Username: Another method would be to write a wrapper class yourself. – R_Kapp Feb 12 '16 at 22:44
  • 1
    @Username warning: vector> has a hidden performance hit. Each vector's data will be allocated somewhere in memory and not necessarily anywhere close to the others. This means that the CPU's attempts to preload and cache data will fail with high frequency. SergeyA had a post on this: http://stackoverflow.com/questions/34077816/how-to-properly-work-with-dynamically-allocated-multi-dimensional-arrays-in-c – user4581301 Feb 12 '16 at 23:20