Michael Sullivan wrote:
> I know that a segmentation fault is the equivalent of a null pointer
> exception in Java, but I don't understand why it's happening here.  My
> full code is posted at
> http://www.espersunited.com/~michael/needhelp.txt .  Basically the
> problem is this:
> 
> class battle
> {
>    private:
>       Ally party[4];
> };
> 
> battle::battle()
> {
>    party[0] = Ally("Michael", 10, 7);
>    party[1] = Ally("Amy", 8, 13);
>    party[2] = Ally("Feli", 12, 15);
>    party[3] = Ally("Imp", 9, 4);
> }
> 
> class Ally : public Character
> {
>    public:
>       Ally(){}
>       Ally(char*, long, long);
>       ~Ally();

You still aren't using 'virtual' destructors.

> };
> 
> Ally::Ally(char* myname, long h, long m) : Character(myname, h, m)
> {
> printf("Arrived in Ally constructor for %s.\n", myname);
> }
> 
> Character::Character(char *myName, long myMaxHP, long myMaxMP)
> {
>    strcpy(name,myName);

Where/How is 'name' defined?  This is probably where your problem is.


>    maxHP = myMaxHP;
>    maxMP = myMaxMP;
> 
>    currentHP = maxHP;
>    currentMP = maxMP;
> }
> 
> Now I've tracked the SegFault down to the battle constructor,
> specifically the line where the 'Amy' Ally is being created.  The
> program enters the Ally constructor, and then SegFaults.  I have very
> little experience with gdb and ddd doesn't work at all.  Can anyone see
> the problem with this?  Am I out of stack space?  Do I need to start
> using the heap?

General observation:  You aren't using any pre-built C++ components. 
Brett already mentioned 'string', but I highly recommend BString.  If 
you are going to be tossing lots of string data around and aren't 
familiar with 'string', you might as well use the buff class I wrote 
(which offers a number of important features not found in 'string').

Also, you are statically declaring a party size to be 4.  If that will 
only allow 4 people and there will always be 4 people, then that's fine, 
but if you want it to be dynamic (4...6...3...more, less), then Block 
(or 'vector') is going to be more appropriate.

I recommend reading Safe C++ Design Principles.  I haven't had a seg. 
fault in almost 4 months at the library layer and over a year at the 
application layer.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to