C中动态分配数组的数组
我并不真正理解 C 中的一些基本知识,例如动态分配数组的数组。 我知道你可以这样做:
int **m;
为了声明一个二维数组(随后将使用某些 *alloc 函数进行分配)。 此外,还可以通过执行 *(*(m + line) + column)
来“轻松”访问它。 但是我应该如何为该数组中的元素赋值呢? 使用 gcc,以下语句 m[line][column] = 12;
因分段错误而失败。
任何文章/文档将不胜感激。 :-)
I don't truly understand some basic things in C like dynamically allocating array of arrays.
I know you can do:
int **m;
in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be "easily" accessed by doing *(*(m + line) + column)
. But how should I assign a value to an element from that array? Using gcc the following statement m[line][column] = 12;
fails with a segmentation fault.
Any article/docs will be appreciated. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
m[line][column] = 12
语法没问题(前提是line
和column
在范围内)。但是,您没有编写用于分配它的代码,因此很难确定它是错误还是正确。 它应该是
一些旁注的内容:
The
m[line][column] = 12
syntax is ok (providedline
andcolumn
are in range).However, you didn't write the code you use to allocate it, so it's hard to get whether it is wrong or right. It should be something along the lines of
Some side-notes:
您的语法 m[line][colummn] 是正确的。 但为了在 C 中使用二维数组,必须为其分配内存。 例如,此代码将为给定行和列的表分配内存。
请注意,为了简洁起见,我省略了 malloc 的错误检查。 真正的解决方案应该包括它们。
Your syntax m[line][colummn] is correct. But in order to use a 2D array in C, you must allocate memory for it. For instance this code will allocated memory for a table of given line and column.
Note, I left out the error checks for malloc for brevity. A real solution should include them.
它不是一个二维数组 - 它是一个数组的数组 - 因此它需要多次分配。
It's not a 2d array - it's an array of arrays - thus it needs the multiple allocations.
这是 quinmars 解决方案的修改版本,它仅分配单个内存块,并且可以通过
void *
与通用值一起使用:我不确定将
void **
转换为int 是否真的安全**
(我认为标准允许在与void *
进行转换时进行转换?),但它可以在 gcc 中工作。 为了安全起见,您应该将每次出现的void *
替换为int *
...先前算法的类型安全版本:
以下宏实现了 他们像这样:
Here's a modified version of quinmars' solution which only allocates a single block of memory and can be used with generic values by courtesy of
void *
:I'm not sure if it's really safe to cast
void **
toint **
(I think the standard allows for conversions to take place when converting to/fromvoid *
?), but it works in gcc. To be on the safe side, you should replace every occurence ofvoid *
withint *
...The following macros implement a type-safe version of the previous algorithm:
Use them like this:
尽管我同意其他答案,但在大多数情况下最好立即分配整个数组,因为 malloc 非常慢。
要释放数组只需执行以下操作:
自由(*数组); 自由(数组);
注意:此解决方案仅在您不想更改行的顺序时才有效,因为您可能会丢失第一个元素的地址,稍后您需要释放该数组。
Although I agree with the other answers, it is in most cases better to allocate the whole array at once, because malloc is pretty slow.
To free the array simply do:
free(*array); free(array);
Note: this solution only works if you don't want to change the order of the rows, because you could then lose the address of the first element, which you need to free the array later.
嗯。 老式时尚烟雾和镜子作为一种选择怎么样?
Humm. How about old fashion smoke and mirrors as an option?
使用
malloc(3)
分配第一个数组并放入由malloc(3)
创建的指针应该与array[r][c] 因为它应该等价于
*(*(array + r) + c)
,它是 C 标准中的。Using
malloc(3)
for allocate the first array and putting in there pointers created bymalloc(3)
should work witharray[r][c]
because it should be equivalent to*(*(array + r) + c)
, it is in the C standard.