How to Check Memory Leak of Array

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Check Memory Leak of Array

1,337 Views
athmesh_n
Contributor IV

Hi,

I'm using LPC1754 with 128kB flash and RAM of 32kB. I'm using MCUxpresso IDE.

 I've used only 37% of RAM while compiling the code. I'm using embedded c language.

I've come to know that there's a chance of memoryleak, i.e. values of memory changes abnormally without our consent. Some array gets filled with values of other Array or value.

for eg: I'm using character array of size 1362 and another of size 260  declared as following.

char Buffer[1362], which is declared globally.

char response[260], which is also declared globally.

sometimes response, which is cleared by using memset function, gets filled with some data present in Buffer. I checked for shortage of array size. which is not seen.

I found the same issue with some other variables  too, whose value gets changed without our consent, which is causing serious issue in logic.

Some points that I'm concerned about are:

1. heap and stacksize How to modify the size so that the code works without any issue in memory leak.

2. Array size: Which is  not found to give any impact at present. But, would like to know steps to check if the array size is getting filled up.

Thanks and Regards,
Athmesh Nandakumar   

0 Kudos
1 Reply

1,152 Views
johnadriaan
Contributor III

What you are describing is called "Accessing an array out of bounds", rather than a "memory leak".

A memory leak is when your code does a C malloc() or a C++ new, but then doesn't do a free() or delete when it is finished with the memory. It is like the memory has "leaked" out of the computer, never to be recovered! Instead, you're describing one array being corrupted by data from another array.

Because your two arrays are declared globally, rather than on the stack or the heap, the size of the stack or the heap is not the cause of your problem. The compiler will reserve enough space for those two arrays, and if it runs out of memory it will give a compiler error.

Instead, it is how your code uses the arrays (especially Buffer) that is the problem. You don't give a example code, but for instance, if you did the following:

char Buffer[1362];
...
if (read(file, Buffer, 1536)) {
    Process(Buffer);
} // if

That number 1536 above should be sizeof(Buffer), which lets the compiler fill in the number for how many bytes to read into Buffer.