sizeof(<array>) does not work when array is declared like this

At least with Arduino sizeof(…) does not work if you declare an array like this:

const uint32_t m_arrayColorTable

Where you fill in the elements in the source file like so:
{
RGB(0, 0, 0),
RGB(5, 5, 5),
RGB(10, 10, 10),
RGB(15, 15, 15),
RGB(20, 20, 20),
RGB(25, 25, 25),
RGB(30, 30, 30),
RGB(35, 35, 35),
.
.
.
}

In CRainbow::begin()

 m_nArraySize = sizeof(m_arrayColorTable) / 4;

 Serial.print(sizeof(m_arrayColorTable));
 Serial.print(" ");
 Serial.println(m_nArraySize);

These output 0 as my array size.
The only way I can get non 0 is to put a value between the in the array declaration.

I am sure this is not a problem in Visual C++.

I am assuming this is a short coming in the compiler that Arduino is using.

So short of counting up how many array elements I actually have specified and then manually puting that number between the in the array declaration, is there some work around for this pest of a problem?

Color.cpp (9.49 KB)

Color.h (1.32 KB)

Common.h (2.06 KB)

Constants.h (119 Bytes)

CString.cpp (26.6 KB)

CString.h (9.63 KB)

Debug.cpp (19.5 KB)

Debug.h (4.76 KB)

RGBGardenLights.ino (888 Bytes)

boylesg: At least with Arduino sizeof(...) does not work if you declare an array like this:

const uint32_t m_arrayColorTable[]

Where you fill in the elements in the source file like so: { RGB(0, 0, 0), RGB(5, 5, 5), RGB(10, 10, 10), RGB(15, 15, 15), RGB(20, 20, 20), RGB(25, 25, 25), RGB(30, 30, 30), RGB(35, 35, 35), . }

Prove it. Post an MCVE that demonstrates the problem and doesn't involve downloading 9 files full of unrelated code.

At least with Arduino sizeof(...) does not work if you declare an array like this:

const uint32_t m_arrayColorTable[]

const uint32_t m_arrayColorTable[]

Does not compile so I look forward to seeing you use it in a program that provides the proof of what you say

I have no trouble with

void setup() {
 
  Serial.begin(115200);
  Serial.println("Hello!");
}


#define RGB(nRed, nGreen, nBlue) (uint32_t)(((uint32_t)nGreen << 16) | ((uint32_t)nRed <<  8) | (uint32_t)nBlue)

#define GET_GREEN(nColor) (nColor & 0x00FF0000) >> 16

#define GET_RED(nColor) (nColor & 0x0000FF00) >> 8

#define GET_BLUE(nColor) nColor & 0x000000FF

#define WHITE (uint32_t)(((uint32_t)255 << 16) | ((uint32_t)255 <<  8) | (uint32_t)255)

#define RED (uint32_t)(((uint32_t)0 << 16) | ((uint32_t)255 <<  8) | (uint32_t)0)

#define YELLOW (uint32_t)(((uint32_t)255 << 16) | ((uint32_t)255 <<  8) | (uint32_t)0)

#define GREEN (uint32_t)(((uint32_t)255 << 16) | ((uint32_t)0 <<  8) | (uint32_t)0)

#define CYAN (uint32_t)(((uint32_t)255 << 16) | ((uint32_t)0 <<  8) | (uint32_t)255)

#define BLUE (uint32_t)(((uint32_t)0 << 16) | ((uint32_t)0 <<  8) | (uint32_t)255)

#define MAGENTA (uint32_t)(((uint32_t)0 << 16) | ((uint32_t)255 <<  8) | (uint32_t)255)

#define BLACK (uint32_t)0

#define MAX_COLORS 205


const uint32_t m_arrayColorTable[] =
{
RGB(0, 0, 0),
 RGB(5, 5, 5),
 RGB(10, 10, 10),
 RGB(15, 15, 15),
 RGB(20, 20, 20),
 RGB(25, 25, 25),
 RGB(30, 30, 30),
 RGB(35, 35, 35),
 RGB(30, 30, 30),
 RGB(35, 35, 35)
};
void loop() {
  
 size_t m_nArraySize = sizeof(m_arrayColorTable) / 4;

 Serial.print(sizeof(m_arrayColorTable));
 Serial.print(" ");
 Serial.println(m_nArraySize);
 delay(1000);
}

What do you do that is different ?

Edit : I note you have not pre-pended the class name to the array filling declaration !!! Should it not be

const uint32_t CRainbow ::m_arrayColorTable[]

boylesg: I am assuming this is a short coming in the compiler that Arduino is using.

The Arduino IDE is using the gnu gcc compiler, so I'd be very surprised if something as basic as sizeof isn't working.

wildbill:
The Arduino IDE is using the gnu gcc compiler, so I’d be very surprised if something as basic as sizeof isn’t working.

I’m astounded by the frequency with which “the compiler” is blamed in posts on this forum. I consider myself a moderately-experienced coder, learned ‘C’ in college about 35 years ago. I think the number of times I’ve honestly been able to blame a “compiler error” as the problem (verses a screw-up in my code) can be counted on one hand – leaving out a few fingers. Even then, it was usually due to an incorrect option setting in the toolchain as opposed to a true error.

gfvalvo: I think the number of times I've honestly been able to blame a "compiler error" as the problem (verses a screw-up in my code) can be counted on one hand -- leaving out a few fingers.

A couple of weeks ago someone reported a problem with a 'for' loop. It was definitely a compiler problem but that is the first one I have seen in a long long time.

This problem here seems to be that the OP has a class definition and a global definition of the array with the same variable name. He only initialises/fills the global definition but takes the sizeof the class definition.

Never mind it was a stupid error.

Forgot the CRainbow:: in const uint32_t CRainbow::m_arrayColorTable[] =

There were two copies of the array - one was a global variable and the empty one was a class member.

boylesg: Never mind it was a stupid error.

Forgot the CRainbow:: in const uint32_t CRainbow::m_arrayColorTable[] =

There were two copies of the array - one was a global variable and the empty one was a class member.

:) As I pointed out !