This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Catching memory leaks

I have some code running on an 8051 based processor that is quite large and complex and it also uses a few arrays. To make sure that I can avoid mistakes accessing memory location that do not exist, I'd like to know if there are anyway to get the C51 Compiler to catch this type of mistakes.

BYTE xdata myArray[8]
BYTE i;

for (i=0;i<10;i++)
    myArray[i]=i;

The compiler has no problem with that and doesn't report memory leaks. Are there any ways to make the compiler to generate an error or even a warning?

Parents
  • As already noted, this is not what the term "memory leak" usually means: a "memory leak" is when you use dynamic allocation, but don't fully release the memory when you're finished with it.
    The risk of such problems is one reason why dynamic allocation is usually avoided in embedded systems...

    What you're talking about requires runtime array bounds checking. That means adding code at every array reference to validate the index before using it.
    In 'C', this would probably be pointless without also having some sort of runtime check on all pointer accesses.

    The overhead in terms of both execution time and code size is likely to be impractical on an 8051-based system

    "I'd like to know if there are anyway to get the C51 Compiler to catch this type of mistakes"

    This kind of error is not detectable at compile time, so no compiler can catch it.

    However, things like Lint can check for constructs that are susceptible to the risk - and warn you about that.

    BYTE xdata myArray[8]
    BYTE i;
    
    for (i=0;i<10;i++)
        myArray[i]=i;
    

    This is the classic problem with Magic Numbers!

    Don't use a Magic Number for your array size or your loop test:

    #define MY_ARRAY_SIZE 8
    BYTE xdata myArray[ MY_ARRAY_SIZE ]
    BYTE i;
    
    for ( i=0; i < MY_ARRAY_SIZE; i++ )
    {
        myArray[i]=i;
    }
    

    Note also the use of braces, even though they are not strictly necessary in this case.

    Try doing some searches on "defensive programming".

    You might also have a look at the MISRA rules...

Reply
  • As already noted, this is not what the term "memory leak" usually means: a "memory leak" is when you use dynamic allocation, but don't fully release the memory when you're finished with it.
    The risk of such problems is one reason why dynamic allocation is usually avoided in embedded systems...

    What you're talking about requires runtime array bounds checking. That means adding code at every array reference to validate the index before using it.
    In 'C', this would probably be pointless without also having some sort of runtime check on all pointer accesses.

    The overhead in terms of both execution time and code size is likely to be impractical on an 8051-based system

    "I'd like to know if there are anyway to get the C51 Compiler to catch this type of mistakes"

    This kind of error is not detectable at compile time, so no compiler can catch it.

    However, things like Lint can check for constructs that are susceptible to the risk - and warn you about that.

    BYTE xdata myArray[8]
    BYTE i;
    
    for (i=0;i<10;i++)
        myArray[i]=i;
    

    This is the classic problem with Magic Numbers!

    Don't use a Magic Number for your array size or your loop test:

    #define MY_ARRAY_SIZE 8
    BYTE xdata myArray[ MY_ARRAY_SIZE ]
    BYTE i;
    
    for ( i=0; i < MY_ARRAY_SIZE; i++ )
    {
        myArray[i]=i;
    }
    

    Note also the use of braces, even though they are not strictly necessary in this case.

    Try doing some searches on "defensive programming".

    You might also have a look at the MISRA rules...

Children