Thread: how the malloc function work in C

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    how the malloc function work in C

    I've been trying to understand how the malloc function in C dynamically allocates memory, and I'm not entirely clear on it. I read some theory but still have some questions.

    Let's look at this line of code as an example:

    Code:
    int *ptr = (int *)malloc(sizeof(int));

    I understand that ptr is declared as a pointer to an integer. What's puzzling me is the statement that "malloc returns a pointer to the first byte of the allocated memory."


    If I get it correctly, ptr should hold the memory address where integer variables can be stored. But what does it mean by "malloc returns a pointer to the first byte of the allocated memory"? Does this imply that ptr is holding another pointer that points to the actual memory?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    All pointers point to the first byte of whatever it is they're pointing to.

    A picture of some memory, with an integer and a pointer.
    Code:
         +-----+
    1000 | 2A  |    int var = 42
         +-----+    Declares an integer, which takes 4 bytes of memory, and contains the value 42
    1001 | 00  |
         +-----+
    1002 | 00  |
         +-----+
    1003 | 00  |
         +-----+
    1004 | E8  |    int *ptr = &var
         +-----+    Declares a pointer, which takes 4 bytes of memory, and contains the value 1000 (0x03E8)
    1005 | 03  |    That being the first byte of where the integer is stored.
         +-----+
    1006 | 00  |
         +-----+
    1007 | 00  |
         +-----+
    Whenever you use *ptr in your code, the compiler knows that the singular address 1000 is just the first byte of a 4-byte quantity that is an entire integer.

    Now with malloc.
    Code:
         +-----+
    1004 | 34  |    int *ptr = malloc(sizeof(int))
         +-----+    Declares a pointer, which takes 4 bytes of memory, and contains the value 0x1234
    1005 | 12  |    as the result of calling malloc.
         +-----+    The memory at 0x1234 through to 0x1237 (a total of 4 bytes) is the space
    1006 | 00  |    used to store one integer.
         +-----+
    1007 | 00  |
         +-----+
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    All pointers point to the first byte of whatever it is they're pointing to.
    Thank you salem
    What's puzzling me is the statement that "malloc returns a pointer to the first byte of the allocated memory."

    So, a function's return type can vary depending on the specific requirements . It can return values, pointer


    I believe that when the malloc function is executed, it returns a void pointer (void*), and the value it returns is, in fact, the memory address that the pointer ptr holds.

    I'd like to confirm if my understanding is correct.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Pointers always point at just one memory location.

    It's the pointer type which tells you how many bytes in total after that address are meaningful.

    > So, a function's return type can vary depending on the specific requirements . It can return values, pointer
    > I believe that when the malloc function is executed, it returns a void pointer (void*), and the value it returns is, in fact, the memory address that the pointer ptr holds.
    Type != Value

    > What's puzzling me is the statement that "malloc returns a pointer to the first byte of the allocated memory."
    Would it be better for you if it just said "malloc returns a pointer to the allocated memory"?

    Because that's what it means.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    Thank you salem
    What's puzzling me is the statement that "malloc returns a pointer to the first byte of the allocated memory."
    Why does this puzzle you? A pointer is a memory address. By almost-universal convention, pointers contain the address of the first byte of the data structure being pointed to. There are exceptions to this, however, including malloc()! Some data structures are stored with a "prefix" that occurs before the address that is pointed to. Generally,
    this is done to avoid having the data overwritten, or to allow the pointer to serve multiple purposes. Examples:

    1. In C, a common idiom for dynamic arrays (what C++ calls a vector) is to store length and capacity in a prefix struct that appears at the beginning of the allocated storage, then store the array. The returned pointer always references the start of the array, and some pointer math has to be done to look up the prefix struct.

    2. In C++ a common implementation of multiple inheritance is implemented by partitioning object memory into distinct parts for each parent class. Since inheritance can involve virtual methods, each distinct part stores a separate vtable as well, making it possible to pass a pointer into the middle of the child object to serve as the address for different parent classes.


    So, a function's return type can vary depending on the specific requirements . It can return values, pointer
    In C, a function's return type is set in stone when the function is declared. The return type can never be other than what it was declared with. You can certainly change the declaration as a developer, and you have the option of returning a union or a struct containing multiple data items. But once the code is compiled, the return type is fixed.

    I believe that when the malloc function is executed, it returns a void pointer (void*), and the value it returns is, in fact, the memory address that the pointer ptr holds.
    Technically, there are quibbles about whether NULL is a pointer or not. But you are substantially correct.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by aghast View Post
    Technically, there are quibbles about whether NULL is a pointer or not. But you are substantially correct.
    NULL the constant may be either a pointer or just a plain 0. So an implementation is allowed to do either of these:

    Code:
    #define NULL 0
    or
    Code:
    #define NULL ((void *)0)
    (or a variation of one of those).

    A null pointer (one that has been set to NULL or 0) is still a pointer, but it just happens to point to nothing.

    Note: yes, there are some architectures where a null pointer is not all zero bits, but even on those architectures, NULL is still defined as 0. C takes care of converting a 0 constant to whatever bit pattern the architecture uses for null pointers. In the vast majority of cases you don't have even to worry about it. See Question 5.2 How do I get a null pointer in my programs?, Question 5.4 What is NULL and how is it defined?, and Question 5.5 How should NULL be defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer?. C++ also has a nullptr keyword which is basically equivalent to NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc function
    By Nikosant03 in forum C Programming
    Replies: 3
    Last Post: 02-10-2019, 02:24 PM
  2. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM
  3. Why doesnt this malloc work please?
    By rkooij in forum C Programming
    Replies: 24
    Last Post: 10-10-2006, 09:04 AM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. Replies: 5
    Last Post: 03-11-2002, 05:22 PM

Tags for this Thread