Thread: Segmentation Fault Debug

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    30

    Segmentation Fault Debug

    I have ended up running into a specific problem in my own C++ project which is now crashing (throwing a segmentation fault error). I have created an entire project, which I have pasted below, which essentially recreates all of the code/classes/methods that are related to the segmentation fault error in as simple a form as possible. The project below behaves in exactly the same way as my more complicated personal project in that:
    1. it crashes due to a segmentation fault, and
    2. it runs successfully if the datatype used throughout the project is switched to being an integer instead of a string.

    Does anyone have a clue why this would be? I am a lost, beginner programmer, and the error/debugger messages in Codeblocks are not helping me out much (although I don't exactly know for what to look--emphasis on beginner programmer).

    An idea of what the code is doing in general: I have an array of child objects stored within a parent object. Every child object has a single attribute of type string. I want to be able to retrieve ("get") that attribute value of a specific child object stored within a given parent object, passing the "child objects array" index as a parameter.

    main.cc
    Code:
    #include <iostream>
    #include "parent.h"
    
    int main() {
      Parent ParentObject;
      std::cout << ParentObject.BadassGetObjectAttribute(5) << std::endl;
    }
    parent.cc
    Code:
    #include "parent.h"
    
    std::string Parent::BadassGetObjectAttribute(int x) {
      array_of_children[x].GetObjectAttribute();
    }
    parent.h
    Code:
    #ifndef PARENT_H
    #define PARENT_H
    
    #include "child.h"
    
    class Parent {
     public:
      std::string BadassGetObjectAttribute(int);
    
     private:
      Child array_of_children[20];
    };
    
    #endif
    child.cc
    Code:
    #include "child.h"
    
    Child::Child() {
      object_attribute = "this string is initialized";
    }
    
    std::string Child::GetObjectAttribute() {
      return object_attribute;
    }
    child.h
    Code:
    #ifndef CHILD_H
    #define CHILD_H
    
    #include <string>
    
    class Child {
     public:
      Child();
      std::string GetObjectAttribute();
    
     private:
      std::string object_attribute;
    };
    
    #endif

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your compiler should be complaining bitterly about this function:
    Code:
    std::string Parent::BadassGetObjectAttribute(int x) {
      array_of_children[x].GetObjectAttribute();
    }
    Probably something like "not all paths return a value". In fact no path returns a value, because nothing is returned from this function. You should probably return your string you get from GetObjectAttribute.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    Don't know why the compiler doesn't complain about that. Somehow, integer datatypes actually work perfectly in the code as written.

    The correct code, as you mentioned should be:
    Code:
    std::string Parent::BadassGetObjectAttribute(int x) {
      return array_of_children[x].GetObjectAttribute();
    }
    Thanks for the quick review.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nair View Post
    Don't know why the compiler doesn't complain about that.
    The reason is probably "you turned off compiler warnings". In Code::Blocks, you go to Settings->Compiler and Debugger and you get a bunch of options. You want to check at least the ones that say -Wall and -Wextra. If you want to use the debugger, you'll need to make sure -g is checked. There's probably no reason not to check -pedantic too.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    You are correct. Every flag in my "Compiler Settings" was unchecked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't debug the source of segmentation fault in my code
    By sourabhsinha in forum C Programming
    Replies: 2
    Last Post: 04-28-2011, 08:50 AM
  2. help with a segmentation fault.
    By squal in forum C Programming
    Replies: 1
    Last Post: 04-21-2009, 04:43 PM
  3. segmentation fault
    By incognito54 in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:04 PM
  4. segmentation fault
    By charleswong in forum Linux Programming
    Replies: 9
    Last Post: 04-09-2004, 05:32 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM