Jump to page: 1 2
Thread overview
[Issue 1914] New: Array initialisation from const array yeilds memory trample
Mar 12, 2008
d-bugmail
Mar 13, 2008
d-bugmail
Mar 13, 2008
d-bugmail
Jun 06, 2008
d-bugmail
Nov 21, 2008
d-bugmail
Nov 21, 2008
d-bugmail
Nov 09, 2009
Jason Spashett
Nov 09, 2009
Stewart Gordon
Jan 15, 2010
Don
Feb 13, 2010
Don
Jan 21, 2011
Don
Feb 06, 2011
Don
March 12, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1914

           Summary: Array initialisation from const array yeilds memory
                    trample
           Product: D
           Version: 1.028
          Platform: All
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: jason@spashett.com


This bug is reproducible on (at least) Linux, DMD 1.028. Windows DMD 1.026

The following code prints "0", Unless I am mistaken "a" should be initialized with the contents of const array i. y is corrupted. With this construct, permissible or not, the compiler should not allow this to happen.

Note that direct initialization with an array (not though an intermediate const) does work as expected.


import std.stdio;

struct S1
{
    const float[10] i = [0,0,0,0,0,0,0,0,0,0];
    float a[10] = i;
    int y=5;
}


void main()
{
    S1    s;
    writefln(s.y);
}


-- 

March 13, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1914





------- Comment #1 from jason@spashett.com  2008-03-13 06:04 -------
This works correctly with gdc on linux, using dmd frontend 1.021.

user@vm-fruitbat:~$ gdc --version
gdc (GCC) 4.1.3 20070831 (prerelease gdc 0.25, using dmd 1.021) (Ubuntu
0.25-4.1.2-16ubuntu1)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

user@vm-fruitbat:~$ gdc array_test.d
user@vm-fruitbat:~$ ./a.out
5


-- 

March 13, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1914


jason@spashett.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason@spashett.com




------- Comment #2 from jason@spashett.com  2008-03-13 07:29 -------
From looking at the assembly briefly, it appears that:

Direct initialisation is fine:

float a[10] = [0,0,0,0,0,0,0,0,0,0];

(compiler appears to generate "template" struct to initalise array and following int in one go with rep movsd.

esi points to a correct "template" in this case.


In the case of

...
const float[10] i = [0,0,0,0,0,0,0,0,0,0];
float a[10] = i;
...

Code is the same, but the "template" is wrong. Presumably because the compiler does not understand the const array initialiser properly.


-- 

June 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1914


jason@spashett.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         OS/Version|Linux                       |All
            Version|1.028                       |1.030




------- Comment #3 from jason@spashett.com  2008-06-06 16:35 -------
Still present in DMD 1.030 on windows and on DMD 1.030 Linux.


-- 

November 21, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1914


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com
           Keywords|                            |wrong-code




------- Comment #4 from smjg@iname.com  2008-11-20 20:42 -------
It seems that it's simply failing to initialise y correctly.  Changing main to

void main()
{
    S1    s;
    writefln(s.y);
    writefln(s.a);

    s.y = 4;
    writefln(s.y);
    writefln(s.a);
}

gives the output

0
[0,1,2,3,4,5,6,7,8,9]
4
[0,1,2,3,4,5,6,7,8,9]


-- 

November 21, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1914





------- Comment #5 from jason@spashett.com  2008-11-21 05:32 -------
(In reply to comment #4)
> It seems that it's simply failing to initialise y correctly.  Changing main to
When I looked at the assembly, I think (can't remember now) that y was initialised, but the array initialisation was overwriting the initialisation of y.


A note that the values you put into the array i affects the value of y. try the second struct below, which might give you

C:\temp>dmd test.d
C:\temp>test.exe
1065353216

on windows.


Removing the initialisation from const array does "fix" it though, so this works:

struct S1
{
    float a[10] = [1,1,1,1,1,1,1,1,1,1];
    int y=5;
}

but this does not:

struct S1
{
    const float[10] i = [1,1,1,1,1,1,1,1,1,1];
    float a[10] = i;
    int y=5;
}


-- 

November 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=1914


Jason Spashett <jason@spashett.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|1.030                       |1.051


--- Comment #6 from Jason Spashett <jason@spashett.com> 2009-11-09 02:58:45 PST ---
Still present. To restate the bug, here is a unit test, that should not fail:

unittest {
        struct A
        {
            const char[10] i = [0,0,0,0,0,0,0,0,0,0];

                        char[10] x = i;
            int y=5;
        }
        A a;
        assert(a.y==5, "bug #1914 present");
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=1914


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|1.051                       |1.030


--- Comment #7 from Stewart Gordon <smjg@iname.com> 2009-11-09 03:26:56 PST ---
Please don't increase version numbers. http://www.digitalmars.com/d/archives/digitalmars/D/bugs/bugzilla_usage_tips_10071.html

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1914


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au
           Severity|major                       |critical


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1914


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #8 from Don <clugdbug@yahoo.com.au> 2010-02-13 14:09:33 PST ---
ROOT CAUSE: The glue layer is misinterpreting it as an array block assignment, so for a 5 element array, it duplicates the array five times. It recognizes that strings are slice assignments rather than block assignments, but it needs to do the same thing for array literals.


PATCH(against DMD2 svn 382): todt.c, line 1001 in TypeSArray::toDtElem.


    }
-    else if (e->op != TOKstring)
+    else if (e->op != TOKstring && e->op != TOKarrayliteral)
    {   // Array block assignment (rather than slice assignment)
        for (i = 1; i < len; i++)
        {
        if (tbn->ty == Tstruct)
        {   pdt = tnext->toDt(pdt);
            while (*pdt)
            pdt = &((*pdt)->DTnext);
        }
        else
            pdt = e->toDt(pdt);
        }
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2