Scope variable in method and its persistence in C ++

Consider the following public method that adds an integer variable to the ints (private member) vector in a class in C ++.

KoolMethod()
{
  int x;
  x = 10;
  KoolList.Add(x);
}
Vector<int>KoolList;

      

But is this a valid addition to the vector ??? After calling the method, a local variable is created. This local variable runs out of scope the moment the runtime leaves this method. And because this local variable is allocated on the stack (when the method is called), any KoolList member points to an invalid memory location on the freed stack, which may or may not contain the expected value x. Is this an accurate description of the above mechanism?

Is there a need to create an int in heap storage using a "new" operator every time a value needs to be added to a vector as described below ????:

KoolMethod()
{
  int *x = new int();
  *x = 10;
  KoolList.Add(x);
}
 Vector<int*>KoolList;   

      

+2


a source to share


4 answers


But is this a valid addition to the vector?

Yes, the vector (standard library) stores copies.



Is there a need to create int in heap storage using the "new" operator

If you don't want objects to be copied or to work with polymorphic objects (see clipping objects ), you should use pointers.In this case, you'd better avoid handling deallocations manually and use wrappers (smart pointers or pointer containers), although for securing exceptions.

+2


a source


A Vector<int>

(at least if it std::vector

) stores the elements by value, so the calls add()

create a copy of the parameter object and store that copy in the array. So it doesn't matter what happens to the original object, the copy inside the vector is alive as long as the vector itself (unless explicitly deleted or overwritten, of course).

A Vector<X*>

might be more appropriate if you



  • want to work with polymorphic objects X

    ,
  • no need to copy objects X

    eg. because it is expensive or prohibited,
  • want to share the same objects between different sides.

Of course, none of them refers to int

, but only to real objects. However, it is better to store smart pointers in a vector instead of raw pointers, for example. or . They automatically manage object deletion for you. vector<auto_ptr<X> >

vector<shared_ptr<X> >

+2


a source


If you create a vector ints, it will not be a vector of pointers. Integers are stored by value, without pointers, so you won't run into problems with invalid memory addresses.

Here is some sample code that could cause this problem:

std::vector<int *> my_list;

void a_method() {
    int value = 2; // allocated on the stack
    my_list.push_back(&value); // pushes a pointer to the stack... not good
}

      

0


a source


Think about it: adding to a standard vector creates a copy of the added object.

In your first code snippet, you are using an ints vector, so you add a copy of the local int and you should be fine.

In the second code snippet, you are using a vector of pointers for int, so you will add a copy of the pointer ( not a copy of the object that this pointer points to). Since the object that the pointer points to remains in effect after the method exits (it is initialized with a new operator and is not deleted anywhere) everything will be fine.

0


a source







All Articles