Help passing D strings to C libs
Jonathan M Davis
jmdavisProg at gmx.com
Mon Mar 14 11:14:48 PDT 2011
On Monday, March 14, 2011 10:54:57 Trass3r wrote:
> > I'm having trouble passing D strings (char[]) to SDL, in particular
> > SDL_LoadBMP(), I keep receiving a segfault.
> >
> > Heres the code:
> >
> > void setImg(string path) {
> >
> > // concat null terminating char to string and cast to c type string
> >
> > when
> >
> > // passing to SDL_LoadBMP()
> > path ~= "\0";
> > image = SDL_LoadBMP( cast(char*)path );
> >
> > }
> >
> > and the value of path is "./resources/cannon.bmp"
> >
> > I'm using SDL 1.2.14 and DMD 1.067 on Ubuntu 10.10
>
> Well strings are put into read-only space on Linux and I guess this is
> also the case for D1.
> Since you are doing a ~= it probably tries to alter that value and crashes.
No. If it can't concatenate in place, then it will reallocate the array. The
fact that it was in read-only memory is irrelevant. It just means that
reallocation is guaranteed instead of being only possible.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list