segfault with dynamic allocation

... actually with trying to initialize an allocated object.

the other class works properly, the memory is what does the actual allocation, called inside the constructor. even if I put the allocation inside the constructor I still get the segfault when calling the init in the main, so its not that...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

robot::robot(int num_of_cmds, const char *nm)
{

try{commands = new command[num_of_cmds];}

 catch(std::bad_alloc) {cout << "Error allocating " << num << " bytes of memory\n";}
//here if i try commands[0].init(); no segfault occurs (see below)
strcpy(name,nm);//never mind this
};

int robot::init()
{
commands[0].init();
//all i try is to init one object, or just print out something from it, segfault occurs
}


all my main does is

1
2
robot set(14,"SETH"); //constructor call
set.init;
Last edited on
For one thing, your "set.init" should say "set.init()". Other than that, this code looks fine to me, with minimal assumptions about the missing accompanying code. If you can provide the rest of your code, someone here may be able to help you find the error.
Actually, it is not that... I guess it is because I allocated the memory, for each allocated object the default constructor is probably called(?) After that I try initializing it again, and that is the problem...
There is more code to this but I should not be posting it because it is an assignment...
anyway that is my guess correct me if I am wrong!
Last edited on
closed account (zb0S216C)
What does your destructor look like?

Wazzak
I'm still stuck on this... I just made my destructor for command, all it does is cout<<"dest"; and it does not print anything for these dyn. all. objects (it does for the ones that are created regularly)

In my "robot" constructor, I can access (print out and call constructors) for the command objects, but when it comes to trying this in other methods, the segfault occurs. I think it has something to do with my commands pointer... it is as though memory is only allocated inside the constructor, and then becomes unusable (I would say destructed but the destructors are not called). I keep changing my code and am trying any possible combinations, but all I get are these segfaults when I try to access DA objects...

Please someone help!
Try adding some debug output in your destructor to see if it gets run at all... You are most likely passing an object by value to a copy constructor, causing the destructor to get called for the temp object, which I assume deletes the commands pointer.
no, my main is really simple, for debugging all it does is call the constructor and then another method that should access these DA objects...
and the destructors are coded for both classes and both display what they should for "normally" created objects but not for the DA, and the DA seem to be constructed because I can display them (their private members) in the parent object...
I am doing an assignment and the logic is quite easy for me, I guess I almost have it done but this stuff is holding me back and its getting annoying, and I have not yet found an example of something similar...
Last edited on
Topic archived. No new replies allowed.