动态数组,传递地址
Dynamic array, passing addresses
我想创建一个函数,其参数是我动态分配的二维数组,问题是我不知道如何构建我的函数。
这是我的主要内容(N_MAX 定义为 300):
int i;
int **Bord = NULL;
Bord = malloc(N_MAX * sizeof(*Bord));
if(Bord == NULL)
{
printf("Error while allocating memory to an array");
free(Bord);
return -1;
}
for(i = 0; i < N_MAX; i++)
{
printf("%d\n", i);
Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
if(Bord[i] == NULL)
{
printf("Error while allocating memory to an array");
while(i != 0)
{
free(Bord[i]);
i--;
}
free(Bord);
return -1;
}
}
我试过用一个不是动态分配的数组做一些事情,但它真的很糟糕。当我想调试函数时,我的手表可以工作(我的数组中有正确的数字)但程序不工作(错误:一元'*'的无效类型参数(有'int'))
我已经在 Google 上进行了一些搜索,但我找不到包含 "dynamic allocation" 和 "pass by reference" 的主题,如果有请告诉我。
如何建立这个功能?
在尝试猜测您的 'new' 函数的外观之前,让我们更正发布的代码中的(许多)问题。
// added missing `#include` statements
#include <stdio.h>
#include <stdlib.h>
// added missing definition of N_MAX
#define N_MAX 300
int main( void ) // corrected signature for 'main'
{
//int i;
// minimize the scope of variables
int **Bord = NULL;
//edited following line
Bord = malloc(N_MAX * sizeof(*Bord));
if(Bord == NULL)
{
//printf("Error while allocating memory to an array");
// error messages should be output to 'stderr', not 'stdout'
// suggest:
perror( "malloc failed" );
//free(Bord); DONT do this, the allocation was not successful
return -1;
}
// implied else, malloc was successful
for( int i = 0; i < N_MAX; i++)
{
printf("%d\n", i);
Bord[i] = malloc(N_MAX * sizeof(*(Bord[i]));
if(Bord[i] == NULL)
{
//printf("Error while allocating memory to an array");
// error messages should be output to 'stderr', not 'stdout'
// suggest:
perror( "malloc failed" );
//while(i != 0)
// this will not free the first sub allocation
// suggest
i--;
while( i >= 0 )
{
free(Bord[i]);
i--;
}
free(Bord);
return -1;
}
}
// the following will result in a memory leak
// because all those memory allocations have not been passed to 'free()'
return 0;
}
这是一个简单的程序,它说明了两个函数 — init_array()
在问题中的代码(或多或少)创建数组后初始化数组,print_array()
打印值在数组中。除了合理的职责分离外,这两个函数还表明 init_array()
中设置的内容在 print_array()
函数中可见。还有一个小函数 free_array()
,made static inline
,它释放为数组分配的数据——它处理部分和完全分配的数组。除了 main()
之外的所有函数都标记为 static
,因为没有其他文件需要查看任何函数。请注意,为了便于测试,我将 N_MAX
设置为 8,而不是 300。现在我知道它正在工作,将 N_MAX
设置为 300 是可行的,也许是明智的。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static void print_array(int **base);
static void init_array(int **base);
enum { N_MAX = 8 };
static inline void free_array(int n_rows, int **array)
{
for (int i = 0; i < n_rows; i++)
free(array[i]);
free(array);
}
int main(void)
{
int i;
int **Bord = NULL;
srand(time(0)); // Not good, but better than nothing
Bord = malloc(N_MAX * sizeof(*Bord));
if (Bord == NULL)
{
fprintf(stderr, "Error while allocating %zu bytes of memory\n", N_MAX * sizeof(*Bord));
return -1;
}
for (i = 0; i < N_MAX; i++)
{
Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
if (Bord[i] == NULL)
{
fprintf(stderr, "Error while allocating %zu bytes of memory\n", N_MAX * sizeof(*Bord[i]));
free_array(i, Bord);
return -1;
}
}
init_array(Bord);
print_array(Bord);
free_array(N_MAX, Bord);
return 0;
}
static void init_array(int **base)
{
for (int i = 0; i < N_MAX; i++)
{
for (int j = 0; j < N_MAX; j++)
base[i][j] = (100 * (i + 1)) + (10 * (j + 1)) + rand() % 10;
}
}
static void print_array(int **base)
{
for (int i = 0; i < N_MAX; i++)
{
printf("[%d]:", i);
for (int j = 0; j < N_MAX; j++)
printf(" %3d", base[i][j]);
putchar('\n');
}
}
请注意,它使用 time()
来初始化随机数生成器,因此它通常应该在每次调用时生成一组新的数字,除非您在一秒钟内多次调用它。
[0]: 115 121 137 142 159 166 175 181
[1]: 211 224 239 248 253 265 277 283
[2]: 316 320 337 349 357 364 376 380
[3]: 419 428 439 448 451 469 476 484
[4]: 511 527 534 544 558 569 578 585
[5]: 616 623 631 647 650 664 671 688
[6]: 710 729 739 748 759 766 779 783
[7]: 817 824 839 847 850 860 878 881
我想创建一个函数,其参数是我动态分配的二维数组,问题是我不知道如何构建我的函数。
这是我的主要内容(N_MAX 定义为 300):
int i;
int **Bord = NULL;
Bord = malloc(N_MAX * sizeof(*Bord));
if(Bord == NULL)
{
printf("Error while allocating memory to an array");
free(Bord);
return -1;
}
for(i = 0; i < N_MAX; i++)
{
printf("%d\n", i);
Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
if(Bord[i] == NULL)
{
printf("Error while allocating memory to an array");
while(i != 0)
{
free(Bord[i]);
i--;
}
free(Bord);
return -1;
}
}
我试过用一个不是动态分配的数组做一些事情,但它真的很糟糕。当我想调试函数时,我的手表可以工作(我的数组中有正确的数字)但程序不工作(错误:一元'*'的无效类型参数(有'int'))
我已经在 Google 上进行了一些搜索,但我找不到包含 "dynamic allocation" 和 "pass by reference" 的主题,如果有请告诉我。
如何建立这个功能?
在尝试猜测您的 'new' 函数的外观之前,让我们更正发布的代码中的(许多)问题。
// added missing `#include` statements
#include <stdio.h>
#include <stdlib.h>
// added missing definition of N_MAX
#define N_MAX 300
int main( void ) // corrected signature for 'main'
{
//int i;
// minimize the scope of variables
int **Bord = NULL;
//edited following line
Bord = malloc(N_MAX * sizeof(*Bord));
if(Bord == NULL)
{
//printf("Error while allocating memory to an array");
// error messages should be output to 'stderr', not 'stdout'
// suggest:
perror( "malloc failed" );
//free(Bord); DONT do this, the allocation was not successful
return -1;
}
// implied else, malloc was successful
for( int i = 0; i < N_MAX; i++)
{
printf("%d\n", i);
Bord[i] = malloc(N_MAX * sizeof(*(Bord[i]));
if(Bord[i] == NULL)
{
//printf("Error while allocating memory to an array");
// error messages should be output to 'stderr', not 'stdout'
// suggest:
perror( "malloc failed" );
//while(i != 0)
// this will not free the first sub allocation
// suggest
i--;
while( i >= 0 )
{
free(Bord[i]);
i--;
}
free(Bord);
return -1;
}
}
// the following will result in a memory leak
// because all those memory allocations have not been passed to 'free()'
return 0;
}
这是一个简单的程序,它说明了两个函数 — init_array()
在问题中的代码(或多或少)创建数组后初始化数组,print_array()
打印值在数组中。除了合理的职责分离外,这两个函数还表明 init_array()
中设置的内容在 print_array()
函数中可见。还有一个小函数 free_array()
,made static inline
,它释放为数组分配的数据——它处理部分和完全分配的数组。除了 main()
之外的所有函数都标记为 static
,因为没有其他文件需要查看任何函数。请注意,为了便于测试,我将 N_MAX
设置为 8,而不是 300。现在我知道它正在工作,将 N_MAX
设置为 300 是可行的,也许是明智的。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static void print_array(int **base);
static void init_array(int **base);
enum { N_MAX = 8 };
static inline void free_array(int n_rows, int **array)
{
for (int i = 0; i < n_rows; i++)
free(array[i]);
free(array);
}
int main(void)
{
int i;
int **Bord = NULL;
srand(time(0)); // Not good, but better than nothing
Bord = malloc(N_MAX * sizeof(*Bord));
if (Bord == NULL)
{
fprintf(stderr, "Error while allocating %zu bytes of memory\n", N_MAX * sizeof(*Bord));
return -1;
}
for (i = 0; i < N_MAX; i++)
{
Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
if (Bord[i] == NULL)
{
fprintf(stderr, "Error while allocating %zu bytes of memory\n", N_MAX * sizeof(*Bord[i]));
free_array(i, Bord);
return -1;
}
}
init_array(Bord);
print_array(Bord);
free_array(N_MAX, Bord);
return 0;
}
static void init_array(int **base)
{
for (int i = 0; i < N_MAX; i++)
{
for (int j = 0; j < N_MAX; j++)
base[i][j] = (100 * (i + 1)) + (10 * (j + 1)) + rand() % 10;
}
}
static void print_array(int **base)
{
for (int i = 0; i < N_MAX; i++)
{
printf("[%d]:", i);
for (int j = 0; j < N_MAX; j++)
printf(" %3d", base[i][j]);
putchar('\n');
}
}
请注意,它使用 time()
来初始化随机数生成器,因此它通常应该在每次调用时生成一组新的数字,除非您在一秒钟内多次调用它。
[0]: 115 121 137 142 159 166 175 181
[1]: 211 224 239 248 253 265 277 283
[2]: 316 320 337 349 357 364 376 380
[3]: 419 428 439 448 451 469 476 484
[4]: 511 527 534 544 558 569 578 585
[5]: 616 623 631 647 650 664 671 688
[6]: 710 729 739 748 759 766 779 783
[7]: 817 824 839 847 850 860 878 881