Realloc copy the whole memory previously allocated using malloc into a new memory location in O(n) time complexity? (Yes/No)
malloc(3) gives you a void* , you must specify the type that you allocated memory for it.
int* ptr = (int *) malloc(4) doesn't make sense why ptr has to be declared as int* twice here
This is two operations smooshed together int* ptr; ptr = (int*) malloc(sizeof(i)); Bear in mind that malloc returns void*
Okay, so this is how it's done what about the Realloc ques?
yes
So that means push_back() of vector in cpp (which is also a dynamic array) has no time complexity compared to realloc?
A vector, holds a array. It's dynamic, but this dynamic comes into play when the array is filled up, so it expands the array, using malloc/realloc and copies old data to new array. When you do push_back(), if array is not full, it will be o(1) but if it's full it would be o(1)+o(time to reallocate n*existing array size and copy data)
thanks for taking out time to type that much I understand now ah
No, in C you have autocast from void*. Realloc enlarge Memory allocated, but if can't 1)create new Memory 2)Copy to new address O(n) 3)release old address.
No bruh
Обсуждают сегодня