3

How can i check whether malloc() was fail in Keil C?

unsigned char xdata malloc_mempool [0x100];
void display()
{
    unsigned char xdata *ptr;
    int a;
    init_mempool (&malloc_mempool, sizeof(malloc_mempool));

    ptr = malloc(9000000);
    if(ptr != 0)
    {
        a = 7;
        free(ptr);
    } else {
        a = 9;
    }
}

As stated in http://www.keil.com/support/man/docs/c51/c51_malloc.htm malloc will return null pointer if there is not enough memory to satisfy the allocation request. It is obviously not so mush memory in 8051. But the result of a is always 7.

mingpepe
  • 489
  • 5
  • 10
  • 1
    `9000000` doesn't even fit into an int. I don't remember by heart what the compiler is supposed to do in this case, but you might want to change this into a value < 65536. Am I right to suppose that `int` is 16 bit in your case? – glglgl Jun 07 '16 at 13:02
  • @glglgl, `malloc()` takes an `unsigned int` though. – Good Night Nerd Pride Jun 07 '16 at 13:04
  • 1
    How much RAM does your device actually have? – jarmod Jun 07 '16 at 13:04
  • From [this table](http://www.keil.com/boards/8051.asp) one can tell it's definitely below 8.5 MB. – Good Night Nerd Pride Jun 07 '16 at 13:05
  • What's the size of an `int` on your platform? – Jabberwocky Jun 07 '16 at 13:06
  • The result is the same for malloc(9000).(In the range of int). – mingpepe Jun 07 '16 at 13:06
  • @mingpepe And how much RAM does your device have ? – Jabberwocky Jun 07 '16 at 13:07
  • @MichaelWalz RAM size shouldn't matter - `malloc` only takes as much as provided with `init_mempool()`, so for the sake of this queation, 256 Bytes. – glglgl Jun 07 '16 at 13:09
  • @mingpepe try: `for (;;) { unsigned char xdata *ptr; ptr = malloc(1000); if (ptr == NULL) crash(); }`. With this `crash()` should be called at some point. – Jabberwocky Jun 07 '16 at 13:10
  • Maybe there is a minimum for the size of the mempool. 256 bytes is really very low. – Jabberwocky Jun 07 '16 at 13:11
  • @Abbondanza As `unsigned int` and `int` have the same sizes, a system with 16 bit `int` has also 16 bit `unsigned int`, where the limit is 65535. – glglgl Jun 07 '16 at 13:11
  • @MichaelWalz sizeof(int) = 2 and RAM=512 bytes, but it seems not matter. And the code does not execute the crash() – mingpepe Jun 07 '16 at 13:15
  • 1
    @mingpepe look at the source code of `malloc`. BTW does it make sense to have dynamic memory allocation on a system with 512 bytes of RAM ? – Jabberwocky Jun 07 '16 at 13:22
  • @mingpepe You could try displaying the value of `ptr` in order to maybe get an idea of why it doesn't work – glglgl Jun 07 '16 at 13:37
  • @mingpepe, why is there still `9000000` in your example code? Update it to be a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). What exactly is returned in `ptr`? What happens when you try to read from/write to the memory `malloc()` claims to have allocated? – Good Night Nerd Pride Jun 07 '16 at 13:46
  • 1
    "But the result of a is always 7." --> Perhaps since code does nothing with `a` that `a` is optimized. OP's assertion that `a` is 7 is based upon reading `a` in an unstated method. Make real code that prints the value of `a`. and the value of `ptr`. – chux - Reinstate Monica Jun 07 '16 at 14:22

1 Answers1

2

From Understanding The Memory Organization Of 8051 Microcontroller the 8051 has a maximum 64KB of memory.

So it will be using 2 bytes for pointers. 9000000 decimal is 0x895440 in hex, which is 3 bytes.

Very probably the malloc() function will just ignore the bits it cannot use, so in reality the call you are making is

ptr = malloc(0x5440);

which is

ptr = malloc(21568);
  • 1
    Probably correct, but that still doesn't answer why it doesn't work with `malloc(9000)` (see comments). – Jabberwocky Jun 07 '16 at 13:29
  • 3
    Er, yes it does. The question says " the result of a is always 7." which means malloc allocated memory successfully. If you get the same result with malloc(9000) that is expected. –  Jun 07 '16 at 13:34
  • 2
    @buffjape With a malloc pool of 256 bytes I wouldn't expect that at all. – glglgl Jun 07 '16 at 13:36
  • OP shouldn't expect others to gather the missing pieces from some comments but [post a minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) instead. – Good Night Nerd Pride Jun 07 '16 at 13:41