Segmentation Fault

This is a discussion on Segmentation Fault within the C++ Programming forums, part of the General Programming Boards category; I'm getting a segmentation fault and I don't know how to fix it. I have narrowed it down to where ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Segmentation Fault

    I'm getting a segmentation fault and I don't know how to fix it. I have narrowed it down to where it's occurring but I don't know how I am causing it. From my understanding a Segmentation Fault has something to do with the program either accessing illegal data or trying to illegally overwrite data.

    I'm going to post my code but be warned, it's really long and I understand if you don't want to help.

    There are 4 files (2 header and 2 .cpp). The first two are the driver programs that I was given. The last two are the files that I created to allow the driver program to work.

    Here's where the problem is. In:

    Code:
    //strdrv.cpp (driver program)
    
    void test10()
    
        {
    
        system("cls");
    
        cout << "10. Testing: String concatenation." << endl << endl;
    
        csis << "10. Testing: String concatenation." << endl << endl;
    
        String s10("DEF");
    
        String t10('H');
    
        String u10("ABC" + s10 + "G" + t10 + 'I');
    
        u10.setName("u10");
    
        u10.print();
    
        String v10('X' + u10);
    
        v10.setName("v10");
    
        v10.print();
    
        wait();
    
        }
    In bold is where the Seg Fault is occuring. Adding all of strings, objects, and char's together is calling some friend functions that I created in //string.cpp.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Here is the Driver Program header and .cpp

    Code:
    ...
    Code:
    ...
    Last edited by warfang; 04-23-2007 at 01:48 AM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Here is the program I wrote:

    Code:
    ...
    Code:
    ...
    Last edited by warfang; 04-23-2007 at 01:48 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> length = strlen(&oneCharBuf);
    If it is a single char, then the length must be 1. Also, strlen won't work because it expects a null terminated array, not a pointer to a single character.

    Also, in your destructor, you delete [] name. But you don't initialize name in any of your constructors. I didn't look at all the code to see if you are using it, but if you are, you should initialize it to null (0) if you don't assign new memory to it. That way delete [] name won't fail.

  5. #5
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Code:
    String::String(char oneCharBuf){
                        
                        length = strlen(&oneCharBuf);
                        buf = new char[length + 1];
                        buf[0] = '\0';
                        strcpy(buf, &oneCharBuf);
                        
                        }
    This should not work. A single character (ie 'H') is not null terminated, and therefore the behavoir of strlen is undefined. strcpy will therefore copy some random string (you will sometimes get lucky and only copy one char though).

    Also, doing buf[0] = '\0' is pointless because strcpy will copy over that value, and appends its own null terminator anyways. That may not be your problem, but try looking a little more at how C Strings work. They are tricky little buggers.
    Programming Your Mom. http://www.dandongs.com/

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Thanks so far. I'm going to try out all that you suggested. I'm also going to update the code that I wrote to reflect the changes. In all the + overloaded operators I just linked them to the += operator instead of having all of the excess code. That part seems to be working for now.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I just edited my code posts with update code. I changed a lot of stuff and commented other stuff out.

    The problem I'm having right now is with "test 17" in the driver program:

    Code:
    void test17()
    
        {
    
        system("cls");
    
        cout << "17. Testing: Substr function." << endl << endl;
    
        csis << "17. Testing: Substr function." << endl << endl;
    
        String s17("Jai Guru Dev"), t17;
    
        s17.setName("s17");
    
        t17.setName("t17");
    
        t17 = s17.substr(4, 8);
    
        s17.print();
    
        t17.print();
    
        wait();
    
        }
    I highlighted the probelm areas. The "substr(x,y)" function is supposed to take the string from the object, start at x position, and continue for y spaces making a new string with those characters. So in this instance t17 should equal "Guru Dev". For some reason when I compile it and run it t17 is equal to "Guru Dev Morgan". I have no idea how "Morgan" got there.

    I did some debugging and found that when I try and make a new character pointer to hold the memory something screws up and it makes a bigger character array and just fills it with junk. I've been trying to fix it but I can't figure out how. If you could help me that would be awesome!!!

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The reason is that you never append the terminating 0 character to the substring.

    Code:
    string[0] = '\n';
    //fill string here
    This is useless, because you'll overwrite the 0-character after all.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Thank you! I just figured it would just be taken care of by itself. I didn't initially mean for the program to take care of it but it just slipped my mind because I haven't been appending the null character to the end of any of the strings.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I just posted my updated code. Everything seems to be working fine on it's own. The problems I'm having though are with a few of the tests.

    If I compile the program as is, it will crash on Test 17 unless I comment out Tests 10, 11, and 12.

    Also test 12 works fine only when Tests 10 and 11 are commented out. It still allows me to run the code without crashing but it doesn't give me the right output.

    Does anyone know what might cause this?

    EDIT: Looks like I just fixed it 0.0. I'm going to take my code off for now.
    Last edited by warfang; 04-23-2007 at 01:47 AM.

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, 02: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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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