在同一行上使用 malloc 创建两个数组

gal*_*ois 3 c c++ memory arrays malloc

在 C/C++ 中,您可以通过以下语法将一组变量初始化为相同的值: a = b = c = 1;

这会起作用malloc吗?IE 类似的东西:

char *a, *b;
a = b = malloc(SOME_SIZE * sizeof(char));
Run Code Online (Sandbox Code Playgroud)

这会创建两个相同大小的数组,但每个数组都有自己的内存吗?或者它会将两个数组分配到地址空间中的同一个位置?

Abh*_*eet 5

如果您将多个分配行分解为单个分配行,您的解决方案将变得更加清晰。

a = b = malloc(SOME_SIZE * sizeof(char));
Run Code Online (Sandbox Code Playgroud)

或者

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.
Run Code Online (Sandbox Code Playgroud)