Big memory leak

Added by ahmed Elmenshawy almost 4 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));
var1=(int *)malloc(1*sizeof(int));
if (fwrite(data, sizeof(int), 1, file)!=1) {
printf("wrong file");

var2=(int *)malloc(1*sizeof(int)); //address very far in 0xc0000138
var3=(int *)malloc(1*sizeof(int)); //also be normal
return 0;




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.



test.rar (60 KB)

Replies (1)

RE: Big memory leak - Added by Dominic Giambo almost 4 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.

    Add picture from clipboard (Maximum size: 2 GB)