Thread: to malloc() or not to malloc()...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    to malloc() or not to malloc()...

    Hi

    Is the outcome of

    Code:
    uint8_t *message;
    message = (uint8_t*)malloc(56);
    assuming success exactly same as

    Code:
    uint8_t message[56];
    Thanks

    David

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No.

    The latter only lives for as long as the scope it is in. So if it's a global variable, it's going to live as long as the application. But if it's a local variable, it will disappear when the function returns. That may be a good thing or a bad thing, depending on what you want to do with the variable. If it's only used within the function, make it local.

    Of course, malloc() will take a fair few cycles (as in hundreds or thousands) from the CPU to find the space for the memory, allocating the space on the stack involves a single add instruction - modern processors will do that in one clockcycle.

    And of course, if you use malloc, you should use free to return the memory when you are finished.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Shouldn't it be malloc(sizeof(uint8_t) * 56)?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by noops View Post
    Shouldn't it be malloc(sizeof(uint8_t) * 56)?
    Yes, but if uint8_t is "unsigned char", we know that its size is 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Not really, because the memory for the latter is allocated on the stack, while the former is allocated on the heap. If you allocate something on the stack, it is available only within the function in which it is allocated. For example, this is very wrong:

    Code:
    #define SIZE_OF_BUF 56
    uint8_t *right(void)
    {
        uint8_t *buffer = malloc(SIZE_OF_BUF);
        int i = 0;
        for (i = 0; i < SIZE_OF_BUF; ++i)
            buffer[i] = i;
        return buffer;
    }
    
    uint8_t *wrong_wrong(void)
    {
        uint8_t buffer[SIZE_OF_BUF];
        int i = 0;
        for (i = 0; i < sizeof(buffer); ++i)
            buffer[i] = i;
        return &buffer[0];
    }
    
    int main(void)
    {
        uint8_t *output = wrong_wrong();
        // What's in the buffer variable is likely gone, because it was 
        // allocated on the stack and therefore went away once you
        // returned from the function.
        // output is now pointing somewhere it's not supposed to.
        printf("Yikes, accessing the output will likely crash!\n");
        
        output = right();
        printf("It will be fine to access output, however, "
                 "you must de-allocate it when you're done\n");
        free(output);
     
        return 0;
    }
    Look here for a nice, *nix-specific explanation of memory management in C.
    Last edited by rags_to_riches; 08-18-2008 at 10:08 AM. Reason: Declare i in functions and return int from main()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM