X
xz
Does this function have memory problem?
void pushSomthingIntoVector(vector<T> & v) { //T is a class
v.assign(0, T(100)) //T(int a) is a constructor of class T
v.push_back(T(50))
}
void anotherFunction() {
vector<T> v;
pushSomethingIntoVector(v);
cout << v[0] << endl;
cout << v[1] << endl;
}
T(100) and T(50) are created locally within the function
pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
function
? say, in the two lines of "cout" ?
Should I change the code as follows:
void pushSomthingIntoVector(vector<T> & v) { //T is a class
v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
v.push_back(*(new T(50)))
}
void pushSomthingIntoVector(vector<T> & v) { //T is a class
v.assign(0, T(100)) //T(int a) is a constructor of class T
v.push_back(T(50))
}
void anotherFunction() {
vector<T> v;
pushSomethingIntoVector(v);
cout << v[0] << endl;
cout << v[1] << endl;
}
T(100) and T(50) are created locally within the function
pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
function
? say, in the two lines of "cout" ?
Should I change the code as follows:
void pushSomthingIntoVector(vector<T> & v) { //T is a class
v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
v.push_back(*(new T(50)))
}