www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error runtime feedback of D too short?

reply AEon <AEon_member pathlink.com> writes:
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
Mar 22 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
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

Mar 22 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"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.
Mar 22 2005