Let's assume that I have a struct with a void ** member. This member serves as an array of pointers to data channels. The data type is irrelevant. Below is an example of how I'd like to allocate memory for this 2D array and then associate channel pointers with their memory.
#define N_CHANNELS (/*Number of data channels*/)
#define N_SAMPLES (/*Number of data samples per channel*/)
typedef /*Some data type*/ DATA_TYPE;
void **ppdata; /*Conceptual struct member*/
/*Allocate memory for array of data channels */
ppdata = (void **) malloc(sizeof(DATA_TYPE *) * N_CHANNELS);
ppdata[0] = malloc(sizeof(DATA_TYPE) * N_CHANNELS * N_SAMPLES);
/*Cast for pointer arithmetic and data access*/
DATA_TYPE **ptr = (DATA_TYPE **) ppdata;
/*Associate channels with their memory*/
int alloc_index;
for (alloc_index = 0; alloc_index < N_CHANNELS; alloc_index++)
{
ptr[alloc_index] = (*ptr + alloc_index * N_SAMPLES);
}
So, the question arises: Is this dereference and allocation behaving as I have assumed it is?
ppdata[0] = malloc(sizeof(DATA_TYPE) * N_CHANNELS * N_SAMPLES);
i.e., is this allocation compatible with the manner in which I later access the memory?
DATA_TYPE **ptr = (DATA_TYPE **) ppdata;
...
ptr[alloc_index] = (*ptr + alloc_index * N_SAMPLES);
typedef DATA_TYPE;istypedef int DATA_TYPE;This is determined at compile time. - BLUEPIXYvoid *is the "generic" pointer type in C, it turns out thatvoid **can not portably be used as a generic pointer-to-pointer. So your lineDATA_TYPE **ptr = (DATA_TYPE **)ppdatais suspect. See c-faq.com/ptrs/genericpp.html - Steve Summit