CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2004
    Posts
    198

    variables in storage classes going out of scope

    Code:
    void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
    {
    	MyObject obj();
    	obj.SetMemberData(data,lengthOfData);
    	m_vector.push_back(obj);
    }
    is this code ok? or is it the case that since obj is declared in the function, when the function ends and it goes out of scope, the item at the back of the vector member in MyClass will just be garbage or null? m_vector is of type std::vector<MyObject>

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: variables in storage classes going out of scope

    Quote Originally Posted by peterworth
    Code:
    void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
    {
    	MyObject obj();
    	obj.SetMemberData(data,lengthOfData);
    	m_vector.push_back(obj);
    }
    is this code ok? or is it the case that since obj is declared in the function, when the function ends and it goes out of scope, the item at the back of the vector member in MyClass will just be garbage or null? m_vector is of type std::vector<MyObject>
    Well, the code you posted has one serious problem. You would get error C2228 with VC 6.0, saying SetMemberData must have class/struct/union type. Why? Have a look at Is there any difference between List x; and List x();?.

    For your original question, the easiest way is, try to get the value out of the vector in some other function and see what you get.
    Last edited by Ejaz; February 14th, 2005 at 07:03 AM.

  3. #3
    Join Date
    Oct 2004
    Posts
    198

    Re: variables in storage classes going out of scope

    Quote Originally Posted by Ejaz
    Well, the code you posted has one serious problem. You would get error C2228 with VC 6.0, saying SetMemberData must have class/struct/union type. Why? Have a look at Is there any difference between List x; and List x();?.

    For your original question, the easiest way is, try to get the value out of the vector in some other function and see what you get.
    ah yes sorry, so should it be:
    Code:
    void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
    {
    	MyObject obj = MyObject();
    	obj.SetMemberData(data,lengthOfData);
    	m_vector.push_back(obj);
    }
    ?

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: variables in storage classes going out of scope

    Quote Originally Posted by peterworth
    ah yes sorry, so should it be:
    Code:
    void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
    {
    	MyObject obj = MyObject();
    	obj.SetMemberData(data,lengthOfData);
    	m_vector.push_back(obj);
    }
    ?
    Code:
    void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
    {
    	MyObject obj; // = MyObject();
    	obj.SetMemberData(data,lengthOfData);
    	m_vector.push_back(obj);
    }
    thats all

  5. #5
    Join Date
    Aug 2004
    Location
    INDIA
    Posts
    260

    Re: variables in storage classes going out of scope

    For second question.

    vector:: push_back - Appends (inserts) an element to the end of a vector, allocating memory for it if necessary.

    MyObject - should have proper copy constructor, assignemnt operator otherwise vector shall use default copy, assignment opertaor to make a copy.

    Eventhough your local object get destroyed within a function, vector will have its own copy.
    If you feel this post is useful,
    Rate this Post by clicking right top corner (Rate this Post)
    Santhosh

    ***Add strength to your country - Interesting Poll
    ***

  6. #6
    Join Date
    Oct 2004
    Posts
    198

    Re: variables in storage classes going out of scope

    Quote Originally Posted by santoct2002
    For second question.

    vector:: push_back - Appends (inserts) an element to the end of a vector, allocating memory for it if necessary.

    MyObject - should have proper copy constructor, assignemnt operator otherwise vector shall use default copy, assignment opertaor to make a copy.

    Eventhough your local object get destroyed within a function, vector will have its own copy.
    thats good, i just wanted to make sure the copy constructor or assignment operator is actually used even though its not explicit in the code (just calling push_back) - there is no mention in the msdn page for push_back etc wether it copies the object or not.

  7. #7
    Join Date
    Oct 2004
    Posts
    198

    Re: variables in storage classes going out of scope

    one other thing - does the same happen the other way - eg if you do something to an object you got from vector::at are you affecting the object stored in the vector or a copy of it?

  8. #8
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: variables in storage classes going out of scope

    You can easily find yourself whether object is being copied or not. Look at the function you pass your class as a parameter to declaration. If parameter is being passed by value - copy constructor would inevitably be called (or default one if you haven't provide your own for the passing class).

    Also you can add debug print code to any function of passing class to check the fact and order of calling that function in test code.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: variables in storage classes going out of scope

    The signature of vector<>::push_back is:

    void
    push_back(const T& x);


    It still takes a copy when it adds it to the vector, though. Passing by value invokes the copy ctor for the function call. It says nothing about the internal workings of the function.

    peterworth: You can affect the stored value if you use an iterator or operator[]/at() as an lvalue. If you extract from the vector, you get a copy:
    Code:
       vector<int> ivec;
       
       // fill vector...
       
       vector<int>::iterator it = ivec.begin();
       *it = 2;	  // Directly affects stored value
       ivec[3] = 0; // Directly affects stored value
     ivec.at(3) = 0; // same as above
       int j = ivec[1]; // or ivec.at(1)
       j = 7; // Does not affect ivec[1];
    Last edited by Graham; February 14th, 2005 at 10:11 AM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: variables in storage classes going out of scope

    Graham
    You are right, it's just one-sided indication.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  11. #11
    Join Date
    Oct 2004
    Posts
    198

    Re: variables in storage classes going out of scope

    thanks everyone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured