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));
2 Antworten
+ 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
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/