www.digitalmars.com         C & C++   DMDScript  

D - elementary problem?

reply Berin Loritsch <bloritsch d-haven.org> writes:
I have a code snippet that compiles fine but does not run fine:

     char[] onOff = ((fullScreen) ? "on" : "off");

When I run it, I get

$ ./hello
Full Screen: Error: Access Violation

So what am I missing?
Dec 02 2003
next sibling parent reply Berin Loritsch <bloritsch d-haven.org> writes:
Berin Loritsch wrote:

 I have a code snippet that compiles fine but does not run fine:
 
     char[] onOff = ((fullScreen) ? "on" : "off");
 
 When I run it, I get
 
 $ ./hello
 Full Screen: Error: Access Violation
 
 So what am I missing?
 
What is the difference between printf("Hello: %s", onOff) and printf("Hello: %.*s", onOff)? Why is it that I get an Access Violation from the first format and not the second?
Dec 02 2003
next sibling parent reply "Mark J. Brudnak" <mjbrudna oakland.edu> writes:
"Berin Loritsch" <bloritsch d-haven.org> wrote in message
news:bqiuli$3053$1 digitaldaemon.com...
 Berin Loritsch wrote:

 I have a code snippet that compiles fine but does not run fine:

     char[] onOff = ((fullScreen) ? "on" : "off");

 When I run it, I get

 $ ./hello
 Full Screen: Error: Access Violation

 So what am I missing?
What is the difference between printf("Hello: %s", onOff) and printf("Hello: %.*s", onOff)? Why is it that I get an Access Violation from the first format and not the second?
Try the explicit cast to something that printf can handle. printf("Hello: %s", (char *)onOff) ; Mark
Dec 02 2003
next sibling parent reply Berin Loritsch <bloritsch d-haven.org> writes:
Mark J. Brudnak wrote:

 
 Try the explicit cast to something that printf can handle.
 
 printf("Hello: %s", (char *)onOff) ;
Hmm. It seems rather clumsy if you consider that printf() is the preferred way to send formatted text to the screen. Can't this particular cast be taken care of by the language? It would make it alot easier to work with.
Dec 02 2003
next sibling parent reply "Vathix" <vathix dprogramming.com> writes:
"Berin Loritsch" <bloritsch d-haven.org> wrote in message
news:bqivkr$b5$1 digitaldaemon.com...
 Mark J. Brudnak wrote:

 Try the explicit cast to something that printf can handle.

 printf("Hello: %s", (char *)onOff) ;
