Memory Management

Memory Manangement in Small Systems

Memory Management demo code


Motivation & Purpose

Reference: IBM Developer Works paper.
Here is an error from running Vivado on a virtual machine.

Memory leaks and C Pointers

These are some of the prominent pitfalls while working with memory at low level
  1. Uninitialized memory
    char *p = malloc (10);
    memset(p,”\0”,10); // initialize memory
  2. Memory overwrite
    char *name = (char *) malloc(11); // Assign some value to name memcpy ( p,name,11);
  3. Memory overead
    char *ptr = (char *)malloc(10); char name[20] ;
    memcpy ( name,ptr,20);
  4. Memory leak: reassignment
    char *memoryArea = malloc(10);
    char *newArea = malloc(10);
    memoryArea = newArea;
  5. Memory leak: freeing the parent area first
    free(memoryArea); //wrong
    //correct method is given below
    free( memoryArea->newArea); free(memoryArea);
  6. Improper handling of return functions
    
    char *func ( ) { 
    return malloc(20);
     // make sure to memset this location to ‘\0’… 
    } 
    
    void callingFunc ( ) { 
    func ( ); // Problem lies here 
    }
    

Summary

We discussed some things to pay attention to when designing systems at low level and deal with dynamic memory management. This is especially critical for small embedded systems with limited memory. Memory leaks are serious problem resulting in dramatic system slow down at runtime. Memory mismanegements are favorite exploits for hackers.