Sizeof Byte Array ( Solved)

This is a strip down of a larger program but shows the problem. I pass an 8 byte array through two subroutines but the sizeof in the last routine reads only 4 bytes, not 8
Can anyone explain please

byte Set_Run[]  = {0x01,0x05,0x08,0x14,0xFF,0x00,0xCE,0x5E}; //1,5,8,20,255,0,206,94

/*-----------------------------------------------------------------
'Send an Array To the Controller
'-----------------------------------------------------------------
*/
void Send_To_Controller(byte Buf[])
  {
  Serial.println("Sending to controller");                
	SendRTX2(Buf);
  }

/*-----------------------------------------------------------------
'Sends to RTX2
'-----------------------------------------------------------------
*/
void SendRTX2(byte Buffer[])
  {
  Serial.println("Send RTX2");
  for (int i=0;i<8;i++)
    Serial.println(Buffer[i], HEX);
  Serial.print("Buffer Size    :");
  Serial.println(sizeof(Buffer));
  }

void setup() 
  {
  Serial.begin(115200);
  Serial.print("Size of Set_Run  ");
  Serial.println(sizeof(Set_Run));
	Send_To_Controller(Set_Run);  
  }

void loop() 
  {

  }

Here is the printout
Size of Set_Run 8
Sending to controller
Send RTX2
1
5
8
14
FF
0
CE
5E
Buffer Size :4

When you pass array as reference, you lost the info about its size. The size, that you see in the print - is the size of pointer, which always 4byte, regardless array size.

The common workaround for the issue is passing the size of array as separate integer parameter to the function.

Read about c++ array decay to pointer

Here is a first Google hit on the topic

If you have access to the Standard Template Library, you may want to have a look at std::span. This container can be used to pass both the content as well as the size of an array to a function.

If this is not the case, you may want to have a look at the array-helpers library, in which a limited version of std::span is implemented.

Example:

#include <arrayHelpers.h>

void test(Span<byte> arr) {
  Serial.println(arr.size());
  Serial.println(arr[6]);
}

void setup() {
  Serial.begin(9600);

  byte arr[] {1, 2, 3, 4, 5, 6, 7, 8};
  test(arr);
}

void loop() {}

Output:

8
7
byte Set_Run[]  = {0x01, 0x05, 0x08, 0x14, 0xFF, 0x00, 0xCE, 0x5E}; //1,5,8,20,255,0,206,94

/*-----------------------------------------------------------------
  'Send an Array To the Controller
  '-----------------------------------------------------------------
*/
void Send_To_Controller()
{
  Serial.println("Sending to controller");
  SendRTX2();
}

/*-----------------------------------------------------------------
  'Sends to RTX2
  '-----------------------------------------------------------------
*/
void SendRTX2()
{
  Serial.println("Send RTX2");
  for (int i = 0; i < sizeof(Set_Run); i++)
    Serial.println(Set_Run[i], HEX);
  Serial.print("Buffer Size    :");
  Serial.println(sizeof(Set_Run));
}

void setup()
{
  Serial.begin(115200);
  Serial.print("Size of Set_Run  ");
  Serial.println(sizeof(Set_Run));
  Send_To_Controller();
}

void loop()
{

}

array is global. any function can access it. no need to pass pointer and size.

is there a reason why you don't want to pass in the size??

byte Set_Run[]  = {0x01,0x05,0x08,0x14,0xFF,0x00,0xCE,0x5E}; //1,5,8,20,255,0,206,94

/*-----------------------------------------------------------------
'Send an Array To the Controller
'-----------------------------------------------------------------
*/
void Send_To_Controller(byte Buf[], int len)
  {
  Serial.println("Sending to controller");                
	SendRTX2(Buf,len);
  }

/*-----------------------------------------------------------------
'Sends to RTX2
'-----------------------------------------------------------------
*/
void SendRTX2(byte Buffer[], int len)
  {
  Serial.println("Send RTX2");
  for (int i=0;i<len;i++)
    Serial.println(Buffer[i], HEX);
  Serial.print("Buffer Size    :");
  Serial.println(len);
  }

void setup() 
  {
  Serial.begin(115200);
  Serial.print("Size of Set_Run  ");
  Serial.println(sizeof(Set_Run));
	Send_To_Controller(Set_Run,sizeof(Set_Run));  
  }

void loop() 
  {

  }

good luck.. ~q

Thanks... Learnt something today from you guys

Also have a look at the full section, it has some nice examples.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.