View mode: basic / threaded / horizontal-split · Log in · Help
March 01, 2011
About const and C functions
Do you know why DMD doesn't give a compilation error here?


import core.stdc.stdio: sscanf;
immutable int value = 5;
void main() {
   sscanf("10".ptr, "%d".ptr, &value);
}

Bye,
bearophile
March 02, 2011
Re: About const and C functions
On 3/1/2011 2:33 PM, bearophile wrote:
> Do you know why DMD doesn't give a compilation error here?
>
>
> import core.stdc.stdio: sscanf;
> immutable int value = 5;
> void main() {
>      sscanf("10".ptr, "%d".ptr,&value);
> }
>
> Bye,
> bearophile

I'm not sure that's checkable.  I think this falls squarely into the 
realm of "undefined behavior".
March 02, 2011
Re: About const and C functions
Am 01.03.2011, 23:33 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:

> Do you know why DMD doesn't give a compilation error here?
>
>
> import core.stdc.stdio: sscanf;
> immutable int value = 5;
> void main() {
>     sscanf("10".ptr, "%d".ptr, &value);
> }
>

What's the D signature of sscanf?
March 02, 2011
Re: About const and C functions
On 02/03/2011 08:56, Trass3r wrote:
> Am 01.03.2011, 23:33 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:
>
>> Do you know why DMD doesn't give a compilation error here?
>>
>>
>> import core.stdc.stdio: sscanf;
>> immutable int value = 5;
>> void main() {
>> sscanf("10".ptr, "%d".ptr, &value);
>> }
>>
>
> What's the D signature of sscanf?

void* after the format arg...

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
March 02, 2011
Re: About const and C functions
On 02/03/2011 11:28, Simon wrote:
> On 02/03/2011 08:56, Trass3r wrote:
>> Am 01.03.2011, 23:33 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:
>>
>>> Do you know why DMD doesn't give a compilation error here?
>>>
>>>
>>> import core.stdc.stdio: sscanf;
>>> immutable int value = 5;
>>> void main() {
>>> sscanf("10".ptr, "%d".ptr, &value);
>>> }
>>>
>>
>> What's the D signature of sscanf?
>
> void* after the format arg...
>

Rather it's the ellipses, which is effectively void*

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
March 02, 2011
Re: About const and C functions
Bekenn:

> I'm not sure that's checkable.  I think this falls squarely into the
> realm of "undefined behavior".

The signature of sscanf is something like:
int sscanf(char* str, char* format, ...);

Can't D/DMD err on the side of safety and consider the C-style variadic argument as not const, and so produce an error if you give to them something that's D const/immutable (and require a cast there)? (Especially a function like sscanf where the third and successive arguments are known to be modified).

Bye,
bearophile
March 02, 2011
Re: About const and C functions
On 3/2/11 4:06 AM, bearophile wrote:
>
> Can't D/DMD err on the side of safety and consider the C-style variadic argument as not const, and so produce an error if you give to them something that's D const/immutable (and require a cast there)? (Especially a function like sscanf where the third and successive arguments are known to be modified).
>

With an annotation on the function signature, perhaps, and certainly it 
should do this at all times in code marked @safe.
Top | Discussion index | About this forum | D home