dynamic memory for a class object

Given a struct:


1
2
3
4
struct Element{
  string name;
  myClass *data;
};


If I have an Element vector and I want to set field 'data' with dynamic memory (I want to create a new object each time a function is called), how do I do it?
1
2
vector<Element> elements;
elements.push_back( { "my_name", new myClass() } );


Full example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <vector>
#include <string>
using std::string;
using std::vector;

class myClass;

struct Element {
  string name;
  myClass *data;
};

class myClass {
    
};

int main()
{
  vector<Element> elements;
  elements.push_back( { "my_name", new myClass() } );

  delete elements[0].data; // cleanup
}
Last edited on
thanks.
If the Elements are stored in std::vector, then they and all their members are placed into dynamically allocated memory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <string>
using std::string;
using std::vector;

class myClass {
    
};

struct Element {
  string name;
  myClass data;
  Element( string name ) : name(name) {}
};


int main()
{
  vector<Element> elements;
  elements.emplace_back( "my_name" );

  // cleanup is automatic
}


If you really need distinct dynamic memory for a member, then the object should manage its own resources:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>
#include <string>
using std::string;
using std::vector;

class myClass {
    
};

struct Element {
  string name;
  myClass* data;
  Element( string name ) : name(name), data(new myClass) {}
  Element( const Element& rhs ) : name(rhs.name), data(new myClass(*rhs.data) ) {} // copy ctor
  Element& operator=( const Element& rhs ) // copy assignment
  { name = rhs.name; *data  = *rhs.data; return *this; }
  ~Element() { delete data; }
};


int main()
{
  vector<Element> elements;
  elements.emplace_back( "my_name" );

  // cleanup is automatic
}
it is generally bad practice to put a pointer in a class and manage the memory yourself. but you asked, and this is how you do it.
- either the constructor allocates the memory with new, or it sets the member to null.
- if the constructor sets to null, then a setter function will allocate the memory. The setter function will check null, and if not null, you have a choice of delete and re-allocate or ignore the call to setter (its already been called).
- the destructor will call delete.
- you need to ensure your class will function with rules of 3/5.
this is a LOT of bloat, a lot of things to screw up, more things to test, and so on. Its terrible.
After I allocate a new element, I want to change a member of class myClass. How can I do it?
Last edited on
In my example, elements[0] is an individual Element object, which contains a name object and a pointer-to-myClass object.

If you had
1
2
3
class myClass {
    int some_member_variable;
};

You could do: elements[0].data->some_member_variable, that's how you would access it.
e.g. elements[0].data->some_member_variable = 42;
Last edited on
Topic archived. No new replies allowed.