Hmm. It seems rather clumsy if you consider that printf() is the preferred way to send formatted text to the screen. Can't this particular cast be taken care of by the language? It would make it alot easier to work with.
char[] will implicitly convert to char* if it knows it should. But since the types of the variable arguments aren't known at compile time, it has to pass the type that it is. When dealing with D strings, it's just much simpler to use %.*s instead of null-terminated strings.
Dec 02 2003
parent Berin Loritsch <bloritsch d-haven.org> writes:
Oh....  Ok.  (I'd still like to not worry about it though...)


Vathix wrote:

 "Berin Loritsch" <bloritsch d-haven.org> wrote in message
 news:bqivkr$b5$1 digitaldaemon.com...
 
Hmm.  It seems rather clumsy if you consider that printf() is the
preferred way to send formatted text to the screen.  Can't this
particular cast be taken care of by the language?  It would make
it alot easier to work with.
char[] will implicitly convert to char* if it knows it should. But since the types of the variable arguments aren't known at compile time, it has to pass the type that it is. When dealing with D strings, it's just much simpler to use %.*s instead of null-terminated strings.
J Anderson wrote:
 The difference is that %s is the C format (zero terminated) and %.*s is
 like the D format (length followed by the characters).  printf is a
 relic of C that is why you have to use %.*s (which is rarly used/needed
 in C).
Dec 02 2003
prev sibling next sibling parent reply Ant <Ant_member pathlink.com> writes:
In article <bqivkr$b5$1 digitaldaemon.com>, Berin Loritsch says...
Mark J. Brudnak wrote:

 
 Try the explicit cast to something that printf can handle.
 
 printf("Hello: %s", (char *)onOff) ;
Hmm. It seems rather clumsy if you consider that printf() is the preferred way to send formatted text to the screen. Can't this particular cast be taken care of by the language? It would make it alot easier to work with.
There is a long discussion on it! actully more then one discussion. The jury is still out (and some of the proposed solutions are kinda of uggly). D strings are just arrays with the lenght first and not null terminated. printf is just the C printf that does know anything about D. so %s is bound to try to access all your computer memory until it finds a null. as you probably know %.*s gets the lenght to print from the parameters. Walter promissed to create a FAQ on this, as most newcomers fall on that trap, did you look on the FAQs? is it there yet? Walter, maybe the page should be called "newcomers read this" or "D for C/C++ programers". Ant
Dec 02 2003
next sibling parent reply "Mark J. Brudnak" <mjbrudna oakland.edu> writes:
"Ant" <Ant_member pathlink.com> wrote in message
news:bqj0sl$2bs$1 digitaldaemon.com...
 In article <bqivkr$b5$1 digitaldaemon.com>, Berin Loritsch says...
Mark J. Brudnak wrote:

 Try the explicit cast to something that printf can handle.

 printf("Hello: %s", (char *)onOff) ;
Hmm. It seems rather clumsy if you consider that printf() is the preferred way to send formatted text to the screen. Can't this particular cast be taken care of by the language? It would make it alot easier to work with.
There is a long discussion on it! actully more then one discussion. The jury is still out (and some of the proposed solutions are kinda of uggly). D strings are just arrays with the lenght first and not null terminated. printf is just the C printf that does know anything about D. so %s is bound to try to access all your computer memory until it finds a null. as you probably know %.*s gets the lenght to print from the parameters.
So the printf man page says that .* means that the length preceeds the string. D encodes char[] as: struct { int length ; char * string ; } This struct gets pushed onto the stack as a parameter in the varargs way. We then hope that printf() interprets this as an int and a pointer. Is that right? It seems that there is some violation of sound coding principles here. That is do not rely on the internal implementation of an object.
 Walter promissed to create a FAQ on this, as most newcomers
 fall on that trap, did you look on the FAQs? is it there yet?

 Walter, maybe the page should be called
 "newcomers read this" or "D for C/C++ programers".

 Ant
Dec 02 2003
parent reply Ant <Ant_member pathlink.com> writes:
In article <bqj2qs$56t$1 digitaldaemon.com>, Mark J. Brudnak says...

It seems that there is some violation of sound coding principles here.  That
is do not rely on the internal implementation of an object.
You are absolutly right. The thing is that D is in developement, this is temporary. I believe that is stated on the D documentation. Discussion with proposals are live on this ng. Ant
Dec 02 2003
parent "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
Ideally there could be a simple amendment of printf to provide a D-string
format specifier, e.g. "%S" (maybe that's already used <g>?).

However, this would mean that D would be imposing behaviour on any
underlying CRT. I don't really have a problem with this, and any
implementation that cannot rely on such behaviour from any underlying CRT
could have an intermediate runtime translation of %S to %.*s

One way around this would be to mandate that D compilers do such a
translation, but there's always the possibility that someone might compose
format strings dynamically.

It's a tricky one alright.

:)

Matthew

"Ant" <Ant_member pathlink.com> wrote in message
news:bqj403$6so$1 digitaldaemon.com...
 In article <bqj2qs$56t$1 digitaldaemon.com>, Mark J. Brudnak says...

It seems that there is some violation of sound coding principles here.
That
is do not rely on the internal implementation of an object.
You are absolutly right. The thing is that D is in developement, this is temporary. I believe that is stated on the D documentation. Discussion with proposals are live on this ng. Ant
Dec 02 2003
prev sibling parent reply J C Calvarese <jcc7 cox.net> writes:
Ant wrote:
 In article <bqivkr$b5$1 digitaldaemon.com>, Berin Loritsch says...
 
Mark J. Brudnak wrote:


Try the explicit cast to something that printf can handle.

printf("Hello: %s", (char *)onOff) ;
Hmm. It seems rather clumsy if you consider that printf() is the preferred way to send formatted text to the screen. Can't this particular cast be taken care of by the language? It would make it alot easier to work with.
There is a long discussion on it! actully more then one discussion. The jury is still out (and some of the proposed solutions are kinda of uggly). D strings are just arrays with the lenght first and not null terminated. printf is just the C printf that does know anything about D. so %s is bound to try to access all your computer memory until it finds a null. as you probably know %.*s gets the lenght to print from the parameters. Walter promissed to create a FAQ on this, as most newcomers fall on that trap, did you look on the FAQs? is it there yet?
This might help explain... http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#RuntimeErrors Justin
 
 Walter, maybe the page should be called
 "newcomers read this" or "D for C/C++ programers".
 
 Ant
 
 
Dec 02 2003
parent reply Ant <Ant_member pathlink.com> writes:
In article <bqj6sc$alc$1 digitaldaemon.com>, J C Calvarese says...
 Walter promissed to create a FAQ on this, as most newcomers
 fall on that trap, did you look on the FAQs? is it there yet?
This might help explain... http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#RuntimeErrors
yes, but the problem is not the information it self, this is explained over and over. The problem is getting that information to the people that needs it. Ant
Dec 02 2003
parent reply J C Calvarese <jcc7 cox.net> writes:
Ant wrote:

 In article <bqj6sc$alc$1 digitaldaemon.com>, J C Calvarese says...
 
Walter promissed to create a FAQ on this, as most newcomers
fall on that trap, did you look on the FAQs? is it there yet?
This might help explain... http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#RuntimeErrors
yes, but the problem is not the information it self, this is explained over and over. The problem is getting that information to the people that needs it. Ant
I completely agree. I think it might help if Wiki4D (http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage) was featured at a more prominent spot on the Digital Mars website. It's not perfect, but I think it has the most comprehensive collection of "Welcome to D" info (other than the Digital Mars website itself, of course). Justin
Dec 02 2003
parent "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
What about if, during the pre-1.0 phase of the compiler, or in debug builds,
compiler errors and the default runtime catch, all printed the URL of the D
information page on dm.com?

"J C Calvarese" <jcc7 cox.net> wrote in message
news:bqj946$e8c$1 digitaldaemon.com...
 Ant wrote:

 In article <bqj6sc$alc$1 digitaldaemon.com>, J C Calvarese says...

Walter promissed to create a FAQ on this, as most newcomers
fall on that trap, did you look on the FAQs? is it there yet?
This might help explain... http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#RuntimeErrors
yes, but the problem is not the information it self, this is explained over and over. The problem is getting that information to the people that needs it. Ant
I completely agree. I think it might help if Wiki4D (http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage) was featured at a more prominent spot on the Digital Mars website. It's not perfect, but I think it has the most comprehensive collection of "Welcome to D" info (other than the Digital Mars website itself, of course). Justin
Dec 02 2003
prev sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Berin Loritsch wrote:

 Mark J. Brudnak wrote:

 Try the explicit cast to something that printf can handle.

 printf("Hello: %s", (char *)onOff) ;
Hmm. It seems rather clumsy if you consider that printf() is the preferred way to send formatted text to the screen. Can't this particular cast be taken care of by the language? It would make it alot easier to work with.
Not really. Printf is a library specification. If it was to be taken care of the by the d compiler, you would either need to covert it to a C string (inefficient) or change over to (IMHO) ugly c strings. You should use %.*s, that cast is bad practice. I think something like %S (capitals) should be added to the d specification of printf, which is a bit easier then %.*s.
Dec 02 2003
prev sibling parent "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
 I have a code snippet that compiles fine but does not run fine:

     char[] onOff = ((fullScreen) ? "on" : "off");

 When I run it, I get

 $ ./hello
 Full Screen: Error: Access Violation

 So what am I missing?
What is the difference between printf("Hello: %s", onOff) and printf("Hello: %.*s", onOff)? Why is it that I get an Access Violation from the first format and not the second?
Try the explicit cast to something that printf can handle. printf("Hello: %s", (char *)onOff) ;
This is a mistake. If the null-terminator is missing or not right after the end of the current slice you will get unexpected results. The way to do it is printf("Hello: %.*s", onOff) ;
Dec 02 2003
prev sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Berin Loritsch wrote:

 Berin Loritsch wrote:

 I have a code snippet that compiles fine but does not run fine:

     char[] onOff = ((fullScreen) ? "on" : "off");

 When I run it, I get

 $ ./hello
 Full Screen: Error: Access Violation

 So what am I missing?
What is the difference between printf("Hello: %s", onOff) and printf("Hello: %.*s", onOff)? Why is it that I get an Access Violation from the first format and not the second?
The difference is that %s is the C format (zero terminated) and %.*s is like the D format (length followed by the characters). printf is a relic of C that is why you have to use %.*s (which is rarly used/needed in C).
Dec 02 2003
prev sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Berin Loritsch wrote:

 I have a code snippet that compiles fine but does not run fine:

     char[] onOff = ((fullScreen) ? "on" : "off");

 When I run it, I get

 $ ./hello
 Full Screen: Error: Access Violation

 So what am I missing?
Runs fine for me. -- import c.stdio; int main( char [] [] args ) { bool fullscreen = false; printf("hi\n"); char[] onOff = ((fullscreen) ? "on" : "off"); printf(onOff); printf("\nbye"); while(1) { }; return 1; }
Dec 02 2003