1

I am working on C++ based project where i am getting one error for below statement:

Code:

typedef char pb_static_assertion_UINT32_T_WRONG_SIZE3507__COUNTER__ 
[ ( sizeof ( uint32_t ) == 4 ) ? 1 : - 1 ] ;   // The line giving error

Error:

"C:\Users\tkumar\Documents\LDRA\LDRAunit_C_CPP_9.7.1\trial\inszt_algctivationgateway.cpp", 
line 4330: error #95: the size of an array must be greater than zero"

Reference: uint32_t is defined using:

typedef unsigned long uint32_t;

I have doubt for unsigned long size, anybody here to explain to tell me the reason behind this error ?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
tkumar
  • 51
  • 1
  • 1
  • 4

2 Answers2

2

The smallest addressable unit (i.e. "char") on the C2000 architecture is 16 bits.

It's a bit unusual these days, but it's allowed. C allows for all kinds of fun CPU architectures past and future does not actually require a "char" to be 8 bits. "Char" could be 9, 10, 11, or even 16 bits like on the C2000.

For the C2000 C compiler, sizeof(char) = 1 and char is 16 bits. Now you can see why, to be consistent, sizeof(uint32_t) = 2

If it helps, you can think of the C2000 as having 16 bit bytes. So a 32 bit value is two of these big bytes. It does making using 3rd party C libraries on C2000 a little tricky because most people assume uint8_t is available.

I've seen some libraries typedef a uint8_t that is actually a uint16_t but this just hides the problem. PLEASE avoid that. Those fake "uint8_t" can have values over 255 which is bad news for code that assumes it will wrap around!

There's a great write up (not mine) here: https://faehnri.ch/byte-not-8-bits/

Chris
  • 21
  • 2
  • Yes, the proper assertion is `32 == CHAR_BIT * sizeof (uint32_t)` which will do 8*4 on most architectures and 16*2 on C2000 and in all cases give 32. – Ben Voigt Aug 25 '23 at 17:08
1

1) Code-Composer compiler outputs object in formats called COFF or EABI. When COFF was selected, unsigned long was 40-bits on for some of the microprocessors at least. When EABI was selected, it was 32-bits. 2) For some of the TI microprocessors, such as the C2000 series, the smallest addressable word was 16 bits, and sizeof would then output 2 instead of 4 for a 32-bit type. 3) One or the other of these is probably causing your sizeof statement to be returning something other than 4, causing ?: to select -1. So the array size is less than 0. 4) Don't think you really want an array after a typedef.

mgspan
  • 21
  • 2