Dynamic array size, but I dont know the size until too late

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I am sending arrays, but of different sizes, I want the RX to handle what to do in each case.
i.e

switch(arraySize)
{
case 5:
//do something because array size is 5;
return;
case 9:
//do something different because array size is 9;
return;
default:
//print something
}

(just a bit of pseudo) then what would be the best way to go about this. Currently I am doing this:

        rx_block(recbuf, 256); 

    	char* subchar_array = strtok(recbuf, ":"); //splits up the received data based on :
          
    		while (subchar_array != NULL) {
			uart0_printf("% s:\n", subchar_array);
tempfloat = atof(subchar_array);// convert char to float
			++count;
			subchar_array = strtok(NULL, ":");
			}

to try and find the array size out (I am using : as delimiter), but I then cant process it again, so my plan is to store the data in a float array as it comes in. I can then process based on the size of the float array, but Im not sure how to dynamically allocate the array to the correct size during the loop?

EDIT:
something like this?:

float * processedBuf;  //buffer to hold message received 

    		while (subchar_array != NULL) {
			uart0_printf("% s:\n", subchar_array);
			tempfloat = atof(subchar_array);// convert char to float
			++count;
			processedBuf = (float *)malloc(count * sizeof(float));
			processedBuf[count-1] = tempfloat;
			subchar_array = strtok(NULL, ":");
			}

???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code would allocate a different array for every value that comes in (irrevocably throwing away the previous one in the process). Just create an array that is the largest possible array, then fill in only the elements needed in the loop.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I used realloc in the end. Then free() once Im done with it. Any issues with that method?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This function only uses the 256 as the maximum size to receive.

        rx_block(recbuf, 256);

In fact the tx_block() function sends a special 'size' packet before it send the actual contents of the block.
So the rx_block() knows what to expect.

Since you intend to send an array of 6 floats, you know that this is a fixed 24 byte block.
If you send a human readable version, then the ascii representation may take anything from 20 bytes to 120 bytes.

In other words, you always know the worst case for memory use. It is generally simpler to just reserve a fixed size array or buffer.

Using malloc(), realloc() or free() is not worth the bother. You seldom have enough SRAM for long linked lists.
e.g. float array[100] takes 400 bytes.
e.g. struct list { struct list *next; float val; } wastes 2 bytes for the pointer as well as at least 4 bytes for the malloc() housekeeping for each list entry.

David.