www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A problem with alloca()

reply bearophile <bearophileHUGS lycos.com> writes:
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
Jul 17 2008
parent Sean Kelly <sean invisibleduck.org> writes:
bearophile wrote:
 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.
Dunno, but this sounds like a compiler bug to me.
 -------------------
 
 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");
     }
 }
setjmp and longjmp with DMD/Win32 are broken. I had no idea it was possible to get these working at all. Sean
Jul 17 2008