Thread overview
[Issue 832] New: Struct copying changed in 1.0
Jan 12, 2007
d-bugmail
Jan 12, 2007
d-bugmail
Jan 12, 2007
Lionello Lunesu
[Issue 832] NRVO: return inside foreach results in junk
Jan 27, 2007
d-bugmail
Jan 31, 2007
d-bugmail
Feb 09, 2007
d-bugmail
Feb 15, 2007
d-bugmail
January 12, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=832

           Summary: Struct copying changed in 1.0
           Product: D
           Version: 1.00
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: alan@akbkhome.com


The behaviour changed in dmd-0.178 such that foreach on an array of structs only appears to do  a shallow copy, rather than a deep copy.

output of the code below is 29/30 or <.178, and a random number on 178/1.0.

import std.stdio;

const char[] XML_PUBLIC_ID_SYNCML_SYNCML11 ="-//SYNCML//DTD SyncML 1.1//EN";


struct WBXMLPublicIDEntry {
                int  wbxmlPublicID;     /**< WBXML Public ID */
                char[] xmlPublicID; /**< XML Public ID */
                char[] xmlRootElt;  /**< XML Root Element */
                char[] xmlDTD;      /**< XML DTD */
        }

const WBXMLPublicIDEntry sv_wml11_public_id           = {
        33,
        XML_PUBLIC_ID_SYNCML_SYNCML11,
        "wml",
        "http://www.wapforum.org/DTD/wml.xml"
};

        struct WBXMLLangEntry
        {
                int               langID;             /**< Language ID */
                WBXMLPublicIDEntry    publicID;          /**< Public ID */
        }


const WBXMLLangEntry mainTable[] = [

        { 123,
                sv_wml11_public_id },

        { 132,              sv_wml11_public_id }
];

void main()
{
         WBXMLLangEntry mt = getfirst();
         WBXMLPublicIDEntry pie = getName(mt);

         writefln("got %d", pie.xmlPublicID.length);
}
static WBXMLLangEntry getfirst()
{

        foreach(mt;mainTable) {
                return mt;
        }
}
WBXMLPublicIDEntry getName(WBXMLLangEntry mt)
{
        return mt.publicID;
}


-- 

January 12, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=832


wbaxter@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Struct copying changed in   |Struct copying changed in
                   |1.0                         |1.0




------- Comment #1 from wbaxter@gmail.com  2007-01-12 05:14 -------
Probably also NRVO-related.
See http://d.puremagic.com/issues/show_bug.cgi?id=829 also.


-- 

January 12, 2007
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>             Summary|Struct copying changed in   |Struct copying changed in
>                    |1.0                         |1.0


Find the differences.. :S
January 27, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=832


lio@lunesu.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |major
           Keywords|                            |wrong-code
            Summary|Struct copying changed in   |NRVO: return inside foreach
                   |1.0                         |results in junk




------- Comment #3 from lio@lunesu.com  2007-01-27 04:03 -------
// Smaller program with same problem.
// Should print "1" three times, but only first is OK

import std.stdio;

struct Struct
{
    int langID;
    long _force_nrvo;
}

Struct[1] table;

Struct getfirst()
{
    foreach(v; table) {  //inout also doesn't work
        writefln(v.langID);// not OK
        return v;
    }
}

void main()
{
    table[0].langID = 1;

    foreach(v; table) {
        writefln(v.langID);//OK
        break;
    }
    auto v = getfirst();
    writefln(v.langID);// not OK
}


-- 

January 31, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=832





------- Comment #4 from alan@akbkhome.com  2007-01-31 03:49 -------
For reference, the workaround is to return pointers to the struct, rather than passing around the Struct.


-- 

February 09, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=832


lio@lunesu.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #5 from lio@lunesu.com  2007-02-09 02:08 -------
Tested both programs, work OK in 1.005


-- 

February 15, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=832





------- Comment #7 from thomas-dloop@kuehne.cn  2007-02-15 03:42 -------
Added to DStress as http://dstress.kuehne.cn/run/s/struct_27_A.d


--