小编Phr*_*dge的帖子

可以重新分配alloca()内存吗?

所分配的内存malloc可以使用重新分配realloc。有类似的功能alloca吗?当您不希望在堆上分配内存时,并且您需要多次分配可变的堆栈内存(例如在需要动态内存但又不想动态存储的库函数中)时,重新分配堆栈内存可能很有用。在堆上分配,因为库的用户可能使用自定义堆分配策略。它看起来像这样:

int main(void) {
    float * some_mem = alloca(40 * sizeof(float));
    // do something with this memory...

    // now we need a different amount of memory, but some_mem still occupies a lot of the stack, so just reallocate it.

    // is something like this possible?
    some_mem = realloca(some_mem, 50 * sizeof(float));
}
Run Code Online (Sandbox Code Playgroud)

重要的是,这一切都发生在堆栈上。问:有没有办法重新分配动态堆栈内存?

c memory-management realloc alloca

6
推荐指数
1
解决办法
142
查看次数

编译器/解释器中的符号前瞻

在为简单的编程语言构建某种解释器时,我偶然发现了一个有趣的问题。我称之为“符号前瞻”问题。

我这是什么意思?例如,在 C/C++ 编译器中,您将要使用的符号必须始终已在代码上方的某处声明。像这样:

struct int_pair;

struct rectangle {
    int_pair position;
    int_pair size;
};

struct int_pair {
    int x, y;
};
Run Code Online (Sandbox Code Playgroud)

而不是这样的:

struct rectangle {
    int_pair position;
    int_pair size;
};

struct int_pair {
    int x, y;
};
Run Code Online (Sandbox Code Playgroud)

在 C# 或 Java 中,可以在文件中的任意位置使用任何符号:

public class Rectangle {
    private IntPair position, size; // using IntPair before declaring it
}

public class IntPair {
    public int Sum() { // using x and y before declaring it
        return x + y;
    }

    public int …
Run Code Online (Sandbox Code Playgroud)

c compiler-construction interpreter

-2
推荐指数
1
解决办法
240
查看次数