This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Why Malloc function returning "0x00000000" address?

Howdy!

First look at the code.

Here is the code...

int **arrofptr;

arrofptr = (int **) malloc(sizeof(int*)*3);

arrofptr[0] = (int *)malloc(sizeof(int) * 4800);

arrofptr[1] = (int *)malloc(sizeof(int) * 4800);

arrofptr[2] = (int *)malloc(sizeof(int) * 4736);

Now my questions is when i execute the below statement 

printf ("arrofptr = %p\n,arrofptr[0] = %p\narrofptr[1] %p\narrofptr[2] %p\n",arrofptr,arrofptr[0],arrofptr[1],arrofptr[2]);

it outputs

arrofptr = 0x0080C5F8

arrofptr[0] = 0x00000000

arrofptr[1] = 0x00000000

arrofptr[2] = 0x00000000

First malloc returning some address but other three mallocs returning 0x00000000. But the other three mallocs should return some address like first malloc.

why the other three mallocs are returning 0x00000000 ? 

please help me to get rid of this problem.

Thanks in advance.

  • Hi,

    malloc() should returns zero only in case of heap memory exhaustion. How much heap have you reserved in the linker options (Linker->basic options->heap size)?

    If you don't set it, the linker generates a default 1K heap, that is not enough for your needs.

  • hi,

    yes it is 

    -heap 0x1000

    How much size should i set?

    Thanks for your time,

  • dixit singla said:

    yes it is 

    -heap 0x1000

    How much size should i set?

    0x1000 = 4096bytes, while you need at least sizeof(int) * number of items, that is 4*(4800+4800+4736+3) = 57376bytes plus some space for the heap managment data. Set it to 60000 or higher and retry.

  • Hi,

    Alberto Chessa

    Yes now malloc is working correctly.

    Thanks for your help and for your precious time.

  • hello!

    Alberto Chessa

    I have an another question related to the above problem,

    After increasing the heap size malloc is working correctly suppose arrofptr[0] is returning some address. Is there any way to know the size of memory, allocated by malloc function from the returned address.

    if i will execute the below statement

    printf("%d",sizeof(arrofptr[0]));

    Then simply it will print 4 because arrofptr is a pointer.

    Thanks,

    your answer will help me a lot and will be appreciated.

  • Yes, 

     

     if the return address is not zero, then the allocation is exactly what you asked for in the malloc instruction

     

     

  • dixit singla said:

    Is there any way to know the size of memory, allocated by malloc function from the returned address.

    You should post this quesiton on the "TI C7C++ Compiler" forum.

    Anyway, there isn't a standard way and as far as I know the TI compiler doesn't supply any extension to do that.

  • hi,

    ran

    First thanks for your reply,

    Bus sir my question was, can i get the size just from address returned by malloc somewhere else in the program??

  • hi,

    Alberto Chessa

    Thanks for your interest.

  • dixit singla,

    In most compilers, there is no pre-made function in C programming for getting the size of allocated memory from a pointer. I would recommend using predefined sized - this works well if you know the exact size of the array you want. You can do a #define ARRSIZE (100) to define your array size and refer to this macro, ARRSIZE, whenever you need it.

    You can also store the size in a variable instead of using a preprocessing directive. This would be the better option if you have dynamically allocated memory, especially if the size changes.

    -Ivan

  • dixit singla said:

    Bus sir my question was, can i get the size just from address returned by malloc somewhere else in the program??

    The good way it is to use some kind of struct to pass the array to your functions instead of using the pointer itself. The struct can store the pointer and the size also. In this case, C++ can help a lot.

    If, for any reason, you really don't have any other choice, you can hack, at your risk, the compiler library. Look at <compiter-inst-directory>/lib/rtssrc.zip: you can find memory.c. Inside there is the malloc()/free() implementation so you can discover where the malloc() store the size. Warning: the implementation can change from a version to another.

    For version 7.3.3, you can try with (not tested code):

    typedef struct pack  //defined in memory.c
    {
        unsigned int packet_size;     /* number of bytes        */
        struct pack  *size_ptr;       /* next elem in free list */
    } PACKET;

    #define BLOCK_OVERHEAD sizeof(long double)
    #define BLOCK_USED 1

    unsigned int malloc_size(const void* const p)
    {
      unsigned int tmp=(unsigned int)p;
      tmp-=BLOCK_OVERHEAD;
      const PACKET* const pkt=(const PACKET*)tmp;
      return pkt->packet_size & ~BLOCK_USED;
    }

  • Ivan Pang said:

    dixit singla,

    In most compilers, there is no pre-made function in C programming for getting the size of allocated memory from a pointer. I would recommend using predefined sized - this works well if you know the exact size of the array you want. You can do a #define ARRSIZE (100) to define your array size and refer to this macro, ARRSIZE, whenever you need it.

    You can also store the size in a variable instead of using a preprocessing directive. This would be the better option if you have dynamically allocated memory, especially if the size changes.

    -Ivan

    Ivan Pang,

    You are right,

    But i can not do this because the size of memory i want to allocate dynamically is also calculating at run time,

    The better solution in my opinion is to use a structure.

    struct dynamall
    {
    int size;
    int **ptr;
    }obj;

  • Alberto Chessa said:

    Bus sir my question was, can i get the size just from address returned by malloc somewhere else in the program??

    The good way it is to use some kind of struct to pass the array to your functions instead of using the pointer itself. The struct can store the pointer and the size also. In this case, C++ can help a lot.

    If, for any reason, you really don't have any other choice, you can hack, at your risk, the compiler library. Look at <compiter-inst-directory>/lib/rtssrc.zip: you can find memory.c. Inside there is the malloc()/free() implementation so you can discover where the malloc() store the size. Warning: the implementation can change from a version to another.

    For version 7.3.3, you can try with (not tested code):

    typedef struct pack  //defined in memory.c
    {
        unsigned int packet_size;     /* number of bytes        */
        struct pack  *size_ptr;       /* next elem in free list */
    } PACKET;

    #define BLOCK_OVERHEAD sizeof(long double)
    #define BLOCK_USED 1

    unsigned int malloc_size(const void* const p)
    {
      unsigned int tmp=(unsigned int)p;
      tmp-=BLOCK_OVERHEAD;
      const PACKET* const pkt=(const PACKET*)tmp;
      return pkt->packet_size & ~BLOCK_USED;
    }

    [/quote]

    Alberto Chessa,

    I have found the implementation but it is very complex and to change it is very risky because it will affect my other projects (if i change) but still big thanks to you for clearing my doubts.