Difference between global non-throwing ::operator new and std::malloc

The main differences, aside from syntax and free vs. delete, are you can portably replace ::operator new; malloc comes with realloc, for which new has no equivalent; new has the concept of a new_handler, for which there is no malloc equivalent. (Replacing malloc opens up a can of worms. It can be done, but not … Read more

Does malloc() use brk() or mmap()?

If we change the program to see where the malloc‘d memory is: #include <unistd.h> #include <stdio.h> #include <stdlib.h> void program_break_test() { printf(“%10p\n”, sbrk(0)); char *bl = malloc(1024 * 1024); printf(“%10p\n”, sbrk(0)); printf(“malloc’d at: %10p\n”, bl); free(bl); printf(“%10p\n”, sbrk(0)); } int main(int argc, char **argv) { program_break_test(); return 0; } It’s perhaps a bit clearer that … Read more

Should I cast the result of malloc?

No; you shouldn’t cast the result, since: It is unnecessary, as void * is automatically and safely promoted to any other pointer type in this case. It adds clutter to the code, casts are not very easy to read (especially if the pointer type is long). It makes you repeat yourself, which is generally bad. … Read more

there is no heap in c?

The C language itself does not specify directly for a heap or how it should work, but does provide pointers, etc. malloc and its cousins are part of something called the C Standard Library, and are functions that you link to with any standard implementation of C, and those do provide access to memory that … Read more