View mode: basic / threaded / horizontal-split · Log in · Help
February 20, 2004
[Bug] char[dim] -> char[] implicit conversion bug
Here is another one. It seems that it has something to do with 
implicit conversion from char[dim] to char[]:

markoni@titan ~/src/dbug
$ cat char.d
import std.c.stdio;

void main() {
    char[64] buf;
    // char[] buf = new char[64];   // it works if replaced with this

    sprintf(buf, "Hello, World!\n");
    printf("buf: %.*s\n", buf);     // Works OK
    throw new Error(buf);           // prints garbage
}

markoni@titan ~/src/dbug
$ dmd char.d
d:\dmd\bin\..\..\dm\bin\link.exe char,,,user32+kernel32/noi;

markoni@titan ~/src/dbug
$ ./char.exe
buf: Hello, World!

Error: Ç ↕

Regards,
Marko
February 20, 2004
Re: [Bug] char[dim] -> char[] implicit conversion bug
"Marko Nikolic" <markoni69@verat.net> wrote in message
news:c15ncd$d91$1@digitaldaemon.com...
|     Here is another one. It seems that it has something to do with
| implicit conversion from char[dim] to char[]:
|
| markoni@titan ~/src/dbug
| $ cat char.d
| import std.c.stdio;
|
| void main() {
|      char[64] buf;
|      // char[] buf = new char[64];   // it works if replaced with this
|
|      sprintf(buf, "Hello, World!\n");
|      printf("buf: %.*s\n", buf);     // Works OK
|      throw new Error(buf);           // prints garbage
| }

The pointer to a local variable on the stack becomes invalid once the scope is
exited.
Something like
 throw new Error(buf.dup);
will copy the local string from the stack to the heap. Since "new" allocates
from
the heap that will work, too.

|
| markoni@titan ~/src/dbug
| $ dmd char.d
| d:\dmd\bin\..\..\dm\bin\link.exe char,,,user32+kernel32/noi;
|
| markoni@titan ~/src/dbug
| $ ./char.exe
| buf: Hello, World!
|
| Error: Ç ?
|
| Regards,
| Marko
February 21, 2004
Re: [Bug] char[dim] -> char[] implicit conversion bug
Ben Hinkle wrote:
> "Marko Nikolic" <markoni69@verat.net> wrote in message
> news:c15ncd$d91$1@digitaldaemon.com...
> |     Here is another one. It seems that it has something to do with
> | implicit conversion from char[dim] to char[]:
> |
> | markoni@titan ~/src/dbug
> | $ cat char.d
> | import std.c.stdio;
> |
> | void main() {
> |      char[64] buf;
> |      // char[] buf = new char[64];   // it works if replaced with this
> |
> |      sprintf(buf, "Hello, World!\n");
> |      printf("buf: %.*s\n", buf);     // Works OK
> |      throw new Error(buf);           // prints garbage
> | }
> 
> The pointer to a local variable on the stack becomes invalid once the scope is
> exited.
> Something like
>   throw new Error(buf.dup);
> will copy the local string from the stack to the heap. Since "new" allocates
> from
> the heap that will work, too.

   That's right. I am trying D after many years of not using C (and C++ 
even more - I never liked C++) - I used mainly Java and scripting 
languages) so I got very comfortable in not having memory management in 
mind ;>

   Thanks for your help.

Regards,
Marko
February 22, 2004
Re: [Bug] char[dim] -> char[] implicit conversion bug
You've taken a slice of something that no longer exists by the time the
slice is used.

"Marko Nikolic" <markoni69@verat.net> wrote in message
news:c15ncd$d91$1@digitaldaemon.com...
>     Here is another one. It seems that it has something to do with
> implicit conversion from char[dim] to char[]:
>
> markoni@titan ~/src/dbug
> $ cat char.d
> import std.c.stdio;
>
> void main() {
>      char[64] buf;
>      // char[] buf = new char[64];   // it works if replaced with this
>
>      sprintf(buf, "Hello, World!\n");
>      printf("buf: %.*s\n", buf);     // Works OK
>      throw new Error(buf);           // prints garbage
> }
>
> markoni@titan ~/src/dbug
> $ dmd char.d
> d:\dmd\bin\..\..\dm\bin\link.exe char,,,user32+kernel32/noi;
>
> markoni@titan ~/src/dbug
> $ ./char.exe
> buf: Hello, World!
>
> Error: Ç ?
>
> Regards,
> Marko
Top | Discussion index | About this forum | D home