On Dec 1, 2007 5:27 PM, Michael Sullivan <[EMAIL PROTECTED]> 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:

A segmentation fault can mean you are accessing memory you have no
permission to use, like if you are trying to access memory you have
not allocated (null pointer).

I didn't go through all of your code but a few things stuck out at me.

One thing I recommend is to stop using char * or char[] and use the
C++ string class, it'll eliminate some headaches with strings.

Also, instead of using a plain old array in your battle class, I'd use
a vector or or a list or similar kind of C++ container class, they are
a lot more flexible than plain old arrays, especially when it comes to
adding more entries into it.

Definitely learn how to use a C++ debugger... if using gdb isn't up
your alley, get Eclipse, KDevelop or Anjuta (I am assuming you are on
Linux) and use their graphical debuggers (which probably use gdb
underneath).

> 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();
> };
>
> 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);
>    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?
>
>
>
>
>
> To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
> Yahoo! Groups Links
>
>
>
>



-- 
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
    If I were to divulge it, it would overturn the world."
               -- Jelaleddin Rumi

Reply via email to