Temporary objects deleted to soon?

bash-3.2$ uname -a
SunOS solaris 5.11 snv_86 i86pc i386 i86pc
bash-3.2$ CC -V
CC: Sun C++ 5.9 SunOS_i386 2007/11/15
I believe that the C++ compiler is producing incorrect results with respect
to the creation and destruction of temporary variables when using operator
overloading.
The code sample below is compiled using:
bash-3.2$ CC -o huh6.exe huh6.C -features=tmplife -features=strictdestrorder
// begin code: huh6.C
#include  <stdio.h>
#include  <stdlib.h>
class AClass {
public:
    double *a;
    AClass( double x = 0 );
    ~AClass( void ) {
        //printf("<<<AClass variable deleted>>>\n");
        delete [] a;
    AClass& operator=( const AClass &right );
    friend AClass operator+( const AClass &left, const AClass &right );
AClass::AClass( double x ) {
    a = new double[1];
    a[0] = x;
    //printf("***AClass variable created***\n");
AClass& AClass::operator=( const AClass &right ) {
    a[0] = right.a[0];
    return( *this );
AClass operator+( const AClass &left, const AClass &right ) {
    AClass tmp;
    tmp.a[0] = left.a[0] + right.a[0];
    return( tmp );
int main() {
    AClass f, g, x, y, z;
    x.a[0] = 1.0; 
    y.a[0] = 2.0;
    z.a[0] = 3.0;
    f = x + y + z;
    printf("(1) f.a = %e\n", f.a[0]);
    g = x + y;
    g = g + z;
    printf("(2) g.a = %e\n", g.a[0]);
    return(0);
// end code huh6.C
Execution of the program yields:
bash-3.2$ ./huh6.exe
(1) f.a = 3.000000e+00
(2) g.a = 6.000000e+00
The expected result is
(1) f.a = 6.000000e+00
(2) g.a = 6.000000e+00
By uncommenting the printf statements it will be noted that the temporary
variables are deleted too soon.
Is there a compiler option that I am missing?
Thanks in advance.
===================================
The g++ compiler (using Linux/Mac OS X/OpenBSD/FreeBSD) produces:
(1) f.a = 6.000000e+00
(2) g.a = 6.000000e+00

The code involves copying AClass objects to create temps, and you don't have a copy constructor. The compiler-generated copy constructor copies the pointer 'a' in the class, and when one of the copies is destroyed, the data in the other copy is also destroyed.
Add a copy constructor similar to the one below and your code should behave as you expect.
AClass::AClass( const AClass &c ) {
    a = new double[1];
    a[0] = c.a[0];
    // printf("copy    from %p to %p\n", &c, this);
}

Similar Messages

  • Temporary object deleted too late...

    #include <string>
    #include <memory>
    class A{
    pubilc:
       const std::string& getId() const{ return i_id;}
    private:
       const std::string i_id;
    namespace{
    std::auto_ptr<A>
    return_autoptr_object(const char* arg)
      A* const ret = new A(/*arg*/);
      return std::auto_ptr<A>(ret);
    int main()
        const std::string myID=return_autoptr_object("test_autoptr")->getId();
        typedef void end_of_block_scope;
        std::cout << "*** out of scope here ****" << std::endl;
      return 0;
    }The code above shows that a temporary object(returned by return_autoptr_object(..)) is deleted only AFTER(?) "out of scope here" line being printed. I expected it should be deleted right after it's being assigned its' id to "myID", where the checkpoint for that function exists, and NOT when it goes out of scope (of the block).
    I tested it with other two compilers(gcc and visual age c++), both yield to the same expected result, only sun CC had diff result.
    Possibly it's a bug?

    Just a quick remark: It is with sunstudio 12 that lifetimes become standard. SunStudio 11 still has the same old behavior.
    #include <iostream>
    struct testerClass
            testerClass() {}
            ~testerClass() {std::cout << "Destructor for testerClass" << std::endl;}
            void doit() {std::cout << "doit invoked" << std::endl;}
    int main()
            testerClass().doit();
            std::cout << "Out of scope by now" << std::endl;
    }a) /opt/SUNWspro/v11/bin/CC main.cc
    doit invoked
    Out of scope by now
    Destructor for testerClass
    b) /opt/SUNWspro/v11/bin/CC -features=tmplife main.cc
    doit invoked
    Destructor for testerClass
    Out of scope by now
    version of "/opt/SUNWspro/v11/prod/bin/../../bin/cc": Sun C 5.8 Patch 121015-04 2007/01/10

  • Temporary objects are deleted incorrectly

    The temporary objects are deleted (their destructors called) when they are leaving the scope of the expression due to wich they were created. But the Standard says that a temporary object should be deleted when the full expression due to wich it was created is completely evaluated (if no reference to it was created).
    Consider the following example:
    #include <iostream>
    using namespace std;
    class A
    public:
    A() {
      cout << "A constructor" << endl;
    ~A() {
      cout << "A destructor" << endl;
    A foo() {
    cout << "foo has been called" << endl;
    return A();
    void foo2(A const& a) {
    cout << "foo2 has been called" << endl;
    int main(int argc, char *argv[], char *envp[])
    cout << "main started" << endl;
    foo2(foo()); // BUG
    cout << "main continued execution" << endl;
    return 0;
    }This test makes the following output:
    main started
    foo has been called
    A constructor
    foo2 has been called
    main continued execution
    A destructor
    As you may see, the temporary object of type A created as a return value of function foo() is deleted on main() exit, not on finishing evaluation of the expression "foo2(foo());".
    The expected output is:
    main started
    foo has been called
    A constructor
    foo2 has been called
    A destructor
    main continued execution
    Actually, if the line marked as BUG is enclosed in {} brackets the desired output is achieved.

    For compatibility with older compilers, the compiler by default destroys temporary objects at the end of the block in which they are destroyed.
    To get standard-conforming behavior, use the compiler option
    -features=tmplife
    on each module where temporary lifetime makes a difference.

  • Copy constructor and temporary objects in C++

    I'd need some clarification on how constructor works when the argument is  a temporary object. I defined a dummy MyVect class with operator+ and copy constructor:
    class MyVect {
    public:
    MyVect() {
    cout << "Constructor" << endl;
    MyVect(const MyVect &vect){
    cout << "Copy Constructor" << endl;
    ~MyVect() {
    cout << "Destructor" << endl;
    MyVect operator+(const MyVect &v) const {
    cout << "operator+" << endl;
    MyVect result
    return result;
    Then I test it with this very simple code pattern:
    MyVect v1;
    MyVect v2;
    MyVect v3(v1 + v2);
    The output is:
    Constructor
    Constructor
    operator+
    Constructor
    Destructor
    Destructor
    Destructor
    The first two constructor calls are for v1 and v2, then operator+ for  v1+v2 is called, and  inside it there is a constructor call for result object. But then no copy constructor (nor constructor) for v3 is called. In my limited understanding of C++, I guessed that once the temporary object resulting from v1+v2 has been created, then it would be the argument of the copy constructor call. Instead it looks like the compiler transforms the temporary into v3, since v3 is correctly initialized as the sum of v1 and v2 (I tried with a more complicated version of MyVect to verify this). This behavior reminds me of the move semantics introduced with C++11, but I'm not using the -std=c++11 flag (and in fact if I try to define a move constructor the compilation fails).
    Can anyone please help me to understand what's happening under the hood? Thanks.
    Last edited by snack (2013-02-13 10:44:28)

    wikipedia wrote:The following cases may result in a call to a copy constructor:
    When an object is returned by value
    When an object is passed (to a function) by value as an argument
    When an object is thrown
    When an object is caught
    When an object is placed in a brace-enclosed initializer list
    These cases are collectively called copy-initialization and are equivalent to:[2] T x = a;
    It is however, not guaranteed that a copy constructor will be called in these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example being the return value optimization (sometimes referred to as RVO).
    Last edited by Lord Bo (2013-02-13 13:58:47)

  • Runtime error: Boost::foreach on temporary object

    Hello,
    I have strange behavior, when using boost::foreach on the r-value (temporary object).
    It compiles fine, but that it causes std::bad_alloc while running. :-(
    It affects both ss12 and ss-exp (March one, CC: Sun Ceres C++ 5.10 SunOS_i386 2009/03/06).
    The precompiled code posted here is generated by sstudio-express.
    The problematic line is:
    foreach(std::string hit_type, utils::split((**anim_itor)["hits"]))
    While this is OK:
    std::vector<std::string> pom = utils::split((**anim_itor)["hits"]);
    foreach(std::string hit_type, pom) {
    Here is precompiled code:
    http://filebin.ca/gpubbw/unit_animation.i
    Full source code can be found here (line 557):
    http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_animation.cpp?rev=33993&dir_pagestart=250&view=markup
    Can you please check if it's suncc bug - in case yes, are all info which I provided here enough to put inside bug report?
    Thanks a lot,
    Petr

    Hi
    Looking at
    http://www.boost.org/doc/libs/1_38_0/boost/foreach.hpp
    there's this
    # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)                                                      \
      || BOOST_WORKAROUND(__BORLANDC__, < 0x593)                                                    \
      || (BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 700) && defined(_MSC_VER))                   \
      || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570))                                      \
      || BOOST_WORKAROUND(__DECCXX_VER, <= 60590042)
    #  define BOOST_FOREACH_NO_RVALUE_DETECTIONso there's something specific for Studio 10.
    I have 2 ideas:
    Change the above __SUNPRO_CC test to 0x590 or 0x510
    Use the -featuers=tmplife option.
    Paul
    Edited by: Paul_Floyd on Mar 23, 2009 4:50 AM

  • When i duplicate an object, it moves it far away from where my original object is as soon as i move it. is there any way to fix this?

    when i duplicate an object in numbers, it moves it far away from where my original object is as soon as i move it. is there any way to fix this? thanks

    Hi Rick,
    Your initial duplicate should be offset by one grid unit to the right and below the location of the original. If you move that duplicate by dragging it, without first de-selecting it, then reselecting it, the place you put it will determine the offset for subsequent duplicates (from the same original or from the copy).
    Example:
    Initial object and first duplicate:
    Pressing shift-up arrow would move the duplicate up to align with the top of the original. Pressing shift-left arrow would then place the duplicate directly in front of the original.
    Without being deselected, the duplicate was dragged to the position shown:
    Command-D was then pressed to duplicate the first copy:
    The duplicate appeared in this position
    Regards,
    Barry

  • Objects deleted under transport request by me-how can I know what were dele

    I am using my collegue's user id and password.
    I was creating data element and adding it to segment in we31 and then I did program change in se38.
    Unfortunatley, I was created all the above under my colleague's existing transport request which contains so many objects.  After I realised, I deleted some to create all under a fresh request ( it seems some which are not related to my development were also deleted . so how can i find what ever were I deleted ? ) ...not sure what was I deleted...may be one program, one transaction code etc.
    Kindly help me how to find out what were I deleted under that request  ?
    YOUR HELP WILL BE HIGHLY APPRECIATED.

    There is no easy way to find it out. Something you can do that you can find out the object list from TADIR table which is created by your friend and then go to E070 table and E071 table and check among those objects which are not attached with any transport request.
    So thus you will get a smaller list of objects and your friends objects ( deleted by you) will be definitely in that smaller list. Now you can check with your friends so ensure which are the objects deleted by you.

  • How to find Object delete information when Audit is not enabled.

    Hello Experts,
    One of the object is deleted from our environment .
    I tried fetching the deskin report frm acitvity universe but , no data availble as the audit option is not enabled .
    Is there any way to get the information of object deleted , like who deleted it.
    This is very import isssue to find out the information can any one help me with it.
    Regards,

    Hi Neo, in addition, I suggest you to remove any right to delete any object if it is in the Production environment.
    If you are using the BusinessObjects tool, you should join ASUG at www.asug.com

  • Oracle .trc files getting deleted as soon as it generates.

    Hi,
    Its a strange situation. Oracle .trc files are getting deleted as soon as it genetates. There is no cron job nor any background job is running .
    Any idea what this could be?
    Thanks,
    kam

    change the location of your trace files..1. Is it somewhere documented; if yes then please give link.?
    2. Have you or your friend ever tried like that? If yes then please share your experience.

  • Multiple Objects Deletion

    When Highlighting Multiple Objects and Purge (Recyclebin) or Delete, a repetative confirmation dialog is asked for each object, would like to see "yes for all" functionality in dialog.
    Regards

    You can vote for the existing request at the SQL Developer Exchange, to add weight for possible sooner implementation.
    Regards,
    K.

  • Temporary Files  delete problems

    thanks ,
    I can't delete the Temporary Files
    this is the code:
    //at my servlet .............File tmpFile = File.createTempFile("000000book", ".pdf");String path = tmpFile.getAbsolutePath();tmpFile.deleteOnExit();path = path.replace('\\', '/');session.setAttribute("pdfPATH", path);-------------------do--PDF file create here-----------..............
    and open the PDF file in the next jsp file.
    but when i closed the programe the 000000book7109.pdf file is still
    existed
    and i try to close All.
    I was failed.
    what can i do ?
    thanks

    hi dnamiot:
    it is a wonderful site ,and a fantastic library.
    thanks in advance
    kou

  • Web address deletes as soon as the page loads

    Whenever I click a link, or a recently visited page from the drop down history, as soon as the webpage loads, the web address deletes itself. This is annoying because sometimes I'd like to copy paste it to share with friends. Also, similar problem, when I'm typing on a webpage, sometimes it stops typing in on the webpage, and makes me type it in the web address bar, making me click the field i was typing in again. It's happened while I was filling this form out three times. Bought a new keyboard, thinking my tab button was stuck, didn't help. Can I get help for both of these two problems? Doesn't happen in Chrome or IE, but lets face it. . .they are both terrible, I like Firefox.

    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem.
    *Switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance
    *Do NOT click the Reset button on the Safe Mode start window
    If it works in Safe Mode and in normal mode with all extensions (Firefox/Tools > Add-ons > Extensions) disabled then try to find which extension is causing it by enabling one extension at a time until the problem reappears.
    Close and restart Firefox after each change via "Firefox > Exit" (Windows: Firefox/File > Exit; Mac: Firefox > Quit Firefox; Linux: Firefox/File > Quit)
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes

  • ICE Differential package upload - object deletion

    I am having an issue with uploading a differential package into another KM repository. As I understand, a differential package will download the data necessary for an incremental update, i.e. any renamed, new or deleted objects will be updated in the target repository.
    The issue that I am seeing is that when I delete an object from the source KM, and run through the usual steps:
    - create a link to the node that the deleted object was under (other objects still exist)
    - create a differential package (root > ice > root  >  ice  >  Syndicator  >  Global Offer > differential package)
    - save the zip to local
    - upload the package to the new environment using: Content Management > Content Exchange > Package Upload
    The deleted object is not removed from this environment. Am I missing a step, doing something wrong or is the deletion just not possible? Any help would be appreciated,
    thanks
    James
    Message was edited by: James West

    Hi James,
      Have you received a reponse to this yet, as the problem I am facing seems to be almost identical to this.
    Thanks,
    Gary

  • Objects deletion through policy object manager

    Hi,
    I have a query, I need to remove list of objects which are not referred tro any rules through policy object manager of CSM. Now as per our deployment> first we need open an acitvity in CSM> Open Policy Object Manager> Delete object whichever require>approve the activity.
    So now my question is that do we need any deployment job for this? If not, than it is gone live straight?
    My implimentors says we do not need any deployment jobs as we do not made any policy changes of any firewall.

    LV2009 added a function which is probably crucial to delivering a nice solution to this problem. The function is called Preserve Run-time Class. It allows you to propogate the strict type of an object through a subVI that is coded to operate on a more generic class type.
    Here's an example written in LV2009 for a simple class that stores any other LV class object by name.
    Jarrod S.
    National Instruments
    Attachments:
    Temp.zip ‏33 KB

  • Transporting PI/XI object deletions beyond the first target system?

    Hello,
    we're running PI7.0 at the site I'm working at, and our PI transports are controlled via CMS.
    I'm experiencing a problem in transporting Deletions of integration directory objects.
    From Development to the first target Test system, the Deletions are moved through fine. This 'D' --> 'T 'transport forms the first track.
    The second track moves the same transports from 'Q' (after being assembled and approved out of 'T') to 'I' and then to 'M' systems.
    However, beyond the 'T' system, the Deletion objects seem to disappear off the transport!
    Other items on the same transport (eg. object creations or changes), still get transported fine.
    The outcome is that we seem to only be able to delete objects via transport in the first target system, not any follow-on systems.
    Does anyone know what could be causing the problem?
    many thanks
    DaveR

    Hi Marco,
    For Java development, you will still use NWDI until the assembly step. The result will be a software component archive, which can then be moved over to CTS+.
    For XI development, it is possible to use CTS+ instead of of CMS at the transport technology.
    You also might want to take a look into the following document, which highlights the combination of CTS+ and NWDI:
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/a0d96022-9df5-2910-90b8-8e0580e0987e
    Interesting quote:
    "Usage type DI is by no means invalidated by this [CTS+] scenario: The NWDI is the recommended environment for JAVA-based development."
    Best regards,
    Dennis

Maybe you are looking for

  • Data Committed instead of Rollback after Deadlock Error in SQL Server 2008 R2 (SP2)

    We're having a strange issue which is occurring only with one Customer having SQL Server 2008 R2 (SP2). Basically we have multiple threads uploading data and when an error occurs (like deadlock or any other error). The deadlock victim (process/transa

  • TS3999 Duplicate calendar entries in iCloud

    I am really struggling with duplicate entries within iCloud.

  • RAID for 2010 iMac

    I currently use a 2.93 GHz 27" iMac that was released in July 2010. My 8TB Rocstor RAID is nearly out of space - I configured it as in RAID 5 mode and there's around 500GB left. I haven't had any port modification done on the system, for example, hav

  • Tying together 2 Linksys Wireless AP/Routers

    I have 2 Linksys Router/Wireless Access Points and wish to connect them together. I know the basics of wiring a network and I'm just asking here to confirm if I have to change the IP address of the second router to be able to access the setup page fo

  • 0SD_C03 cube contains data that does not exist  in PSA

    Hi, We have a problem with data difference in cube and PSA. Firstly we deleted PSA extactly then extracted data from r3 to PSA. PSA has 2 records now. Then we extracted data to cube (0SD_C03) via DTP (using 2LIS_13_VDITM transformation rules). Howeve