Thread overview
Error runtime feedback of D too short?
Mar 22, 2005
AEon
Mar 22, 2005
Chris Sauls
Mar 22, 2005
Ben Hinkle
March 22, 2005
In a function I did this:

<code>
void moo()
{
char[16] warn = "read_Cfg_Data";  // [13] would be correct!

..
}
</code>

Now compiling the code with dmd -w will not show an error, but when you run the function the D will say:

    Error: lengths don't match for array copy

In case you are wondering, a quick copy and paste will lead to such bugs, the correct define would be:

    char[13] warn = "read_Cfg_Data";


Alas if you happen to keep coding, until you start compiling, tracking down the specific line of code that is in error can be quite tedious (e.g. using writefln all over the place).


a) So, should not/count not the compiler recognize such (manual) static allocation errors during compile and warn the programmer via error message that contains a file name and line number?

b) Would it be possible to make the runtime errors a bit more specific, to the location of the error?

c) Am I missing some compile switch that would help?

AEon
March 22, 2005
You might just try this:

# void moo() {
#   char[] warn = "read_Cfg_Data".dup;
# }

By using a dynamic array you avoid worrying about changing lengths, and really there is no difference between dynamic and static arrays in memory -- as I understand it.  Only the compiler makes a distinction. Also the ".dup" at the end helps avoid a nasty cross-platform issue between Win32 and Linux wherein Linux wants to protect the string constant (literal) from being modified by slices.

-- Chris Sauls

AEon wrote:
> In a function I did this:
> 
> <code>
> void moo()
> {
> char[16] warn = "read_Cfg_Data";  // [13] would be correct!
> 
> ..
> }
> </code>
> 
> Now compiling the code with dmd -w will not show an error, but when you run the
> function the D will say:
> 
>     Error: lengths don't match for array copy
March 22, 2005
"Chris Sauls" <ibisbasenji@gmail.com> wrote in message news:d1pnsv$7c9$5@digitaldaemon.com...
> You might just try this:
>
> # void moo() {
> #   char[] warn = "read_Cfg_Data".dup;
> # }
>
> By using a dynamic array you avoid worrying about changing lengths, and really there is no difference between dynamic and static arrays in memory -- as I understand it.  Only the compiler makes a distinction. Also the ".dup" at the end helps avoid a nasty cross-platform issue between Win32 and Linux wherein Linux wants to protect the string constant (literal) from being modified by slices.

Although beware that the dup will happen every time the function is run - which can add up to lots of garbage generation. In general I'd recommend using COW instead of dup'ing. One should only dup when writing to a string that one doesn't own.