www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - printf() access violation

reply tocy ize.co.jp writes:
Hi, all.

int main()
{
char[] testString = "test";
printf("%s\n",testString);
return 0;
}

Error: Access Violation

It seems does not work.

dmd ver 0.121
WinXP
Apr 18 2005
next sibling parent Derek Parnell <derek psych.ward> writes:
On Mon, 18 Apr 2005 09:12:01 +0000 (UTC), tocy ize.co.jp wrote:

 Hi, all.
 
 int main()
 {
 char[] testString = "test";
 printf("%s\n",testString);
 return 0;
 }
 
 Error: Access Violation
 
 It seems does not work.
This is because printf expects zero-terminated C-style strings, and in D a char[] is not the same as a C-styled string. In D, all dynamic arrays look like a struct containing two fields; a length and a pointer to the data. Thus when you pass 'testString' to printf, what gets put on the stack for printf is the struct. Thus printf gets two values - a length and a pointer. So its as if you had coded ... printf("%s\n", testString.length, testString.ptr); So it crashes because printf tries to use the length value as if it was a pointer to a C-string. The correct way to use D-strings with printf is ... printf("%.*s\n",testString); The '.*' causes printf to use the next parameter as a length and the subsequent one as the data pointer - which is what you actually passed it. However, a much better alternative is to use the writef routine instead because that understands D-strings. import std.stdio; // required to get writef. int main() { char[] testString = "test"; writef("%s\n",testString); return 0; } -- Derek Melbourne, Australia 18/04/2005 7:25:26 PM
Apr 18 2005
prev sibling parent reply jicman <jicman_member pathlink.com> writes:
I stopped using printf.  It's for c,c++ folks. ;-)  Try using writefln or
writef.

And this brings a point that I was trying to make for a while, but time is not
on my side, as of late: why do I have to do,

import std.stdio;

to get writef, etc., to work?  If these are d's environment "printers", if I may
call them that, should they be loaded (or part of) the normal libraries?  For
example, I don't need to load printf at all.  It's already part of the system.
Which I find interesting, since writef* is the right d utility to show console
output.

Any ideas?

I would love to have the day when I have to import printf and not writef*.

Just a thought... BTW, I should say that I am going home sick and maybe
delirious... Ñ'=

josé

tocy ize.co.jp says...
Hi, all.

int main()
{
char[] testString = "test";
printf("%s\n",testString);
return 0;
}

Error: Access Violation

It seems does not work.

dmd ver 0.121
WinXP
Apr 18 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
jicman wrote:

 For example, I don't need to load printf at all.  It's already part
 of the system. Which I find interesting, since writef* is the right d
 utility to show console output.
You really *need* to "import std.c.stdio;" to get the printf function... IMHO, Walter has just hacked printf into object.d for his own debugging. Please start importing it now, and less things breaks when it's fixed ? (assuming no major changes to D, like more modules implicitly imported) Or import std.stdio and use writef, which might be a better solution. (as suggested in your original posting, writef is more suited to D) --anders PS. You can see where it was commented out from, in std/c/stdio.d : //int printf(char *,...); //int wprintf(wchar_t *, ...);
Apr 18 2005
parent jicman <jicman_member pathlink.com> writes:
Yeah, I haven't had much time to look at the libraries at all.  Too little time.
Thanks for the insight.

jic

=?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
jicman wrote:

 For example, I don't need to load printf at all.  It's already part
 of the system. Which I find interesting, since writef* is the right d
 utility to show console output.
You really *need* to "import std.c.stdio;" to get the printf function... IMHO, Walter has just hacked printf into object.d for his own debugging. Please start importing it now, and less things breaks when it's fixed ? (assuming no major changes to D, like more modules implicitly imported) Or import std.stdio and use writef, which might be a better solution. (as suggested in your original posting, writef is more suited to D) --anders PS. You can see where it was commented out from, in std/c/stdio.d : //int printf(char *,...); //int wprintf(wchar_t *, ...);
Apr 19 2005