1

I am currently learning how memory is managed by malloc and free, and I don't understand why when I want to malloc 1 byte, malloc will allocate 32 bytes for the chunk ? For me, it should allocate 16 bytes for metadata (size of previous chunk and next chunk) and 1 byte for the data...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/**                                                                                            
 * pmem - print mem                                                                            
 * @p: memory address to start printing from                                                   
 * @bytes: number of bytes to print                                                            
 *                                                                                             
 * Return: nothing                                                                             
 */
void pmem(void *p, unsigned int bytes)
{
    unsigned char *ptr;
    unsigned int i;

    ptr = (unsigned char *)p;
    for (i = 0; i < bytes; i++)
    {
        if (i != 0)
        {
            printf(" ");
        }
        printf("%02x", *(ptr + i));
    }
    printf("\n");
}

/**
 * main - moving the program break
 *
 * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS
 */
int main(void)
{
    void *p;
    size_t size_of_the_chunk;
    char prev_used;

    p = malloc(1);
    printf("%p\n", p);
    pmem((char *)p - 0x10, 0x10);
    size_of_the_chunk = *((size_t *)((char *)p - 8));
    prev_used = size_of_the_chunk & 1;
    size_of_the_chunk -= prev_used;
    printf("chunk size = %li bytes\n", size_of_the_chunk);
    return (EXIT_SUCCESS);
}

That's the result :

0x13bf010

00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00

chunk size = 32 bytes

3
  • 1
    How do you check? Please create a minimal reproducible example to show us. Feb 7, 2020 at 13:24
  • 1
    your program invokes Undefined Behavour. Feb 7, 2020 at 13:42
  • Since you're asking about implementation details, you need to be very specific about things like your operating system, your C library, versions of operating system and C library. Feb 7, 2020 at 13:45

4 Answers 4

5

malloc is required to return pointers that are sufficiently aligned to store any type of data, even types of data that can't fit into the space allocated. That means that if _Alignof(max_align_t) == 32 on your system, malloc(1) has to return only pointers that are multiples of 32, even if you ask for less space than that. An easy way for the implementation to do this is for it to round up each allocation to a multiple of 32 in its internal bookkeeping data, which appears to be what you are looking at.

However, the C standard specifically forbids you to access memory even a single byte beyond the end of the amount of space you asked for. It also specifically forbids you to access malloc's internal bookkeeping data. If you run your program under a memory access validator like valgrind or ASan it will crash.

4
  • Ok, but why pointer need to be sufficiently aligned and why malloc need to think about what's the user will put in this memory ? I ask this because i have to reimplemente malloc and I don't catch this concept..
    – Progear
    Feb 7, 2020 at 14:05
  • If you're not familiar with the concept of address alignment, you need to look that up before you continue trying to implement malloc. The C standard's rule is designed to make it so malloc doesn't need to think about what the caller will put in the memory; it just always produces an address that's usable for anything. People argue about whether it makes sense to insist on max_align_t alignment for allocations that are smaller than sizeof(max_align_t); it can waste a lot of space for programs that make lots of small allocations. But the rule is what it is, at least for now.
    – zwol
    Feb 7, 2020 at 15:00
  • Is it link with the fact of processor read 32 bytes at a time ?
    – Progear
    Feb 7, 2020 at 15:15
  • It's linked with that, yes. I really have to ask you to do your own research on this subject -- I can't fit a complete explanation of alignment into the comment box and I'm sure there are good explanations of it online already. I'd look some up for you but I'm on really unreliable coffee shop wifi right now.
    – zwol
    Feb 7, 2020 at 15:56
2

I want to malloc 1 byte, malloc will allocate 32 bytes for the chunk ? For me, it should allocate 16 bytes for metadata (size of previous chunk and next chunk) and 1 byte for the data...

Then you need to write your own version of the malloc.

Standard does not say how this allocation should look like, if there is any additional data, or how much memory will be allocated.

There are many circumstances which have to be taken into the consideration, not only the "metadata". For sure author of the implementation had something in mind when decided how to implement 'malloc'

1

In short, the C library memory management is free to allocate as much memory as it considers useful. Maybe for the sake of simplicity, it only allocates in multiples of 32 bytes. Then, obviously, 32 byte is the minimum.

Other allocators may use other strategies.

0

For me, it should allocate 16 bytes for metadata (size of previous chunk and next chunk)

No, you can't state something like that. It should allocate at least one byte, that's all. You are supposing that a given allocation strategy/implementation works in a very specific manner, but you can't in general. There is no standard that specifies the way block of free/allocated chunks of memory are managed. This may be described in the documentation of the allocation library you use.

and 1 byte for the data

Again, at least. Most of allocators prefer to allocate a bunch of bytes (32 or 64 is common).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.