I’ve this code:
#include <stdio.h>
#include <stdlib.h>
#define OUT
void getDataFromServer(OUT int** array, OUT int* size)
{
int tmpArr[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
*size = sizeof tmpArr / sizeof(int);
*array = tmpArr;
}
int main(void)
{
int size = 0;
int* dataFromServer;
getDataFromServer(&dataFromServer, &size);
int x;
for (x=0; x < size; x++)
printf("%d ", dataFromServer[x]);
printf("\n\n");
return 0;
}
The for loop inside the main prints garbage data.. Is this because of the OUT int** array parameter access data local to a function?
Thanks.
When
getDataFromServerexits, all local variables are popped off of the stack. The pointer you’re using is now pointing at an area of memory that has more or less been wiped out. Either usemallocand copy the data into newly allocated memory (on the heap), or maketmpArrinto astaticvariable.