Calling C function with static array includes length and pointer

Ali Çehreli acehreli at yahoo.com
Mon Jun 21 14:38:50 PDT 2010


mwarning wrote:
> Hi,
> 
> I try to pass a static array to a variadic C function.
> Looks like the array is passed by values as expected,
> but the length and pointer are prepended, too.
> Is this intentional or a bug?
> 
> http://pastebin.com/6ejFF37j

Fixed sized arrays don't have ptr and length members:

import std.stdio;

void main()
{
     int[5] array;
     writeln(array.sizeof);           // prints 20
     writeln(array.ptr, ' ', &array); // prints the save value
}

The code you've pasted has this:

extern(C) void* foo(uint x, ...)
{
     Stdout("x: ")(x).nl;
     Stdout("y: ")(*(&x + 1)).nl; //outputs 64!
     return null;
}

The expression *(&x + 1) is undefined behavior, because (&x + 1) is not 
valid, i.e. not specified to be accessible by the language.

Ali


More information about the Digitalmars-d mailing list