Thread: Why am I getting segmentation fault on this?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    Why am I getting segmentation fault on this?

    Hi


    I keep getting segmentation fault when the program gets to

    Code:
    c[i] = c[i + 1];
    This is in another constructor, I have included the code and the output below, Please let me know if you know why I get segmentation fault when I use c[i] = c[i + 1]


    Code:
    #include <stdio.h>
    #include <string.h>
    
    class Account
    {
    char customer[251];
    char accountNumber[16];
    int balance;
    
    public:
    Account();
    Account(char c[]);
    };
    
    int main()
    {
    Account test("    test");
    
    return 0;
    }
    
    Account::Account()
    {
    strcpy(customer, ",,,;");
    strcpy(accountNumber, "000000000000000");
    balance = 0;
    }
    
    Account::Account(char c[])
    {
     int i = 0;
    
    
    //while loop removes the spaces in front of the string.
     while (c[i] == ' ' || c[i] == '\t' || c[i] == '\n')
     {
        printf("Loop started\n");
        c[i] = c[i + 1];
        i++;
     }
    
    }

    Output

    Loop started
    Segmentation fault

    Thank You

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because " test" is not a writable string. It's a string literal, and cannot be changed in memory.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think this is a buffer overflow, too.
    Code:
    strcpy(accountNumber, "000000000000000");
    BTW, isspace() from <cctypes> is your friend.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by tabstop View Post
    Because " test" is not a writable string. It's a string literal, and cannot be changed in memory.
    But I'm trying to move characters in "char c[]" which is a character array, would you clarify this?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    I think now I understand, first I have to copy the string into the data member than I can move the characters in the string.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also:
    - Use std::strings, not character arrays. Otherwise, you will need to define a copy-constructor and assignment operator. Besides, it will make your life much easier.
    - Space-trimming is so common as to warrant being it's own function, outside of the class.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    char[], when in a function parameter list, is only an alias for char*.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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