0

What is the purpose of doing this.. I've seen this in a code

int* x = (int*)malloc(sizeof(int));     int* y = (int*)malloc(sizeof(int));

25th Aug 2020, 6:14 AM
Morningstar
Morningstar - avatar
2 Answers
+ 1
- sizeof(int) returns the memory size of an int. - malloc() function allocates memory dynamically on the heap and returns a void pointer - The void pointer is then cast into an int pointer, to store it in the x or y variable
25th Aug 2020, 7:39 PM
Terminal_Phantom
Terminal_Phantom - avatar
0
When allocating space to a pointer you mostly ever want to allocate to it memory with the size of what is it pointing to by typically using the sizeof operator. https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
25th Aug 2020, 6:56 AM
minirkk
minirkk - avatar