Re: Memory management and allocation

From: Alex Fraser (me_at_privacy.net)
Date: 02/03/05


Date: Thu, 3 Feb 2005 07:44:30 -0000


"Dan Nilsen" <dan.nilsen@gmail.com> wrote in message
news:50a67599.0502022240.250dc7d7@posting.google.com...
> I'm writing a small piece of software that basically runs on an
> embedded system with a Power-PC cpu. This runs on a stripped down
> version of Linux - Busybox.
>
> As I'm writing a piece of code that basically acts as a server and
> that will be running for weeks or months and probably even longer,
> memory management is a topic that is quite crucial.
>
> Not being a very experienced C programmer, and with next to no help in
> the company I currently work in I turn to this forum.
>
> In my program(s), I do have several functions and files as one would
> expect.
> One of my questions is then: Is it strictly necessary to allocate
> memory as in:
> BYTE *frame = (BYTE*)malloc(sizeof(BYTE * FRAMELEN); and later to free
> this and set the pointer to NULL ? Or can I just allocate the variable
> by
> BYTE frame[FRAMELEN]; and assume the memory is freed on a function
> return?

Personally, I avoid malloc()/free() where practical in general. It is
typically practical if either the allocation size is small and constant, and
the memory is required only for the lifetime of a function, or the
allocation size is constant and required for the lifetime of the program.
The former seems to apply in your case.

Using malloc()/free() is usually slower and can result in heap
fragmentation, both of which can be quite important in embedded systems.

If using malloc()/free() in C, it is not necessary to cast the return value
of malloc() (and for reasons that are mentioned here from time to time, I
recommend not casting). Nor is it usually necessary to set the pointer to
NULL after calling free(); where it's not necessary, this is typically done
to guard against further (erroneous) use of the freed memory.

> I'm pretty sure dynamic memory allocation is quite critical in my main
> program(the server), in order to avoid any memory leakages.
>
> And as a final question:
> If i have code like this:
> BYTE *frame = (BYTE*)malloc(sizeof(BYTE * FRAMELEN);
> ..assign some values to frame...
>
> and then call
> free(frame);
> frame = NULL;
>
> why do I sometimes get a segmentation fault?

Common causes of problems when using malloc() and free() are:
- forgetting to #include <stdlib.h>,
- not checking the return value of malloc(), which may be NULL to indicate
  failure (problem occurs when you try to dereference the pointer),
- writing outside allocated memory, often somewhere else in the program
  (problem most often occurs when calling free(), but can occur at other
  times),
- calling free() twice with the same pointer value (problem occurs on the
  second call), and
- passing free() a pointer value other than one returned by malloc(),
  calloc() or realloc() (including a corrupted pointer value).

Alex



Relevant Pages

  • Re: Is There Any Reason to Even Use VC++ Anymore?
    ... If, for another reason, the calling function needs to allocate memory, ... It does this by taking a pointer to a ball object ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... I kept on getting error messages. ... You don't allocate enough memory here. ... Again conversions between pointer and integer types. ...
    (comp.lang.c)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)
  • Memory management and allocation
    ... As I'm writing a piece of code that basically acts as a server and ... memory management is a topic that is quite crucial. ... One of my questions is then: Is it strictly necessary to allocate ... ..assign some values to frame... ...
    (comp.lang.c)