www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - About const and C functions

reply bearophile <bearophileHUGS lycos.com> writes:
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
Mar 01 2011
next sibling parent reply Bekenn <leaveme alone.com> writes:
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".
Mar 01 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Mar 02 2011
parent Bekenn <leaveme alone.com> writes:
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.
Mar 02 2011
prev sibling parent reply Trass3r <un known.com> writes:
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?
Mar 02 2011
parent reply Simon <s.d.hammett gmail.com> writes:
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
Mar 02 2011
parent Simon <s.d.hammett gmail.com> writes:
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
Mar 02 2011