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?
5
  • 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 Commented Feb 12, 2016 at 22:35
  • 1
    AFAIK, mingw has no 64-bit support. Commented Feb 12, 2016 at 22:42
  • @txtechhelp It prints 8 on Linux, 4 on Windows10 Commented Feb 12, 2016 at 22:43
  • 1
    You probably want std::vector<RGBColor>. Commented Feb 12, 2016 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. Commented Feb 12, 2016 at 22:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thankfully, _palette's size can be manually set for this program, but what if in future I need it to vary during runtime?
Store the size in a variable and keep track of it manually, or use std::vector, which will do that for you.
@Username: std::vector<std::vector<int> > _pallete for example. Or, if you know the second array size is guaranteed to be 3, std::vector<int> _pallete[3].
@Username: Another method would be to write a wrapper class yourself.
@Username warning: vector<vector<type>> 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: stackoverflow.com/questions/34077816/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.