unknown array size

Hi everyone! Quick question. Can I make an array of an unknown size? I seem to be having some issues making a dynamic array that changes based off the amount of data I put in it.

Lets say before setup I have this:

payload;

Then I set a variable in my program to say the payload will be payload[9]. Then after it loops, the array may be payload[15]. Sure I can add the max number of places within the array but if I dont fill it all I will have "white space" or zero's at the end of the array.

Thanks!

Just create an array of the maximum expected size and store the size you want to use in a variable. Example:

byte payload[64];
payloadSize = 9;
//...
payloadSize = 15;

brolly759:
but if I dont fill it all I will have "white space" or zero's at the end of the array.

So what? Why can't you have that?

Dynamic memory allocation on a micro controller is something probably best left alone unless you really really know what you're doing. If you have to ask this question then you aren't there.

If you're using arrays to store C character strings, they are NUL terminated. So that's the indication where the end is.

If you store numeric information, you can use an additional variable with the array that keeps track how many elements are filled.

Its Delta the memory police! :slight_smile: Well if we are being technical, I should be using AVR studio or some type of ICE to program and view memory/code correctly instead of the Arduino platform.

To answer your question, why can't I have that? Well because the longer the payload, the longer it takes to transmit the data over a wireless connection. Its wasted energy (using a coin cell). There is more compiling code on the other side I need to do at the "hub" to omit the ending zero's rather than just sending the correct data to begin with.

Then do like I said. What is your longest payload?

@ sterretje its all numerical values. Cant add anything "extra". I have an algarthem that uses pairs. So [0] is the Identifier, [1] is the dataset. [2] Identifier, [3] dataset, so on and so forth.

@arg, I think I know what you are talking about. I was doing it backwards by trying to start with limiting the array size and then loading the info in, rather than just counting the amounts of data I am adding to each array position and only calling the total array fields that I put into it, and NOT the maximum allowable space. I will try that and see what happens!

I know we all like codes, so I will post some. My hardware is at work but at least I compiled it without errors.

In the Transmit packet I am changing this:

ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));

to this:

ZBTxRequest zbTx = ZBTxRequest(addr64, payload, arraySize);

And here is what my payload looks like:

void compilePayload() {
  
  memset(payload, 0, sizeof(payload));
  
byte arrayIndex = 0;

  switch (sensorType){

  case 1:
  //Sensor Type    
  payload[arrayIndex++] = 1;
  payload[arrayIndex++] = sensorType;
  //Use Case
  payload[arrayIndex++] = 2;
  payload[arrayIndex++] = useCase;
  // Hardware Version
  payload[arrayIndex++] = 3;
  payload[arrayIndex++] = hardwareVer;
  //Firmware Version
  payload[arrayIndex++] = 4;
  payload[arrayIndex++] = firmwareVer;
 
  // Reed State
  payload[arrayIndex++] = 103;
  payload[arrayIndex++] = reedState;
  
if(accelDisable == true){
  
    payload[arrayIndex++] = 50;
    payload[arrayIndex++] = accelDisableCode;
}
else{
  
 // X Axis
  payload[arrayIndex++] = 100;
  payload[arrayIndex++] = xAxis2;
  // Y Axis
  payload[arrayIndex++] = 101;
  payload[arrayIndex++] = yAxis2;
  // Z Axis
  payload[arrayIndex++] = 102;
  payload[arrayIndex++] = zAxis2;
  
}

if(tempDisable == true){
  
  payload[arrayIndex++] = 51;
  payload[arrayIndex++] = tempDisableCode;
  
}
else{
  
  // Temp Reading F (rounded)
  payload[arrayIndex++] = 104;
  payload[arrayIndex++] = tempValue;
  // Humidity Reading %
  payload[arrayIndex++] = 105;
  payload[arrayIndex++] = humValue;
}

if(lightDisable == true){
  
  payload[arrayIndex++] = 52;
  payload[arrayIndex++] = lightDisableCode;
 }
 else{
  
  // Ambient Light Reading
  payload[arrayIndex++] = 106;
  payload[arrayIndex++] = ambientLight;
  // Proximity Read using IR TOF
  payload[arrayIndex++] = 107;
  payload[arrayIndex++] = lightProximity;
 }
 
 // Presence bit
  payload[arrayIndex++] = 5;
  payload[arrayIndex++] = presence;
// Heartbeat bit
  payload[arrayIndex++] = 6;
  payload[arrayIndex++] = heartBeat; 

arraySize = arrayIndex;

  break;


  case 2:   

  break;

  case 3:    
 
  break;

  case 4:

  break;

  case 5:

  break;    
  }  
}

We shall see tomorrow if this fixes it! :slight_smile: Thanks!

brolly759:

@ sterretje its all numerical values. Cant add anything "extra". I have an algarthem that uses pairs. So [0] is the Identifier, [1] is the dataset. [2] Identifier, [3] dataset, so on and so forth.

OK, so this relates to communication and not to arrays :wink: You can't add anything extra because you did not tell us what you were doing :wink:

arraySize = arrayIndex;

So you did add something extra :smiley:

Yup, added it based off the comments here :slight_smile:

brolly759:
Its Delta the memory police! :slight_smile: Well if we are being technical, I should be using AVR studio or some type of ICE to program and view memory/code correctly instead of the Arduino platform.

To answer your question, why can't I have that? Well because the longer the payload, the longer it takes to transmit the data over a wireless connection. Its wasted energy (using a coin cell). There is more compiling code on the other side I need to do at the "hub" to omit the ending zero's rather than just sending the correct data to begin with.

So just send the part with useful data. There's no law that says you have to send the whole array.

And your police comment is way way off. I won't stop you. Just giving friendly advice. If you want to play with fire go right ahead. You're only going to burn yourself.