在C指针INT矩阵 - 内存分配混乱 [英] int matrix with pointers in C - memory allocation confusion
问题描述
我在使用,而无需创建内存泄漏产生的int矩阵的一些问题。我希望能够使一个给定的(全球的)基质成任意大小动态地经由read_matrix()。但我希望能够稍后释放内存。所以在我的主要方法的第二个printf应导致总线错误,因为它不应该有分配给它的内存。我将如何去创造呢?
INT ** first_matrix;
INT ** second_matrix;
INT ** result_matrix;INT ** read_matrix(INT size_x,诠释size_y)
{
INT **矩阵;
矩阵=释放calloc(size_x,sizeof的为(int *));
的for(int i = 0; I< size_x;我++){
矩阵[I] =释放calloc(size_y,sizeof的(INT));
}
的for(int i = 0; I< size_x;我++){
对于(INT J = 0; J< size_y; J ++){
矩阵[I] [J] = I * 10 + J;
}
}
返回矩阵;
}INT主(INT stackc,焦炭**堆栈)
{
first_matrix = read_matrix(10,10);
输出(9:3%d个 - 4:6%d个\\ N,first_matrix [9] [3],first_matrix [4] [6]);
免费(* first_matrix);
免费(first_matrix);
输出(9:3%d个 - 4:6%d个\\ N,first_matrix [9] [3],first_matrix [4] [6]);
}
只是因为记忆已经free'd并不意味着你不能访问它!当然,这是一个极坏的想法来访问它,它已经free'd后,但是这就是为什么它在你的榜样。
注意免费(* first_matrix)
唯一的免费的 first_matrix [0]
,而不是其他的阵列。你可能需要某种形式的标记,以表示最后一个数组(除非你当你释放外阵列有多少内部分配的数组总会知道的)。是这样的:
INT ** read_matrix(INT size_x,诠释size_y)
{
INT **矩阵;
矩阵=释放calloc(size_x,1 +的sizeof(INT *)); // ALLOC一个额外的PTR
的for(int i = 0; I< size_x;我++){
矩阵[I] =释放calloc(size_y,sizeof的(INT));
}
矩阵[size_x] = NULL; //设置额外的PTR为NULL
的for(int i = 0; I< size_x;我++){
对于(INT J = 0; J< size_y; J ++){
矩阵[I] [J] = I * 10 + J;
}
}
返回矩阵;
}
然后当你释放他们:
//不断循环,直到找到NULL 1
的for(int i = 0; first_matrix [我] = NULL;!我++){
免费(first_matrix [I]);
}
免费(first_matrix);
I'm having some issues with producing an int matrix without creating memory leaks. I want to be able to make a given (global) matrix into any size dynamically via read_matrix(). But then i want to be able to free the memory later on. So in my main method the second printf should result in a bus error since it should not have any memory allocated to it. How would i go about creating this?
int** first_matrix;
int** second_matrix;
int** result_matrix;
int** read_matrix(int size_x, int size_y)
{
int** matrix;
matrix = calloc(size_x, sizeof(int*));
for(int i = 0;i<size_x;i++) {
matrix[i] = calloc(size_y, sizeof(int));
}
for(int i = 0;i<size_x;i++) {
for(int j = 0;j<size_y;j++) {
matrix[i][j] = i*10+j;
}
}
return matrix;
}
int main(int stackc, char** stack)
{
first_matrix = read_matrix(10,10);
printf("9:3 %d - 4:6 %d \n", first_matrix[9][3], first_matrix[4][6]);
free(*first_matrix);
free(first_matrix);
printf("9:3 %d - 4:6 %d \n", first_matrix[9][3], first_matrix[4][6]);
}
Just because the memory has been free'd doesn't mean you can't access it! Of course, it's a very bad idea to access it after it's been free'd, but that's why it works in your example.
Note that free( *first_matrix )
only free's first_matrix[0]
, not the other arrays. You probably want some kind of marker to signify the last array (unless you will always know when you free the outer array how many inner arrays you allocated). Something like:
int** read_matrix(int size_x, int size_y)
{
int** matrix;
matrix = calloc(size_x, 1+sizeof(int*)); // alloc one extra ptr
for(int i = 0;i<size_x;i++) {
matrix[i] = calloc(size_y, sizeof(int));
}
matrix[size_x] = NULL; // set the extra ptr to NULL
for(int i = 0;i<size_x;i++) {
for(int j = 0;j<size_y;j++) {
matrix[i][j] = i*10+j;
}
}
return matrix;
}
Then when you're freeing them:
// keep looping until you find the NULL one
for( int i=0; first_matrix[i] != NULL; i++ ) {
free( first_matrix[i] );
}
free( first_matrix );
这篇关于在C指针INT矩阵 - 内存分配混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!