local objects

This is a discussion on local objects within the C++ Programming forums, part of the General Programming Boards category; Here in this code the function "foo" adds local objects to a global list. Then the function "bar" tries to ...

  1. #1
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31

    Smile local objects - solved

    Here in this code the function "foo" adds local objects to a global list.
    Then the function "bar" tries to access the list. In this simple example it worked.
    (I was expecting some garbage values or other errors like invalid memory access).
    So to ensure that "foo"'s stack values are destroyed, I made a large array of doubles
    (which I later changed to smaller and smaller, and ultimately to 1)
    in "bar". And now I get invalid values from the list and mem errors too.
    I was expecting this to happen. Because the objects were created on stack so they were
    gone!
    Now is there a better way to do the same i.e. :
    the "foo" function will create new objects
    and the "bar" function will access them.

    There is only way on my mind right now i.e. to create objects using "new".
    Help Please!

    Code:
    #include <iostream>
    #include <string>
    #include <list>
    #include <cmath>
    
    using namespace std;
    
    typedef list<string> mylist;
    
    mylist msg_list;
    
    void foo()
    {
    	string h("Hello");
    	string w("World");
    
    	msg_list.push_back(h);
    	msg_list.push_back(w);
    }
    
    void bar()
    {
    	double vals[1024];
    	for (int i=0; i<sizeof(vals)/sizeof(double); ++i)
    	{
    		vals[i] = i * sin(3.1415/180.0 * i);
    	}
    
    	mylist::const_iterator it = msg_list.begin();
    	while (it != msg_list.end())
    	{
    		cout << "Msg: " << *it << endl;
    		++it;
    	}
    }
    
    int main()
    {
    	foo();
    	bar();
    
    	return 0;
    }
    Last edited by msp; 09-13-2007 at 03:24 AM. Reason: Oops! An error (pointed by Salem) corrected.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    sizeof() tells you the number of bytes, not the number of subscripts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Salem, sorry for that stupid error!
    Now the program works just fine :shock:
    I am expecting it to show invalid data or make invalid memory errors.
    Why is it working just fine? (Contrary to my belief!)

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Your variables are static globals so all functions have access to them.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Quote Originally Posted by siavoshkc View Post
    Your variables are static globals so all functions have access to them.
    I think it is a special case for string objects only.
    What if the objects are of other types?
    I am trying to do it with my own objects, and I will post them here.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Here in this code the function "foo" adds local objects to a global list.
    Except it doesn't, it makes a copy.

    > msg_list.push_back(h);
    This creates a copy of h, and stores that.

    Try it using a small class of your own so you can trace the instances of constructor, destructor, copy and assignment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Yeah Salem I am doing that!
    But I think (I've seen it in MSVC while writing this example) that the function "push_back"
    takes a reference. So no new copy should have been made.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    The function call itself might take a reference, but that doesn't take into account what happens inside push_back().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Well I tried whatever I could think of but the program runs fine.
    So why worry

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, standard containers make a copy of what you put in. Push-back takes a reference to be able to do it with only one copying of the object instead of two (one for the container, one when passing a copy to the function in the first place).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by msp View Post
    Yeah Salem I am doing that!
    But I think (I've seen it in MSVC while writing this example) that the function "push_back"
    takes a reference. So no new copy should have been made.
    That doesn't follow at all. In fact, a new copy MUST be made because a reference is not reassignable. You can't for instance, have "a vector of references." Ain't no such thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing local objects by reference
    By manav in forum C++ Programming
    Replies: 15
    Last Post: 03-31-2008, 07:32 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21