What does malloc(0) return? [duplicate]

Others have answered how malloc(0) works. I will answer one of the questions that you asked that hasn’t been answered yet (I think). The question is about realloc(malloc(0), 0): What does malloc(0) return? Would the answer be same for realloc(malloc(0),0)? The standard says this about realloc(ptr, size): if ptr is NULL, it behaves like malloc(size), … Read more

When and why to use malloc?

malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means you allocate the memory at run time. For example when you don’t know the amount of memory during compile time. One example should clear this. Say you know there will be maximum 20 students. So you can create an array … Read more

Is malloc thread-safe?

Question: “is malloc reentrant”? Answer: no, it is not. Here is one definition of what makes a routine reentrant. None of the common versions of malloc allow you to re-enter it (e.g. from a signal handler). Note that a reentrant routine may not use locks, and almost all malloc versions in existence do use locks … Read more