-2

I want to store data up to 13 Kb. My application will give a chunks of data with some delay(~10sec), which i need to store in a particular variable. It is bad to take static array with 13000(uint8 buffer[13000]). Here my code,

uint8 buffer[13000];

void read_byte(uint16 Address)
{
    uint8 data;
    uint8 append_byte;

    send_cmd(Address);   //send command for read from address
    receive_byte(&data); // Read the data and store in data var
    check_append_byte(&append_byte); // check the byte from where need to append
    buffer[append_byte] = data; // store in buffer 
}

void file data()
{
    uint8 i;

    if(flag == 1)
    {
        for(i=0; i<8; i++)
        {
            read_byte(Address);
        }
    }
}

So i want to increase/decrease the size of buffer in run time. Can any body guide me.

user3844884
  • 41
  • 1
  • 2
  • 6
  • 1
    You could declare buffer as a `uint8 *`, then allocate with `malloc`, so your buffer is heap allocated. When the time comes to resize, you can use `realloc`. Storing a buffer on the stack is bad because it will use up your stack and if you write outside its boundaries you will corrupt your stack. – Joe Aug 05 '14 at 09:52
  • Please refer http://stackoverflow.com/questions/2937409/resizing-an-array-with-c – Pritam Pol Aug 05 '14 at 10:38

1 Answers1

1

You need to use linked list format instead of array to save your data and allocate memory in run time.

You can find a nice tutorial for linked list in C here, http://www.learn-c.org/en/Linked_lists