Segmentation fault

This is a discussion on Segmentation fault within the C++ Programming forums, part of the General Programming Boards category; I cant figure out why this seg faults's. Main.cpp Code: Charactor *ch = new Charactor(); int main(int argc, char** argv) ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

    Segmentation fault

    I cant figure out why this seg faults's.

    Main.cpp

    Code:
    Charactor *ch = new Charactor();
    
    
    int main(int argc, char** argv) {
    ch->addAnimation("WalkRight",sp);
    }
    }
    Charactor.h

    Code:
    #include <map>
    class Charactor{
        public:
            Charactor();
            void addAnimation(char *animationAlias, Sprite *sp);
            std::map<const char*, Sprite*, strCmp> *animations;
    };
    Charactor.cpp

    Code:
    #include "Charactor.h"
    
    Charactor::Charactor(){
        
    }
    void Charactor::addAnimation(char *animationAlias, Sprite *sp){ 
        (*this->animations)[animationAlias] = sp;
    }
    And its not because sp is NULL.

    Thanks
    Last edited by (Slith++); 05-06-2007 at 01:56 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    probably - because animations is not initialized pointer...

    Why do you need pointers at all?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    It is usually a good idea to give a small but complete example that illustrates your problem. What you've obviously done is remove things you think are irrelevant, so the code you've supplied should not even compile, let alone yield a segmentation fault when run.

    All that is possible is therefore a wild guess. My wild guess is that your Charactor constructor does not initialise the animations member to point at anything, so dereferencing it yields undefined behaviour.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Quote Originally Posted by grumpy View Post
    My wild guess is that your Charactor constructor does not initialise the animations member to point at anything, so dereferencing it yields undefined behaviour.
    Im pretty sure this is what is wrong also. What do I have to put in my Charactor constructor?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why not to use objects instead of pointers?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21