Forums » Software Development »
Big memory leak
Added by ahmed Elmenshawy over 7 years ago
Hi all,
I have a series problem with memory on ompl138, that the program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file;
int *var1,*var2,*var3;
int *data=(int *)malloc(1*sizeof(int));
*data=5;
file=fopen("data.txt","w+");
var1=(int *)malloc(1*sizeof(int));
*var1=5;
if (fwrite(data, sizeof(int), 1, file)!=1)
{
printf("wrong file");
}
var2=(int *)malloc(1*sizeof(int)); //address very far in 0xc0000138
*var2=3;
fclose(file);
var3=(int *)malloc(1*sizeof(int)); //also be normal
*var3=7;;
return 0;
}
var1:0xc0000018
var2:0xc0000138
var3:0xc0000028
why is var2 allocated at very far memory where all this memory was gone?
Is this normal ? does fwrite use heap?
I am using ccsv5.5 simulator I upload with file please help.
regards,
elmenshawy
Replies (1)
RE: Big memory leak - Added by Dominic Giambo over 7 years ago
Hi Ahmed,
Interesting. I don't believe there is a guarantee by the C standard as far as placement of memory. Each implementation may vary in the mechanics as long as the end result is a block of memory of at least the size requested. Most modern malloc() implementations tend to have a minimum allocation size to reduce fragmentation and for alignment. Additionally there are markers in the heap for management uses which consume memory.
I did find a post where a user indicated that the CCS implementation of fread/fwrite do dynamically allocate some memory for internal use as you suspect (TI E2E Forum Post). Since fread and fwrite are doing buffered I/O, the allocations might be for the buffering.