I am pointing to the address of a 2D array, and I am confused as to how to dereference the pointer to free up the memory again. (I don't use "->" or "*". Is that wrong?)
我指向2D数组的地址,我很困惑如何取消引用指针以再次释放内存。 (我不使用“ - >”或“*”。这是错的吗?)
My code:
double array[12][12];
//filled with numbers
double *arrayPtr; //pointer
arrayPtr = &array[0][0]; //pointing to address
multiply(arrayPtr, arrayPtr); //this works fine
//Do I need to do anything further to make sure my memory management is correct? And if so, why?
3 个解决方案
#1
4
In this case, the answer is no -- since you simply defined array
(didn't use something like malloc
to allocate it) you don't have to do anything to free it either. If it was local (defined inside a function) it'll be freed automatically when you exit the function. If you defined it outside any function, it's a global, so it'll exist the entire time the program runs. Either way, you don't have to d any explicit memory management.
在这种情况下,答案是否定的 - 因为你只是定义了数组(没有使用类似malloc的东西来分配它),你也不需要做任何事情来解放它。如果它是本地的(在函数内定义),它将在退出函数时自动释放。如果你在任何函数之外定义它,它是一个全局函数,所以它会在程序运行的整个时间内存在。无论哪种方式,您都不必进行任何显式内存管理。
#2
2
double array[12][12];
You're declaring array
on the stack. It's not dynamically allocated with the heap, so you don't "free up the memory".
你在堆栈上声明数组。它不是用堆动态分配的,所以你不要“释放内存”。
double *arrayPtr; //pointer
arrayPtr = &array[0][0]; //pointing to address
If you want to point to the first element, this would suffice:
如果你想指向第一个元素,这就足够了:
double* arrayPtr = array;
#3
0
First, there a quite different between C and C++
首先,C和C ++之间存在很大差异
In C to use memory management you shall use malloc/calloc/realloc and free. In c++ you will use new and delete.
在C中使用内存管理,你应该使用malloc / calloc / realloc和free。在c ++中,您将使用new和delete。
In your code.
在你的代码中。
double array[12][12];
This is imply to allocate memory in stack. So the memory will be allocated to the scope of this program section so that it will be green when the scope of this variable end.
这意味着在堆栈中分配内存。所以内存将被分配到这个程序部分的范围,以便当这个变量的范围结束时它将是绿色的。
If you will to use free you will need
如果您将免费使用,您将需要
double **array;
array = (double **) malloc(sizeof(double*));
*array = (double*) malloc (24 * sizeof(double));
free (*array);
free (array);
#1
4
In this case, the answer is no -- since you simply defined array
(didn't use something like malloc
to allocate it) you don't have to do anything to free it either. If it was local (defined inside a function) it'll be freed automatically when you exit the function. If you defined it outside any function, it's a global, so it'll exist the entire time the program runs. Either way, you don't have to d any explicit memory management.
在这种情况下,答案是否定的 - 因为你只是定义了数组(没有使用类似malloc的东西来分配它),你也不需要做任何事情来解放它。如果它是本地的(在函数内定义),它将在退出函数时自动释放。如果你在任何函数之外定义它,它是一个全局函数,所以它会在程序运行的整个时间内存在。无论哪种方式,您都不必进行任何显式内存管理。
#2
2
double array[12][12];
You're declaring array
on the stack. It's not dynamically allocated with the heap, so you don't "free up the memory".
你在堆栈上声明数组。它不是用堆动态分配的,所以你不要“释放内存”。
double *arrayPtr; //pointer
arrayPtr = &array[0][0]; //pointing to address
If you want to point to the first element, this would suffice:
如果你想指向第一个元素,这就足够了:
double* arrayPtr = array;
#3
0
First, there a quite different between C and C++
首先,C和C ++之间存在很大差异
In C to use memory management you shall use malloc/calloc/realloc and free. In c++ you will use new and delete.
在C中使用内存管理,你应该使用malloc / calloc / realloc和free。在c ++中,您将使用new和delete。
In your code.
在你的代码中。
double array[12][12];
This is imply to allocate memory in stack. So the memory will be allocated to the scope of this program section so that it will be green when the scope of this variable end.
这意味着在堆栈中分配内存。所以内存将被分配到这个程序部分的范围,以便当这个变量的范围结束时它将是绿色的。
If you will to use free you will need
如果您将免费使用,您将需要
double **array;
array = (double **) malloc(sizeof(double*));
*array = (double*) malloc (24 * sizeof(double));
free (*array);
free (array);