Hi all,
I have a text file (tab delimited). This file contains a set of records. Each line is one record. Imagine that column headers have the following types.
(char*) (uint8_t) (uint32_t) (uint16_t) (uint16_t)
There are
n_cols number of columns and there are certain number of rows, let's say
n_rows.
To allocate memory for such an 2D array, I have used the following function, where
numByteInfo is an array which stores the size info (in bytes) for each column. This array has
n_cols entries.
numByteInfo[i] contains the size for
ith column.
Code:
void** __allocate2D(uint8_t nrows, uint8_t ncolumns, int numByteInfo[])
{
void** header;
header = (void**) malloc(ncolumns * sizeof(void *));
printf("nrows %d, ncolumns %d\n", nrows, ncolumns);
if (!header)
{
printf("\n**TOP LEVEL ERROR****\n");
return NULL;
}
for (uint8_t i=0; i<ncolumns; i++) {
printf("numByteInfo[%d]: %d ", i, numByteInfo[i]);
header[i] = (void*) malloc(nrows * numByteInfo[i]);
if (!header[i]) {
printf("\n***ERROR****\n");
return NULL;
}
}
return header;
}
And for deallocation, I use:
Code:
void __deallocate2D(void** header, uint8_t cols) {
for (uint8_t i=0; i<cols; i++) {
free(header[i]);
}
free(header);
}
Following this, I am allocating an array in the following function.
Code:
signed char some_function(void*** MY_ARRAY_RET, int* num_rows) {
void** MY_ARRAY;
int n_rows;
// The first column is allocated 20 bytes for each of its element
int numByteInfo[10] = {20, 1, 4, 2, 2, 1, 1, 4, 8, 1};
// assuming I have a way to find the n_rows
MY_ARRAY = __allocate2D(n_rows, n_cols, numByteInfo);
// storing some values
char singleMessage[250];
char val_0[20];
uint8_t val_1 = 0;
uint32_t val_2 = 0;
uint16_t val_3 = 0;
uint16_t val_4= 0;
uint8_t i = 0;
while ( fgets(singleMessage, sizeof(singleMessage), file) )
{
sscanf(........., val_0, &val_1, &val_2, &val_3, &val4);
*(char**)(MY_ARRAY[0] + i) = val_0;
*(uint8_t*)(MY_ARRAY[1] + i) = val_1;
*(uint32_t*)(MY_ARRAY[2] + i) =val_2;
*(uint16_t*)(MY_ARRAY[3] + i) = val_3;
*(uint16_t*)(MY_ARRAY[4] + i) = val_4;
i++;
}
*MY_ARRAY_RET = MY_ARRAY;
*num_rows = n_rows;
}
While inside the function
some_function, I am able to print the contents of each column of
MY_ARRAY, and I can see that corresponding values are stored. However, I am unable to retrieve the values in
main() function.
Code:
int main(int argc, char* argv[]) {
void **EXAMPLE_ARRAY = 0;
int r = 0;
ret = some_function(&EXAMPLE_ARRAY, &r);
for (uint8_t i = 0; i < r; i++) {
printf("Hello: ""%s" " "
"%" PRIu8 " "
"%" PRIu32 " "
"%" PRIu16 " "
"%" PRIu16 " "
"\n",
*(char**)(EXAMPLE_ARRAY[0]+ i),
*(uint8_t*)(EXAMPLE_ARRAY[1]+ i),
*(uint32_t*)(EXAMPLE_ARRAY[2]+ i),
*(uint16_t*)(EXAMPLE_ARRAY[3]+ i),
*(uint16_t*)(EXAMPLE_ARRAY[4]+ i)
);
}
__deallocate2D(EXAMPLE_ARRAY, 5);
return 0;
}
The program compiles, I can print the value while in the
some_function, but, cannot do that in
main function.
What can be the problem here.
thanks,
Zahid