extern (C) function call with const char * type will sometimes generate seg fault or core dump.

dysmondad via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 16 21:31:13 PDT 2014


On Thursday, 16 October 2014 at 07:55:24 UTC, Mike Parker wrote:
> On 10/16/2014 4:54 PM, Mike Parker wrote:
>> On 10/16/2014 12:18 PM, dysmondad wrote:
>>> Since I've added this call, my program will sometimes but not 
>>> always
>>> either generate a core dump or a seg fault. It seems that the 
>>> issue is
>>> with the const char * parameter.
>>>
>>> I don't have a good grasp of the difference between the way D 
>>> and C work
>>> for char * types.
>>
>> Strings in C are arrays of chars ending with the nul 
>> terminator. Strings
>> in D are not required to be nul terminated. String literals in 
>> D *are*
>> nul terminated. When passing a string to a C function that 
>> takes const
>> char*, use toStringz as Ali showed in his post.
>>
>>
> Forgot to mention -- toStringz in std.string will add the nul 
> terminator if it is not already there.
>
>
> ---
> This email is free from viruses and malware because avast! 
> Antivirus protection is active.
> http://www.avast.com


Thank you very much. Because of your help, my application has far 
fewer seg faults and core dumps.

Now, if I can just figure out why the SDL_RenderPresent call is 
having problems. I suspect that has more to do with the way I'm 
using the library than the calling convention.


More information about the Digitalmars-d-learn mailing list