A problem with alloca()

bearophile bearophileHUGS at lycos.com
Thu Jul 17 07:13:56 PDT 2008


This exact code (coming from a not nice debugging session):

import std.c.stdlib: alloca;
void main() {
    const int n = 8;
    for (int i; i < 2; i++)
        printf("%p\n", alloca(n));
}

Prints me two times the same address, is this a bug of Phobos/DMD, or am I doing something wrong?

If I remove the const from n, it prints two different addresses, correctly:

import std.c.stdlib: alloca;
void main() {
    int n = 8;
    for (int i; i < 2; i++)
        printf("%p\n", alloca(n));
}

I have tested it with DMD 1.029 and 1.033.

-------------------

This is unrelated.
The following code seems to work on Linux, while with DMD on Win it faults after printing "one".
But if I use the "phobos hack" (not adding -g in the compilation phase) it works correctly on Win too:

import std.c.stdio: puts;
alias int[16] jmp_buf; // int[16] comes from MinGW
extern (C) int setjmp(jmp_buf env);
extern (C) void longjmp(jmp_buf env, int value);
jmp_buf jmpbuf;
void func() {
    longjmp(jmpbuf, 1);
}
void main() {
    if (!setjmp(jmpbuf)) {
        puts("one");
        func();
    } else {
        puts("two");
    }
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list