www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - expected array behaviour

reply Mike James <foo bar.com> writes:
I have a function that uses 2 array strings defined similar to this...

const char[] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[]  array2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

If I make a change to a char in array1 it also changes the same in array2.
But if I define the arrays as follows...

const char[26] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[26]  array2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

It doesn't occur. Is this expected behaviour?

Regards,
-=mike=-
Jan 01 2009
next sibling parent reply John Reimer <terminal.node gmail.com> writes:
Hello Mike,

 I have a function that uses 2 array strings defined similar to this...
 
 const char[] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[]  array2 =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
 If I make a change to a char in array1 it also changes the same in
 array2. But if I define the arrays as follows...
 
 const char[26] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[26]  array2
 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
 It doesn't occur. Is this expected behaviour?
 
 Regards,
 -=mike=-
Wha?! If that's true, that looks like a nasty bug! Is that dmd v2 or v1? Linux or Windows? -JJR
Jan 01 2009
parent reply Mike James <foo bar.com> writes:
Hi John,

I am using D1.038, dsss and Tango.

I've written a quick example but this one is even stranger...

========================================

module main;

import tango.io.Stdout;

int main() {
    func1();
    func2();

    return 0;
}

void func1() {
    char[] array1 = "ABCD";
    char[] array2 = "ABCD";

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;

    array2[0] = 'Z';

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;
}

void func2() {
    char[4] array1 = "ABCD";
    char[4] array2 = "ABCD";

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;

    array2[1] = 'Q';

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;
}

========================================

Regards,

-=mike=-
Jan 01 2009
next sibling parent Mike James <foo bar.com> writes:
Forgot to mention - Windows XP.

Regards,

-=mike=-
Jan 01 2009
prev sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Jan 1, 2009 at 6:56 PM, Mike James <foo bar.com> wrote:
 Hi John,

 I am using D1.038, dsss and Tango.

 I've written a quick example but this one is even stranger...

 ========================================

 module main;

 import tango.io.Stdout;

 int main() {
    func1();
    func2();

    return 0;
 }

 void func1() {
    char[] array1 = "ABCD";
    char[] array2 = "ABCD";

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;

    array2[0] = 'Z';

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;
 }

 void func2() {
    char[4] array1 = "ABCD";
    char[4] array2 = "ABCD";

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;

    array2[1] = 'Q';

    Stdout(array1).newline;
    Stdout(array2).newline;
    Stdout.newline;
 }

 ========================================

 Regards,

 -=mike=-
If you want to modify the contents of string literals, like you're doing here, put a .dup on them. char[] array1 = "ABCD".dup; Again, modifying the contents of string literals is illegal and the results are undefined.
Jan 01 2009
parent John Reimer <terminal.node gmail.com> writes:
Hello Jarrett,

 If you want to modify the contents of string literals, like you're
 doing here, put a .dup on them.
 
 char[] array1 = "ABCD".dup;
 
 Again, modifying the contents of string literals is illegal and the
 results are undefined.
 
Oh, right. I missed that. -JJR
Jan 01 2009
prev sibling next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Jan 1, 2009 at 5:55 PM, Mike James <foo bar.com> wrote:
 I have a function that uses 2 array strings defined similar to this...

 const char[] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 char[]  array2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

 If I make a change to a char in array1 it also changes the same in array2.
 But if I define the arrays as follows...
You'd get a runtime error if you were using Linux. For some reason string literals are not read-only, or Windows doesn't respect it, or something like that. Modifying either array1 or array2 is technically illegal. So, uh, don't do it.
Jan 01 2009
parent reply John Reimer <terminal.node gmail.com> writes:
Hello Jarrett,

 On Thu, Jan 1, 2009 at 5:55 PM, Mike James <foo bar.com> wrote:
 
 I have a function that uses 2 array strings defined similar to
 this...
 
 const char[] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[]  array2 =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
 If I make a change to a char in array1 it also changes the same in
 array2. But if I define the arrays as follows...
 
You'd get a runtime error if you were using Linux. For some reason string literals are not read-only, or Windows doesn't respect it, or something like that. Modifying either array1 or array2 is technically illegal. So, uh, don't do it.
Yes, that's one advantage to Linux. String literals aren't read-only on Win32. This is unfortunate because it means that these sort of bugs are significantly harder to diagnose on Windows than on Linux. I remember that this was a bug in a early DUI version (now GtkD). It's was pretty easy to spot on Linux because of the runtime error. -JJR
Jan 01 2009
parent reply =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= <afb algonet.se> writes:
John Reimer wrote:

 You'd get a runtime error if you were using Linux.  For some reason
 string literals are not read-only, or Windows doesn't respect it, or
 something like that.  Modifying either array1 or array2 is technically
 illegal.  So, uh, don't do it.
Yes, that's one advantage to Linux. String literals aren't read-only on Win32. This is unfortunate because it means that these sort of bugs are significantly harder to diagnose on Windows than on Linux. I remember that this was a bug in a early DUI version (now GtkD). It's was pretty easy to spot on Linux because of the runtime error.
The string literals are read-only on GDC for Win32 too, if that helps... --anders
Jan 02 2009
parent John Reimer <terminal.node gmail.com> writes:
Hello Anders,

 John Reimer wrote:
 
 You'd get a runtime error if you were using Linux.  For some reason
 string literals are not read-only, or Windows doesn't respect it, or
 something like that.  Modifying either array1 or array2 is
 technically illegal.  So, uh, don't do it.
 
Yes, that's one advantage to Linux. String literals aren't read-only on Win32. This is unfortunate because it means that these sort of bugs are significantly harder to diagnose on Windows than on Linux. I remember that this was a bug in a early DUI version (now GtkD). It's was pretty easy to spot on Linux because of the runtime error.
The string literals are read-only on GDC for Win32 too, if that helps... --anders
That's interesting. So it's actually a compiler issue, not a platform one. -JJR
Jan 02 2009
prev sibling next sibling parent reply Mike James <foo bar.com> writes:
Got it - so I should do this...

const char[] array1= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[]  array2 = new char[array1.length];

and then copy the contents of array1 into array2.

Regards,

-=mike=-
Jan 01 2009
parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Jan 1, 2009 at 7:10 PM, Mike James <foo bar.com> wrote:
 Got it - so I should do this...

 const char[] array1= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 char[]  array2 = new char[array1.length];
Just "char[] array2 = array1.dup;" .dup duplicates the array by creating a new array the same length and copying the data over.
Jan 01 2009
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Mike James wrote:
<snip>
 const char[26] array1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 char[26]  array2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
 It doesn't occur. Is this expected behaviour?
Yes. Static arrays have value semantics, unlike dynamic arrays, which have reference semantics. Stewart.
Jan 02 2009