January 26, 2008
Robert Fraser:
> Saaa wrote:
> > char[] str;
> > char[] str1 = "abc";
> > str[0] = 'b';        // error, "abc" is read only, may crash
> > Is this example correct?
> Yes, because the "abc" is a string literal, that is to say it's written in the code itself. If str1 was loaded from an outside source, such as a file, user input, etc., then you could modify it without issue.
> 
> For string LITERALS (a string literal is one you write in the code itself, usually encased in double-quotes), modifying them without calling .dup on them is bad. For other strings, it's perfectly okay.

I am no an expert of D yet, but I think in the following D 1.x code str is a dynamic array, so it can be changed safely:

void main() {
    char[] str = "abc";
    str[0] = 'b';
}

Bye,
bearophile
January 26, 2008
On Sat, 2008-01-26 at 10:26 -0500, bearophile wrote:
> Robert Fraser:
> > Saaa wrote:
> > > char[] str;
> > > char[] str1 = "abc";
> > > str[0] = 'b';        // error, "abc" is read only, may crash
> > > Is this example correct?
> > Yes, because the "abc" is a string literal, that is to say it's written in the code itself. If str1 was loaded from an outside source, such as a file, user input, etc., then you could modify it without issue.
> > 
> > For string LITERALS (a string literal is one you write in the code itself, usually encased in double-quotes), modifying them without calling .dup on them is bad. For other strings, it's perfectly okay.
> 
> I am no an expert of D yet, but I think in the following D 1.x code str is a dynamic array, so it can be changed safely:
> 
> void main() {
>     char[] str = "abc";
>     str[0] = 'b';
> }
> 
> Bye,
> bearophile

On linux you would get an nice Segfault when running that code.

January 26, 2008
Saaa <empty@needmail.com> wrote:

> I finally see what a string literal means, but this code still bothers me.
> What does changing a dynamic char[] (str) have to do with the string literal
> in str1
>
>>> from the 1.0 documentation:
>>>
>>> char[] str;
>>> char[] str1 = "abc";
>>> str[0] = 'b';        // error, "abc" is read only, may crash
>>>
>>> Is this example correct?
>>>
>>>
>>
>> Yes, because the "abc" is a string literal, that is to say it's written in
>> the code itself. If str1 was loaded from an outside source, such as a
>> file, user input, etc., then you could modify it without issue.
>>
>> For string LITERALS (a string literal is one you write in the code itself,
>> usually encased in double-quotes), modifying them without calling .dup on
>> them is bad. For other strings, it's perfectly okay.
>
>

You're right, of course. str[0] has nothing to do with "abc" or str1. The code will fail, but with an arraybounds error (str.length == 0, so no element 0 is available)

Now, this code:

  char[] str1 = "abc";
  str1[0] = 'b';        // error, "abc" is read only, may crash

Should fail for the above reasons.


Simen Kjaeraas
January 26, 2008
Milke Wey:
> On linux you would get an nice Segfault when running that code.

Ah, thank you then. I have thought that's true only for string literals assigned to static arrays:

void main() {
    char[3] str = "abc";
    str[0] = 'b';
}

Bye,
bearophile
January 27, 2008
On Sat, 2008-01-26 at 15:26 -0500, bearophile wrote:
> Milke Wey:
> > On linux you would get an nice Segfault when running that code.
> 
> Ah, thank you then. I have thought that's true only for string literals assigned to static arrays:
> 
> void main() {
>     char[3] str = "abc";
>     str[0] = 'b';
> }
> 
> Bye,
> bearophile

I don't know why but it actually works when using a static array.

-- 
Mike Wey

January 28, 2008
Milke Wey wrote:
> On Sat, 2008-01-26 at 15:26 -0500, bearophile wrote:
>> Milke Wey:
>>> On linux you would get an nice Segfault when running that code.
>> Ah, thank you then. I have thought that's true only for string literals assigned to static arrays:
>>
>> void main() {
>>     char[3] str = "abc";
>>     str[0] = 'b';
>> }
>>
>> Bye,
>> bearophile
> 
> I don't know why but it actually works when using a static array.
> 

On a UNIX system or a Windows system? It should work fine on Windows anyway. Also, the static array declaration may say to the compiler "allocate this on the stack"... not sure; I don't think that's speced.
January 28, 2008
On Sun, 2008-01-27 at 22:44 -0800, Robert Fraser wrote:
> Milke Wey wrote:
> > On Sat, 2008-01-26 at 15:26 -0500, bearophile wrote:
> >> Milke Wey:
> >>> On linux you would get an nice Segfault when running that code.
> >> Ah, thank you then. I have thought that's true only for string literals assigned to static arrays:
> >>
> >> void main() {
> >>     char[3] str = "abc";
> >>     str[0] = 'b';
> >> }
> >>
> >> Bye,
> >> bearophile
> > 
> > I don't know why but it actually works when using a static array.
> > 
> 
> On a UNIX system or a Windows system? It should work fine on Windows anyway. Also, the static array declaration may say to the compiler "allocate this on the stack"... not sure; I don't think that's speced.

On Linux with DMD 1.025.

-- 
Mike Wey

1 2 3
Next ›   Last »