2007-09-18 01:09:00     Malloc returns NULL inspite of having enough memory

Document created by Aaronwu Employee on Aug 7, 2013
Version 1Show Document
  • View in full screen mode

2007-09-18 01:09:00     Malloc returns NULL inspite of having enough memory

Prashanth Dwarakanath (INDIA)

Message: 44234    Hi All,

 

I am trying to run an application on uClinux running on bfin 561.  The application requires me to allocate 96 bytes of memory dynamically( of type struct which is 96 bytes). I used malloc in the following manner:-

 

 

struct GR_WINDOW  *gr_window;   (Structure already defined)

struct GR_WINDOW  gr_window_hold;

 

gr_window = (struct GR_WINDOW*)malloc(sizeof(gr_window_hold));

 

malloc failed in this case. I checked the memory available by running free command in shell and found to have adequate memory left.

 

Tried including stdlib.h but even then malloc returned null.

 

 

After that I wrote a simple program to check the functionality of malloc in the following manner(a chunk is presented below):

 

#include<stdio.h>

#include<stdlib.h>

 

#define SIZEOFINT         4

#define SIZEOFCHAR    1

 

main(int ac, char **av)

{

    int *patchu;

    patchu = (int *)malloc(SIZEOFINT);

      

        if(patchu == NULL)

        {

            printf("\nMalloc failed\n");

        }

          

        else

        {

            free(patchu);

            printf("\nAllocated and freed patchu\n");

        }

 

 

    char *C;

    C = (char *)malloc(SIZEOFCHAR);

 

 

    if(C == NULL)

    {

        printf("\nChar malloc failed\n");

 

    }

    else

    {

        free(C);

        printf("Allocated and freed char malloc\n"); 

    }

 

 

}

 

 

When I executed this program on the target board after cross compiling using required uClinux-gcc complier, Malloc still returned NULL in both the allocations. But when I run the same program on my PC by ordinary gcc compliation, it runs perfectly fine.

 

 

Can somebody pls tell me if there are any other dependency for malloc to function properly.  Or is there any other way to find out  the amount of memory left before doing malloc ?

 

Under what circumstances does the malloc return NULL?? I know one is not enough memory. Are there any other reasons?

 

 

 

Thank You,

 

Prashanth

 

 

 

 

QuoteReplyEditDelete

 

 

2007-09-18 01:44:33     Re: Malloc returns NULL inspite of having enough memory

Mike Frysinger (UNITED STATES)

Message: 44236    what version of the kernel and toolchain are you using ?  malloc() should work perfectly fine (otherwise pretty much everything would fail)

QuoteReplyEditDelete

 

 

2007-09-18 08:28:19     Re: Malloc returns NULL inspite of having enough memory

Robin Getz (UNITED STATES)

Message: 44257    Prashanth:

 

When you say "malloc is failing" - are you seeing a out of memory message from the kernel, or are you just seeing a null pointer being returned?

 

-Robin

QuoteReplyEditDelete

 

 

2008-09-17 17:04:10     Re: Malloc returns NULL inspite of having enough memory

Ed Sutter (UNITED STATES)

Message: 62333   

 

This is probably a dumb question, but it just dawned on me...

 

With uClinux having no MMU, if I have an application that uses malloc, and does not free all of the space that was previously allocated, then the application exits.  Is that allocated memory still released by the OS at exit time?

QuoteReplyEditDelete

 

 

2008-09-17 17:13:00     Re: Malloc returns NULL inspite of having enough memory

Mike Frysinger (UNITED STATES)

Message: 62334   

 

not having a MMU does not mean you cant do resource tracking.  when an application dies, all resources it was using are released exactly the same as running on a processor with a MMU.

QuoteReplyEditDelete

 

 

2008-09-17 17:33:42     Re: Malloc returns NULL inspite of having enough memory

Ed Sutter (UNITED STATES)

Message: 62335   

 

Understood; however, with a true process-based system (using virtual memory), the memory would be released even without resource tracking simply because the data space of the process would virtually go away, correct?

QuoteReplyEditDelete

 

 

2008-09-17 17:40:36     Re: Malloc returns NULL inspite of having enough memory

Mike Frysinger (UNITED STATES)

Message: 62336   

 

there is no such thing as a "true process based system"

 

the binding you're thinking of does not exist at all ... Linux tracks all resources that a process uses regardless of the presence of a MMU.  it does all of this in software, not hardware.  the hardware is there only to accelerate the software table lookup and to actually translate the virtual addresses coming out of the core into physical addresses.  this allows the kernel to stitch/move disparate physical chunks of memory into one contigious chunk of virtual memory.  that's it.

QuoteReplyEditDelete

 

 

2008-09-18 07:58:49     Re: Malloc returns NULL inspite of having enough memory

Ed Sutter (UNITED STATES)

Message: 62381   

 

ok Mike, thanks for the lesson.

Attachments

    Outcomes