Jump to page: 1 2
Thread overview
array handling Linux<->Windows
Sep 27, 2004
Thomas Kuehne
Sep 27, 2004
Ben Hinkle
Sep 27, 2004
Stewart Gordon
Sep 27, 2004
Dave
Sep 27, 2004
Burton Radons
Sep 27, 2004
Arcane Jill
Sep 27, 2004
Regan Heath
Sep 28, 2004
Sean Kelly
Sep 28, 2004
Ben Hinkle
Sep 28, 2004
Sean Kelly
Sep 27, 2004
Thomas Kuehne
Sep 27, 2004
Ant
Sep 27, 2004
Thomas Kuehne
September 27, 2004
The code below will result in a segment fault under Linux but runs on Windows.

int main(){
    char[] str="a1";
    str[0]++;
    assert(str[0]=='b');
    return 0;
}



September 27, 2004
Thomas Kuehne wrote:

> The code below will result in a segment fault under Linux but runs on Windows.
> 
> int main(){
>     char[] str="a1";
>     str[0]++;
>     assert(str[0]=='b');
>     return 0;
> }

I don't think this is a bug - it's like the C (and C++) behavior for string literals. This came up on the main thread a few days ago. It should be mentioned in the doc if it isn't already. To get a modifyable string call dup.

-Ben
September 27, 2004
In article <cj90ft$a4o$1@digitaldaemon.com>, Ben Hinkle says... <snip>
> I don't think this is a bug - it's like the C (and C++) behavior for string literals.  This came up on the main thread a few days ago.  It should be mentioned in the doc if it isn't already.  To get a modifyable string call dup.

I'd been under the impression that initialising a char[] was supposed to create a modifyable copy of the initialisation data.

But whatever the intended behaviour is, it ought to be portable.

Stewart.


September 27, 2004
In article <cj8uoq$63b$1@digitaldaemon.com>, Thomas Kuehne says...
>
>The code below will result in a segment fault under Linux but runs on Windows.
>
>int main(){
>    char[] str="a1";
>    str[0]++;
>    assert(str[0]=='b');
>    return 0;
>}

http://www.digitalmars.com/d/dcompiler.html
"
Differences from Win32 version

* String literals are read-only. Attempting to write to them will cause a
segment violation.
* The configuration file is /etc/dmd.conf
"

I thik that's it

Ant


September 27, 2004
In article <cj90ft$a4o$1@digitaldaemon.com>, Ben Hinkle says...
>
>I don't think this is a bug - it's like the C (and C++) behavior for string literals. This came up on the main thread a few days ago. It should be mentioned in the doc if it isn't already. To get a modifyable string call dup.

Hmmm. It's a shame we can't write:

#    const char[] str="a1";

to get platform-neutral behavior, isn't it?

Jill


September 27, 2004
Ben Hinkle schrieb:
> > The code below will result in a segment fault under Linux but runs on Windows.
> >
> > int main(){
> >     char[] str="a1";
> >     str[0]++;
> >     assert(str[0]=='b');
> >     return 0;
> > }
>
> I don't think this is a bug - it's like the C (and C++) behavior for
> string literals. This came up on the main thread a few days ago.
> It should be mentioned in the doc if it isn't already.

To me it isn't a bug that the code runs/crashs.

The problem is that it behaves different on different plattforms. Consider a module written on a Windows box that runs fine there but crashs when used on Linux. Now, pin-point me the problematic code ;)

Thomas



September 27, 2004
Ant schrieb:
> >The code below will result in a segment fault under Linux but runs on Windows.
> >
> >int main(){
> >    char[] str="a1";
> >    str[0]++;
> >    assert(str[0]=='b');
> >    return 0;
> >}
>
> http://www.digitalmars.com/d/dcompiler.html
> "
> Differences from Win32 version
>
> * String literals are read-only. Attempting to write to them will cause a
> segment violation.
> * The configuration file is /etc/dmd.conf
> "
>
> I thik that's it
>
> Ant

Hrrr, I've been searching through the language definition stuff, but there it is on the _compiler_ page!

Thomas


September 27, 2004
Stewart Gordon wrote:
[snip]
>
> But whatever the intended behaviour is, it ought to be portable.
> 
> Stewart.

Hear, hear!!

September 27, 2004
Stewart Gordon wrote:
> But whatever the intended behaviour is, it ought to be portable.

Next you'll want undefined expressions to have consistent undefined behaviour.  :)

The code doesn't work on Windows either anyway.

   void main ()
   {
       char [] a = "foo";
       char [] b = "foo";

       a [0] = 'b';
       printf ("%.*s %.*s\n", a, b);
   }

This prints "boo boo".

This appears to be a bug in DMD, because constant sections are definitely available and used in Windows:

   void main ()
   {
       ubyte *foo = cast (ubyte *) &main;

       printf ("%p %d\n", *foo, foo);
       *foo = *foo;
   }

This prints the address and data properly but Access Violates in the assignment, indicating that "main" is put in a read-only section. Walter, once you come back, can you explain why const data is not being put into constant sections on Windows?

I remember discussing this but it was years ago.
September 27, 2004
On Mon, 27 Sep 2004 13:28:17 +0000 (UTC), Arcane Jill <Arcane_member@pathlink.com> wrote:
> In article <cj90ft$a4o$1@digitaldaemon.com>, Ben Hinkle says...
>>
>> I don't think this is a bug - it's like the C (and C++) behavior for string
>> literals. This came up on the main thread a few days ago. It should be
>> mentioned in the doc if it isn't already. To get a modifyable string call
>> dup.
>
> Hmmm. It's a shame we can't write:
>
> #    const char[] str="a1";
>
> to get platform-neutral behavior, isn't it?

We can write that.. however the 'const' applies to the array reference, not the data it refers to, AFAIK there is no way to apply const to the data it refers to.

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
« First   ‹ Prev
1 2