For future reference I have 3D arrays below as sample snippet
//want to work on a 3D array on the heap
//first lest get a single then a 2D array working correctly on the heap
#define IMAGE_WIDTH 4
#define IMAGE_HEIGHT 3
#define CHANNELS 2
void experiment6()
{
int* array1D = NULL;
int* array2D = NULL;
int* array3D = NULL;
array1D = (int*)malloc(IMAGE_WIDTH * sizeof(int));
for (int i = 0; i < IMAGE_WIDTH; i++)
{
array1D[i] = i;
}
for (int i = 0; i < IMAGE_WIDTH; i++)
{
printf("[%d]=%d ",i, array1D[i]);
}
printf("\n");
array2D = (int*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT * sizeof(int));
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH;j++)
{
array2D[i * IMAGE_WIDTH + j ] = i * IMAGE_WIDTH + j;
}
}
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH; j++)
{
printf("[%d]=%d ", i * IMAGE_WIDTH + j, array2D[i * IMAGE_WIDTH + j]);
}
printf("\n");
}
array3D = (int*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT * CHANNELS * sizeof(int));
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH; j++)
{
for (int k = 0; k < CHANNELS; k++)
{
array3D[(i* IMAGE_WIDTH * CHANNELS) + (CHANNELS*j) + k] = (i * IMAGE_WIDTH * CHANNELS) + (CHANNELS * j) + k;
//printf("%d ", i * IMAGE_WIDTH * CHANNELS + CHANNELS*j + k);
}
}
}
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH; j++)
{
for (int k = 0; k < CHANNELS; k++)
{
// array2D[(i * IMAGE_WIDTH CHANNELS) + (CHANNELS * j) + k] = (i * IMAGE_WIDTH * CHANNELS) + (CHANNELS * j) + k;
printf("[%d] = %d ", i * IMAGE_WIDTH * CHANNELS + CHANNELS*j + k, array3D[(i * IMAGE_WIDTH * CHANNELS) + (CHANNELS * j) + k]);
}
}
printf("\n");
}
if (array1D != NULL)
{
free(array1D);
}
if (array2D != NULL)
{
free(array2D);
}
if (array3D != NULL)
{
free(array3D);
}
}
//want to work on a 3D array on the heap
//first lest get a single then a 2D array working correctly on the heap
#define IMAGE_WIDTH 4
#define IMAGE_HEIGHT 3
#define CHANNELS 2
void experiment6()
{
int* array1D = NULL;
int* array2D = NULL;
int* array3D = NULL;
array1D = (int*)malloc(IMAGE_WIDTH * sizeof(int));
for (int i = 0; i < IMAGE_WIDTH; i++)
{
array1D[i] = i;
}
for (int i = 0; i < IMAGE_WIDTH; i++)
{
printf("[%d]=%d ",i, array1D[i]);
}
printf("\n");
array2D = (int*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT * sizeof(int));
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH;j++)
{
array2D[i * IMAGE_WIDTH + j ] = i * IMAGE_WIDTH + j;
}
}
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH; j++)
{
printf("[%d]=%d ", i * IMAGE_WIDTH + j, array2D[i * IMAGE_WIDTH + j]);
}
printf("\n");
}
array3D = (int*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT * CHANNELS * sizeof(int));
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH; j++)
{
for (int k = 0; k < CHANNELS; k++)
{
array3D[(i* IMAGE_WIDTH * CHANNELS) + (CHANNELS*j) + k] = (i * IMAGE_WIDTH * CHANNELS) + (CHANNELS * j) + k;
//printf("%d ", i * IMAGE_WIDTH * CHANNELS + CHANNELS*j + k);
}
}
}
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
for (int j = 0; j < IMAGE_WIDTH; j++)
{
for (int k = 0; k < CHANNELS; k++)
{
// array2D[(i * IMAGE_WIDTH CHANNELS) + (CHANNELS * j) + k] = (i * IMAGE_WIDTH * CHANNELS) + (CHANNELS * j) + k;
printf("[%d] = %d ", i * IMAGE_WIDTH * CHANNELS + CHANNELS*j + k, array3D[(i * IMAGE_WIDTH * CHANNELS) + (CHANNELS * j) + k]);
}
}
printf("\n");
}
if (array1D != NULL)
{
free(array1D);
}
if (array2D != NULL)
{
free(array2D);
}
if (array3D != NULL)
{
free(array3D);
}
}
No comments:
Post a Comment