D - elementary problem?
- Berin Loritsch (6/6) Dec 02 2003 I have a code snippet that compiles fine but does not run fine:
- Berin Loritsch (5/16) Dec 02 2003 What is the difference between printf("Hello: %s", onOff) and
- Mark J. Brudnak (5/21) Dec 02 2003 Try the explicit cast to something that printf can handle.
- Berin Loritsch (5/9) Dec 02 2003 Hmm. It seems rather clumsy if you consider that printf() is the
- Vathix (6/15) Dec 02 2003 char[] will implicitly convert to char* if it knows it should. But since...
- Berin Loritsch (3/19) Dec 02 2003 J Anderson wrote:
- Ant (18/27) Dec 02 2003 There is a long discussion on it!
- Mark J. Brudnak (13/43) Dec 02 2003 So the printf man page says that .* means that the length preceeds the
- Ant (6/8) Dec 02 2003 You are absolutly right.
- Matthew Wilson (15/23) Dec 02 2003 Ideally there could be a simple amendment of printf to provide a D-strin...
- J C Calvarese (4/42) Dec 02 2003 This might help explain...
- Ant (5/9) Dec 02 2003 yes, but the problem is not the information it self,
- J C Calvarese (8/24) Dec 02 2003 I completely agree.
- Matthew Wilson (5/29) Dec 02 2003 What about if, during the pre-1.0 phase of the compiler, or in debug bui...
- J Anderson (7/16) Dec 02 2003 Not really. Printf is a library specification. If it was to be taken
- Matthew Wilson (4/24) Dec 02 2003 This is a mistake. If the null-terminator is missing or not right after ...
- J Anderson (5/21) Dec 02 2003 The difference is that %s is the C format (zero terminated) and %.*s is
- J Anderson (18/24) Dec 02 2003 Runs fine for me.
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
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
"Berin Loritsch" <bloritsch d-haven.org> wrote in message news:bqiuli$3053$1 digitaldaemon.com...Berin Loritsch wrote:Try the explicit cast to something that printf can handle. printf("Hello: %s", (char *)onOff) ; MarkI 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
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
"Berin Loritsch" <bloritsch d-haven.org> wrote in message news:bqivkr$b5$1 digitaldaemon.com...Mark J. Brudnak wrote: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.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
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...J Anderson wrote: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.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
In article <bqivkr$b5$1 digitaldaemon.com>, Berin Loritsch says...Mark J. Brudnak wrote: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". AntTry 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
"Ant" <Ant_member pathlink.com> wrote in message news:bqj0sl$2bs$1 digitaldaemon.com...In article <bqivkr$b5$1 digitaldaemon.com>, Berin Loritsch says...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.Mark J. Brudnak wrote: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.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.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
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
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...ThatIt seems that there is some violation of sound coding principles here.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
Ant wrote:In article <bqivkr$b5$1 digitaldaemon.com>, Berin Loritsch says...This might help explain... http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#RuntimeErrors JustinMark J. Brudnak wrote: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?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.Walter, maybe the page should be called "newcomers read this" or "D for C/C++ programers". Ant
Dec 02 2003
In article <bqj6sc$alc$1 digitaldaemon.com>, J C Calvarese says...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. AntWalter 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
Dec 02 2003
Ant wrote:In article <bqj6sc$alc$1 digitaldaemon.com>, J C Calvarese says...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). Justinyes, 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. AntWalter 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
Dec 02 2003
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...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). Justinyes, 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. AntWalter 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
Dec 02 2003
Berin Loritsch wrote:Mark J. Brudnak wrote: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.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
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) ;Try the explicit cast to something that printf can handle. printf("Hello: %s", (char *)onOff) ;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
Berin Loritsch wrote:Berin Loritsch 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).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
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