Thread: Segmentation fault

  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 12:56 AM.

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

    Why do you need pointers at all?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Why not to use objects instead of pointers?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

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, 05: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, 12:53 PM