在 C/C++ 中,您可以通过以下语法将一组变量初始化为相同的值:a = b = c = 1;
这对 malloc
有用吗? IE。类似的东西:
char *a, *b;
a = b = malloc(SOME_SIZE * sizeof(char));
这会创建两个相同大小的数组,但每个数组都有自己的内存吗?或者它将两个数组分配给地址空间中的同一个位置?
最佳答案
如果您将多个分配行分解为单个分配行,解决方案对您来说会变得更加清晰。
a = b = malloc(SOME_SIZE * sizeof(char));
或
b = malloc(SOME_SIZE * sizeof(char)); // b points to the alloced memory
a = b; // a got the value of b i.e., a points to where b is pointing.
关于c++ - 在同一行上使用 malloc 创建两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30659629/