在堆栈上声明内存会覆盖先前声明的内存

Declaring memory on stack overwrites previously declared memory

如何在堆栈上分配内存并使其指向不同的内存地址以便稍后使用?例如。此代码:

for (int i = 0; i < 5; i++) {
    int nums[5];
    nums[0] = 1;
    printf("%p\n", &nums[0]);
}

每次都会打印出相同的地址。我如何将内存写入堆栈(不是堆,没有 malloc)并且不覆盖堆栈上已经存在的其他内容。

我相信您正在寻找:

a way to control how memory is being allocated on the stack, at least in the context of not overwriting already-used memory

当然,这由 OS 负责!低级系统调用将确保新创建的自动变量不会写入已使用的内存块。

在你的例子中:

for (int i = 0; i < 5; i++) {
  int nums[5];
  ...
}

这是不是的情况,因为nums超出范围,当第i次迭代for 循环终止。

因此,内存块nums在第一次迭代时被存储,在第二次迭代开始时将被标记为空闲,这意味着当第一次迭代的nums是将在堆栈中分配,它不会知道第一次迭代的 nums 是否存在,因为它已经超出范围 - 它不存在!

您可以使用 alloca 为循环中的每次迭代从运行时堆栈分配不同的数组。数组内容将保持有效,直到您退出函数:

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

void function() {

    for (int i = 0; i < 5; i++) {
        int *nums = alloca(5 * sizeof(*nums));
        nums[0] = 1;
        printf("%p\n", (void *)nums);
        /* store the value of `num` so the array can be used elsewhere.
         * the arrays must only be used before `function` returns to its caller.
         */
        ...
    }
    /* no need to free the arrays */
}

但是请注意,alloca() 不是 C 标准的一部分,可能并非在所有体系结构上都可用。关于如何使用它还有更多限制,请参阅您系统的文档。