dyinamic allocation of class inside class

I need to allocate memory for objects inside another object. The child would be a private member of the parent, and the parent would have a constructor that recievs an int that is the number of needed objects to be allocated. I did it like this but I am getting a segmentation fault when I try to modify the child objects...

(ps how many ways are there for dynamic allocation?)

1
2
3
4
5
6
7
8
class Parent
{
private:
	Child *children;

public:
 	Parent(n);//constructor	
}


This is the constructor:
1
2
3
4
Parrent::Parent(int n)
{
children = new Child[n];
}


Then I have a method that calls children[i].con() (i can be any number, even just 0 ) and that is where I get the segfault... the con constructor works when I test the child, but like this in the parrent something goes wrong...
which works when I test the child class, but like this I get a segfault...
Last edited on
Can you post more of the code? Like the child class and your test code.
I cannot as this is an assignment... The thing is I missed a lecture about this and it is confusing... I will give you my professors example, and hope someone can explain it just a bit! What I need is someone to explain how I will allocate objects for a "Command" class. I understand the point of it all, but not how it is done. Why is thevoid memory(const char*); inside the private sector? I understand the try, catch and so on. Its just the allocation I am confused with, especially because this is for a string and I need to allocate an array of Command objects!

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;

class robot {
      char *name;
      double mass;
      void memory(const char *);
   public:
      robot(void);
      robot(const char *, double);
      robot(const robot &); // copy constructor
      void operator=(const robot &); // = operator function
      void display(void);
      ~robot( ) {
	 if(name) {
	    cout << "in the destructor... the address about to be deleted is: " << (void *) name << endl;
	    display( );
	    cin.get( );
	    delete [ ] name;
	 }
      }
      friend ostream& operator<<(ostream &, const robot &); // custom ostream operator
							    // used to display robot data
};

void copy_robot(robot);

void robot::memory(const char *p) {
   int len = (p == NULL) ? 0 : strlen(p);

   try {
      name = new char[len + 1];
   }
   catch(std::bad_alloc) {
      cout << "Error allocating " << len + 1 << " bytes of memory\n";
      name = NULL;
   }
   if(name) {
      if(p == NULL)
	 strcpy(name, "");
      else
	 strcpy(name, p);
   }
}

robot::robot(void) {
   memory("univac.dat");
   mass = 500.0;
}
robot::robot(const char *str, double m) {
   memory(str);
   mass = m;
}

robot::robot(const robot &from) {
   memory(from.name);
   mass = from.mass;
}

void robot::operator=(const robot &rhs) {
   memory(rhs.name);
   mass = rhs.mass;
}

void robot::display(void) {
   cout << "robot name: '" << name << "' mass: " << mass << endl;
}

void copy_robot(robot c) { // variable 'c' will be a copy of a robot object!
   cout << "in the copy_robot( ), the address of the copy is: " << (void *) &c << endl;
   c.display( );
   cin.get( );
}

// this friend function accepts an ostream by reference on the left and
// a constant robot reference on the right and sends the robot's name and mass
// to the ostream named 'os'
ostream& operator<<(ostream &os, const robot &r) {
   os << "robot name: " << r.name << "robot mass: " << r.mass << endl;
   return os;
}

int main( ) {
   robot r1, r2("supercalafragalisticexpialodocious", 998.72), hal("H.A.L. 9000", 12345678.9);

   cout << "about to make a copy of robot r2... address is: " << (void *) &r2 << endl;
   copy_robot(r2);

   r1 = hal;

   robot *pr;
   pr = new robot("robot allocated on the heap", 1000.0);
   pr->display( );

   delete pr; // this will call the destructor for object pr

   cout << "----------------------------------------------\n";
   cout << r2 << r1 << hal;  // calling the operator<<( ) function above (cascading calls)
   cout << "----------------------------------------------\n";

   return 0;
}
Does i go outside of the range of child's allocated, you said it can be any number. I would do bounds checking.
No... if I just try for i=1 i still get the seg fault
Topic archived. No new replies allowed.