简体   繁体   English

“实”数组指针和 malloc

[英]“Real” Array pointer and malloc

So I was just playing around with Array pointers to see how they work, my example below allocates space for 2 array pointers to an array with 10 ints.所以我只是在玩数组指针来看看它们是如何工作的,我下面的示例为 2 个数组指针分配空间,指向一个 10 个整数的数组。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int (*p)[10] = malloc(sizeof(int[10]) * 2);

    for (int arrayI = 0; arrayI < 2; ++arrayI) {
        for (int i = 0; i < 10; ++i) {
            p[arrayI][i] = (arrayI+1) * i;
        }
    }

    for (int arrayI = 0; arrayI < 2; ++arrayI) {
        for (int i = 0; i < 10; ++i) {
            printf("%d\n", p[arrayI][i]);
        }
        printf("\n");
    }
}

This seems to work fine and gives me:这似乎工作正常并给了我:

C:\Users\USERNAME\Desktop>gcc -Wall -Wextra --std=c18 a.c && a.exe
0
1
2
3
4
5
6
7
8
9

0
2
4
6
8
10
12
14
16
18

For my question, you rarely see code like this, if at all.对于我的问题,你很少看到这样的代码,如果有的话。 Is there anything dangerous with doing things like this or is it just "bad code".做这样的事情有什么危险还是只是“糟糕的代码”。 And again, this is just me playing around with Array pointers.再说一次,这只是我在玩数组指针。

Is there anything dangerous with doing things like this or is it just "bad code".做这样的事情有什么危险还是只是“糟糕的代码”。

No. This is actually a correct way of making a dynamic allocated 2D array.不,这实际上是制作动态分配的二维数组的正确方法。

Instead of代替

int (*p)[10] = malloc(sizeof(int[10]) * 2);

I prefer我更喜欢

int (*p)[10] = malloc(2 * sizeof *p);

As p is a pointer to an array of 10 ints, *p is an array of 10 ints.因为p是一个指向 10 个整数数组的指针,所以*p是一个 10 个整数数组。

Many prefer to have an explicit release of memory.许多人更喜欢明确发布 memory。 It's not required as the program terminates anyway.这不是必需的,因为程序无论如何都会终止。 It's opinion based but I would add:这是基于意见的,但我会补充:

free(p);
return 0;

at the end of the program.在程序结束时。

my example below allocates space for 2 array pointers to an array with 10 ints我下面的示例为 2 个数组指针分配空间,这些指针指向一个具有 10 个整数的数组

No, this is one array pointer int (*p)[10] .不,这是一个数组指针int (*p)[10] This gives 2 arrays: sizeof(int[10]) * 2 .这给出了 2 arrays: sizeof(int[10]) * 2

Is there anything dangerous with doing things like this做这样的事情有什么危险吗

The only danger is the obscure de-referencing syntax.唯一的危险是晦涩的取消引用语法。 The most correct way to write your example is this:编写示例的最正确方法是:

 int (*p)[2][10] = malloc(sizeof(int[2][10]));

or equivalent或同等学历

 int (*p)[2][10] = malloc(sizeof *p);

But if you do that, you have to write the strange-looking (*p)[i][j] when de-referencing.但是如果你这样做,你必须在取消引用时写出看起来很奇怪的(*p)[i][j] Therefore we drop the left-most dimension, a "manual array decay", and get a int (*)[10] which is actually a pointer to the first element in a int[2][10] array.因此,我们删除最左边的维度,即“手动数组衰减”,并得到一个int (*)[10] ,它实际上是指向int[2][10]数组中第一个元素的指针。 If you want, you could also rewrite the code as:如果需要,您还可以将代码重写为:

int (*arr)[2][10] = malloc(sizeof *arr);
int (*p)[10] = *arr;

Also, you should always free the allocated memory, since free will often result in a crash in case you have any lurking pointer corruption bugs somewhere in the program.此外,您应该始终free已分配的 memory,因为如果您在程序中的某个地方有任何潜伏的指针损坏错误, free通常会导致崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM