www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 832] New: Struct copying changed in 1.0

reply d-bugmail puremagic.com writes:
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;
}


-- 
Jan 12 2007
next sibling parent reply d-bugmail puremagic.com writes:
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





Probably also NRVO-related.
See http://d.puremagic.com/issues/show_bug.cgi?id=829 also.


-- 
Jan 12 2007
parent Lionello Lunesu <lio lunesu.remove.com> writes:
            What    |Removed                     |Added
 ----------------------------------------------------------------------------
             Summary|Struct copying changed in   |Struct copying changed in
                    |1.0                         |1.0
Find the differences.. :S
Jan 12 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
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





// 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
}


-- 
Jan 27 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=832






For reference, the workaround is to return pointers to the struct, rather than
passing around the Struct.


-- 
Jan 31 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=832


lio lunesu.com changed:

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





Tested both programs, work OK in 1.005


-- 
Feb 09 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=832






Added to DStress as
http://dstress.kuehne.cn/run/s/struct_27_A.d


-- 
Feb 15 2007