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

How do you ‘realloc’ in C++?

Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If you want to pass pointer into function, instead of Foo(t) use Foo(&t[0]) It is absolutely correct C++ code, because vector is a smart C-array.

Malloc , Realloc , Memset : Struct pointers , arrays of char, int

Error was in v_push_back implementation : v->e_array = check_a(memcpy((void*)((char*)v->e_array + (v->no_e * v->e_sz)) ,new_val, v->e_sz)); //pointer has not to be saved check_a(memcpy((void*)((char*)v->e_array + (v->no_e * v->e_sz)) ,new_val, v->e_sz)); Because memcpy returns a pointer , in this case, to the last element of the array so we would have lost access to all previous memory

Can you tell me specific what this program is doing? [closed]

First of all, this line if(!(temp = (double*)realloc(data, capacity*sizeof(double)))) should look like if(!(temp = realloc(data, capacity*sizeof(double)))) because as per this discussion we need not to cast the return value of malloc() and family in C.. That said, to break down the statement, First, temp = realloc(data, capacity*sizeof(double)) gets evaluated. This statement reallocates data to have … Read more