在一个项目中,有人推动了这一行:
double (*e)[n+1] = malloc((n+1) * sizeof(*e));
Run Code Online (Sandbox Code Playgroud)
据推测,这会创建一个(n + 1)*(n + 1)双倍的二维数组.
据说,我说,因为到目前为止,我没有人问我能告诉我它的作用,确切地说,它起源于何处或为什么它应该起作用(据称,它确实如此,但我还没有购买它).
也许我错过了一些明显的东西,但如果有人能够向我解释上述内容,我会很感激.因为就个人而言,如果我们使用我们真正理解的东西,我会感觉好多了.
我有以下C代码:
int *a;
size_t size = 2000*sizeof(int);
a = (int *) malloc(size);
Run Code Online (Sandbox Code Playgroud)
哪个工作正常.但如果我有以下内容:
char **b = malloc(2000*sizeof *b);
Run Code Online (Sandbox Code Playgroud)
每个元素b都有不同的长度.
怎么可能b像我一样做同样的事情a; 即以下代码是否正确?
char *c;
size_t size = 2000*sizeof(char *);
c = (char *) malloc(size);
Run Code Online (Sandbox Code Playgroud) 这个问题的目的是提供一个关于如何在C中动态正确分配多维数组的参考.这是一个经常被误解的主题,即使在一些C编程书籍中也很难解释.因此,即使是经验丰富的C程序员也很难做到正确.
我从编程教师/书籍/教程中了解到,动态分配多维数组的正确方法是使用指针指针.
然而,SO上的几个高代表用户现在告诉我这是错误和不好的做法.他们说指针到指针不是数组,我实际上并没有分配数组,而且我的代码不必要地慢.
这就是我教我分配多维数组的方法:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int** arr_alloc (size_t x, size_t y)
{
int** pp = malloc(sizeof(*pp) * x);
assert(pp != NULL);
for(size_t i=0; i<x; i++)
{
pp[i] = malloc(sizeof(**pp) * y);
assert(pp[i] != NULL);
}
return pp;
}
int** arr_fill (int** pp, size_t x, size_t y)
{
for(size_t i=0; i<x; i++)
{
for(size_t j=0; j<y; j++)
{
pp[i][j] = (int)j + 1;
}
}
return pp;
}
void arr_print (int** pp, size_t x, size_t y) …Run Code Online (Sandbox Code Playgroud) c arrays dynamic-arrays dynamic-allocation variable-length-array
昨天我发布了一个问题:我应该如何传递指向函数的指针并为被调用函数内部传递的指针分配内存?
从我得到的答案中,我能够理解我在做什么错误.
我现在面临一个新问题,任何人都可以帮忙解决这个问题吗?
我想动态分配一个2D数组,所以我将一个Pointer-to-Pointer从my main()传递给另一个调用的函数alloc_2D_pixels(...),在那里我使用malloc(...)并for(...)循环为2D数组分配内存.
好吧,从alloc_2D_pixels(...)函数返回后,指针指针仍然保持为NULL,所以很自然地,当我尝试访问或尝试free(...)指针到指针时,程序挂起.
谁能告诉我我在这里犯的错误?
救命!!!
维克拉姆
资源:
main()
{
unsigned char **ptr;
unsigned int rows, cols;
if(alloc_2D_pixels(&ptr, rows, cols)==ERROR) // Satisfies this condition
printf("Memory for the 2D array not allocated"); // NO ERROR is returned
if(ptr == NULL) // ptr is NULL so no memory was allocated
printf("Yes its NULL!");
// Because ptr is NULL, with any of these 3 statements below the program HANGS …Run Code Online (Sandbox Code Playgroud) 我想使用malloc声明一个二维数组。在互联网上查找时,所有网站都告诉您声明一个int **指针,然后使用malloc首先将各个指针分配给1d数组,然后再次使用malloc为各个int分配空间。我的怀疑是,以这种方式声明的数组不会将其元素保存在连续的内存地址中。虽然以下方法仅使用一个malloc语句并动态分配2d数组,但所有地址均根据需要是连续的。因此,以下内容不是动态分配2d数组的正确方法吗?
#include <stdio.h>
int main(){
int (*p)[2] = malloc(3 * sizeof *p);
int i;
int j;
//All addresses printed here are contiguous
for(i=0; i<3; i++){
for(j=0; j<2; j++){
printf("%d\t", &p[i][j]);
}
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud) 我malloc用来动态分配2d char数组.除非我之前将每个数组索引设置为NULL free(),否则在尝试时会出现分段错误free().为什么我会出现分段错误?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int nrows = 20; int ncolumns = 10;
char ** arr = malloc(nrows * sizeof(char *));
for(int i = 0; i < nrows; i++)
arr[i] = malloc(ncolumns * sizeof(char));
arr[0] = "string1";
arr[1] = "string2";
// does not work without the following code:
// for(int i = 0; i < 20; i++)
// arr[i] = NULL;
for(int i = 0; i < 20; i++)
free(arr[i]); …Run Code Online (Sandbox Code Playgroud)