I have this code. What it should do is to allocate a pointer array to genSize img structs. For each of the img structs I'm allocating it's inner pointer array with sizes of imgWidth, imgHeight and byteDepth. So practically I have genSize of img structs, each one of them has imgWidth * imgHeight * byteDepth byte dynamic array. In the first loop I'm allocating space for all of the array fields, in the second one I'm trying to deallocate them the same way, but somehow I'm getting valgrind error:
definitely lost: 282,000 bytes in 94,000 blocks
That size is equal to the total number of pixels of all the images (94000 * 3 (byteDepth)).
I have tried to comment out the the third loop, that is responsible for allocating and freeing the memory for the 3 byte array, both for malloc and free and I was getting this error no more, but obviously, I need to malloc it, so I'll be able to access it and free it later on.
I have followed the same instructions that can be found on the internet, many forum pages, but still I can't see where their code differs from mine and why are my arrays not freeing up.
Minimal working example:
#define imgWidth 50
#define imgHeight 50
#define genSize 40
#define byteDepth 3
struct img {
uint8_t ***bitmap;
uint32_t var;
} img;
int main()
{
struct img *images = malloc(genSize*sizeof(img));
for(int i = 0;i
EDIT: Obviously there is a typo in the second loop, there should be imgHeight instead of byteDepth. Problem solved