This question already has answers here:
Is there a way to statically-initialize a dynamically-allocated array in C++?
(6 answers)
Closed 9 years ago.
In C++, I have a class CMyObject as follows, where CData is another class name:
Class CMyObject
{
CMyObject(CData& Data): m_Data(Data) {};
virtual ~CMyObject(void);
private:
const CData& m_Data;
}
When allocate one CMyObject instance, I can do as follows:
P = new CMyObject(MyData);
However, if I want to create an array of CMyObject, then can I do as follows?
P = new CMyObject(MyData)[10];
You can use an initialization list (I used 4 to save some typing, but you get the idea):
CMyObject* P = new CMyObject[4]{MyData, MyData, MyData, MyData};
But better still, use an std::vector:
std::vector<CMyObject> P(10, MyData);
Related
This question already has answers here:
Object creation on the stack/heap?
(7 answers)
What and where are the stack and heap?
(31 answers)
Stack, Static, and Heap in C++
(9 answers)
Closed 2 years ago.
I wrote this code and could someone explain how many objects are created in heap and stack? Is myStudent object in heap or stack?
Second question, is main method itself and the things inside of main method stored in stack?
class Student
{
public:
Student()
{
id = 0;
}
private:
int id;
};
Student studentCreator()
{
Student* s = new Student();
return *s;
}
int main()
{
Student myStudent = studentCreator();
return 0;
}
myStudent is on the stack. During the function call you are creating something in the heap and losing its reference.
Here you have myStudent on the stack because that function creates a student on the heap but return it dereferencing it, then you have a memory leak. The main function is stored on the stack by the operating system.
This question already has answers here:
Memory consumption after new then delete
(3 answers)
Closed 3 years ago.
I declare and assign vector pointer with shared pointer of class that has a big-size array pointer.
From deleting the vector pointer, I expect to free the memory from the big array. My question is that why the memory is NOT deallocated at the right moment after 'delete vec' before 'return 0'. From debugging mode, you can see it in the window task manager. Please let me know why 'delete vec' does not work as I expected.
class test
{
public:
test() { A = new double[500000000]; }
~test() { delete[] A; }
double *A;
};
int main()
{
vector<shared_ptr<test>> *vec =new vector<shared_ptr<test>>();
vec->push_back(shared_ptr<test>(new test()));
vec->push_back(shared_ptr<test>(new test()));
vec->push_back(shared_ptr<test>(new test()));
delete vec;
return 0;
}
I tested your code and there is no problem when deleting ptr.
This question already has answers here:
When to use "delete"?
(5 answers)
When to use new and delete
(4 answers)
Calling delete on variable allocated on the stack
(12 answers)
Do I have to delete struct pointer manually in C++?
(4 answers)
Closed 3 years ago.
Let's say I have a struct defined as so:
struct Barre {
int startString;
int endString;
Barre() { startString = endString = -1; }
Barre(int s, int e) : startString(s), endString(e) {}
bool exists() { return startString > -1; }
};
I will create an instance of this struct like this, for example:
Barre b = Barre(2, 4);
Let's say I insert this into a std::map<int, Barre> which is a member of a class, with a key of, for example, 3.
If I then create another Barre as above and overwrite the value of the map at key 3 with this new instance of the Barre struct, do I need to explicitly delete the old Barre object that I'm overwriting to prevent memory leaks? Or does it not persist once it is no longer stored in a map in this way?
Thanks for any help.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Accessing Class Veriables in C++
Hi I have following class hierarchy in C++
Class1 {
vector<Class2> vecClass2;
}
Class2 {
private:
const Class1 * ptrClass1;
vector<Class3> vecClass3;
public:
Class2(const Class1 * ptrClass1);
int intC2publicVar;
string strC2publicVar;
}
Class3 {
private:
const Class2 * ptrClass2;
vector<Class4> vecClass4;
public:
Class3(const Class2 * ptrClass2);
}
Class4 {
private:
const Class3 * ptrClass3;
vector<Class5> vecClass5;
public:
Class4(const Class3 * ptrClass3);
void class4Method() const;
}
In class4Method() I am doing something like this:
void Class4::class4Method() const {
const Class2 * pC2 = ptrClass3->ptrClass2;
int valClass2 = pC2->intC2publicVar;
//Above value is giving wrong value, I have no idea from where it is fetching the wrong value
string strVatClass2 = pC2->strC2publicVar;
//Above line of code cause run time termination of code and programs stops as soon as this line executes.
const Class2 c2 = * pC2;
//Above line of code cause run time termination of code and programs stops as soon as this line executes.
}
I have no idea why this is happening in class4Method() of Class4. Please help me how to solve this. My whole project is struck due to this problem and I could not move further without solving it.
Do I assume correctly that the const ClassN-1* ptrClassN-1 members are pointers to "owners" of the current instance and that the instance they point to lives in a vector in the ClassN-2?
In that case you simply forgot that vectors have this habit of moving their content around the memory as they reallocate to accommodate newly inserted values. Don't store pointers to objects in vectors but either store the objects in list (and take pointer to the instance in the list, because it's still copied) or store vector of pointers (and make sure you delete the elements in destructor).
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
deleting memory allocated in static function in C++
Hi All,
I have C++ class as follows
interface myInterface;
class anotherClass : public myInterface {};
class myClass {
private:
myClass() {}
~myClass() {}
typedef std::map<string, myInterface* > stringToClass;
static stringToClass s_stringToClass;
public:
static myInterface& getStringToclass(string name);
};
in above class for getStringToClass defintion is as follows
myInterface& myClass::getStringToClass(string name) {
stringToClass::iterator iter;
iter = s_stringToClass.find(name);
if(iter == s_stringToClass.end()) {
typedef stringToClass::value_type stringToClassPair;
anotherClass* pothClass = new anotherClass();
s_stringToClass.insert(stringToClassPair(name, pothClass));
return pothClass;
}
else {
return iter->second;
}
}
now my question is we are allocating memory in static function and returning a pointer of class type, but here i want to retrun a reference as we don't want to give control of pointer to user. And also i don't want to delete immedetily unless user asked to or want to delete at end of program How can we delete memory? As there is only instance of class will be there, as there are only static functions.
Thanks for the help.
Note:
You should reformat on the old thread, instead of creating new question. Anyway, I paste my answer here.
I think in your case, the destructor won't help, because there is no any object of MyClass.
I propose three ways
1. Don't store pointer, store the object itself.
2. Put the delete function into atexit; In your case
class MyClass
{
.....//Your already existing code
static void Destroy()
{
//iterate s_StringToClass and delete them
}
static void getStringToClass( string name )
{
struct DestroySetter
{
DestroySetter()
{
atexit( MyClass::Destroy );
}
};
static DestroySetter setter; //Setup the destoyer
//Your existing code here
}
Use smart pointer to manage the resource, shared_ptr is recommended.
Though I put a lot in second way, I suggest the 3rd way.