LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segmentation fault on strcat() (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-on-strcat-267163/)

Ephracis 12-16-2004 04:11 PM

Segmentation fault on strcat()
 
I have this c++ code:
Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
        char *message;
        message = "first string\n";
        printf(message);
        strcat(message, "second string\n");
        printf(message);
}

This gives segmentation fault so I tried to change one line to this:
Code:

        char *message = (char*)malloc(512);
but no success. What is the problem here? I need to have the string as a pointer and I have to change it through the program with something like strcat().

itsme86 12-16-2004 04:28 PM

Code:

message = "first string\n";
String literals are stored in read-only memory. Try doing something like:
Code:

        char *message = (char *)malloc(512);
        strcpy(message, "first string\n");
        printf(message);
        strcat(message, "second string\n);
        printf(message);

That should work for you.

bm17 12-16-2004 04:32 PM

In the first case, you are trying to append to a constant string. The details depend on your compiler but maybe the string was statically allocated in non-writable memory, or maybe the memory after the first string is either invalid or in use by something else.

In the second case you are using a C-string which has not been initialized. There is no guarentee that malloc will return memory with a zero in the first byte.

I highly recommend that you use the c++ stl strings instead. They will handle the buffer allocation for you.

dave_starsky 12-16-2004 04:35 PM

char *message = (char *) malloc(strlen("first string\n") + 1);

is a handy way of malloc'ing some space for strings, what itsme86 said looks to me as though it should work fine.

bm17 12-16-2004 04:37 PM

Here's the c++ way to do it:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string first("first string\n");
cout << first;
first += "second string\n";
cout << first;
}


If you need to get a so-called C-string (really a char array) then you can use the first.str() method.

itsme86 12-16-2004 04:37 PM

Quote:

Originally posted by dave_starsky
char *message = (char *) malloc(strlen("first string\n"));

is a handy way of malloc'ing some space for strings

Maybe. But in this case it won't allocate enough memory to store the second string as well. That's why I left it at the 512 he tried.

EDIT: Also, don't forget that you need to allocate room for the '\0' so you'd need to malloc(strlen(str)+1) bytes instead of malloc(strlen(str)) bytes.

dave_starsky 12-16-2004 04:38 PM

Quote:

Originally posted by itsme86
Maybe. But in this case it won't allocate enough memory to store the second string as well. That's why I left it at the 512 he tried.
Thats a very good point :D

aluser 12-16-2004 08:53 PM

As a completely unneeded point, if you need to malloc a copy of a string you can use strdup() instead of malloc(), strlen(), and strcpy(). It saves you from forgetting that +1 :)

Hivemind 12-17-2004 01:28 AM

And don't cast the return value of malloc. It's not necessary in C for the code to compile, but doing so may hide a diagnostic if you've failed to include the proper header for malloc (or another that includes it), namely <stdlib.h>. In C++ there's no automatic conversion from void*, so a cast is necessary but you should never use malloc()/free() with C++ because it doesn't know about constructors/destructors.


All times are GMT -5. The time now is 03:50 PM.