Thread: Append or strcpy segmentation fault

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Append or strcpy segmentation fault

    hi there
    I was wondering if someone could help me.
    I'm writing a programme, where I have to return some char array or string.
    The problem is, it works only for the first time.. see
    Code:
    class CliLogic{
        private:
            char orient, state, * name;
            std::string m_buf;
        public:
            CliLogic(){state = 0; m_buf.reserve(1024);}
            void CM( const char * mes);        
    
    };
    Name receives a pointer from another class, so it should be valid (and actually IS valid, tested..). But take next method CM

    Code:
    void CliLogic::CM(const char * mes){
            printf("Start\n");
            m_buf = ""
            m_buf.append(name);
            printf(".");
            m_buf.append(mes);
            printf(".");
            m_buf.append("\r\n");
            printf(".");
            printf("%s\n",m_buf.c_str());        
    }
    On the first run it goes like "Start\n...Name message\r\nStart\n segmentation fault". Valgrind says it touches memory on 0x0, but I didn't figure out the fault. The size of the string (or char array, tested on both) shouldn't be a problem. Name is not bigger than 512 bits and message isn't longer than 5 chars + "\r\n".

    I have tried it with append and with strcpy (which was originally there), both resulting in segfaults.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eramol
    Name receives a pointer from another class, so it should be valid (and actually IS valid, tested..).
    So, the CliLogic object does not own the string represented by the name member, right?

    Quote Originally Posted by Eramol
    On the first run it goes like "Start\n...Name message\r\nStart\n segmentation fault". Valgrind says it touches memory on 0x0, but I didn't figure out the fault. The size of the string (or char array, tested on both) shouldn't be a problem. Name is not bigger than 512 bits and message isn't longer than 5 chars + "\r\n".
    You should initialise name to be a null pointer in the constructor, then check that both name and mes are not null pointers.

    It would be better if you posted the smallest and simplest compilable program that demonstrates the segmentation fault. The problem with claiming that something that is not shown "actually IS valid, tested" is that you may have made an oversight, e.g., your tests may show validity of the pointer in a context before it becomes invalid.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>m_buf.reserve(1024);
    A string is not a buffer. There is no need to reserve storage.

    >>printf("Start\n");
    Don't use printf. Use std::cout.

    >>m_buf.append(name);
    Or you could just type
    m_buf += name;

    >> m_buf = ""
    >> m_buf.append(name);
    Or you could just do
    m_buf = name;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    A string is not a buffer. There is no need to reserve storage.
    but if you know how much data you'll be putting in it, pre-allocating space can improve performance, especially if you don't append all the data at once. this can become very important if you're dealing with large amounts of data (several megabytes or more).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault With pointer to pointer strcpy
    By touchy in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 12:35 AM
  2. Segmentation fault
    By ksusha in forum C Programming
    Replies: 7
    Last Post: 04-27-2010, 10:04 AM
  3. strcpy(), 2 strings, and segmantation fault
    By jigidyjensen in forum C Programming
    Replies: 4
    Last Post: 03-20-2009, 01:42 PM
  4. Segmentation Fault on -> and strcpy line
    By 6thmercury in forum C Programming
    Replies: 5
    Last Post: 04-12-2005, 06:10 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread