int** returnColumnSizes = (int**)malloc(sizeof(int) * 2000); what is the problem
时间: 2023-10-07 08:10:51 浏览: 38
The problem is that the allocated memory size is not correct. It should be sizeof(int*) * 2000 instead of sizeof(int) * 2000. This is because we are allocating memory for an array of pointers to integers, not for an array of integers.
相关问题
int** returnColumnSizes = (int*)malloc(sizeof(int*) * 100); is this correct
No, it is not correct.
The correct way to allocate memory for an array of integers is:
```
int* returnColumnSizes = (int*)malloc(sizeof(int) * 100);
```
This allocates memory for an array of 100 integers, and the pointer `returnColumnSizes` points to the first element of the array.
int** returnColumnSizes = (int**)malloc(sizeof(int*) * 100);
This line of code allocates memory for an array of 100 integer pointers. The purpose of this array is to store the size of each column in a 2D array.
The syntax used here is known as dynamic memory allocation, which allows the program to request memory from the operating system at runtime. In this case, the size of the memory block is determined by the size of the data type (int*) multiplied by the number of elements (100).
The resulting memory block is then cast to type int**, which is a pointer to a pointer of integers. This means that the array can be treated as a 2D array, with each element pointing to a row of integers.
It is important to note that this memory block must be freed using the free() function when it is no longer needed, to avoid memory leaks in the program.