LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-16-2004, 04:11 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
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().

Last edited by Ephracis; 12-16-2004 at 04:28 PM.
 
Old 12-16-2004, 04:28 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 12-16-2004, 04:32 PM   #3
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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.
 
Old 12-16-2004, 04:35 PM   #4
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
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.

Last edited by dave_starsky; 12-17-2004 at 05:10 AM.
 
Old 12-16-2004, 04:37 PM   #5
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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.
 
Old 12-16-2004, 04:37 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.

Last edited by itsme86; 12-16-2004 at 04:42 PM.
 
Old 12-16-2004, 04:38 PM   #7
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
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
 
Old 12-16-2004, 08:53 PM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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
 
Old 12-17-2004, 01:28 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
segmentation fault??? max_rsr Programming 2 04-15-2005 08:47 PM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
Segmentation fault sin-x Slackware 2 01-12-2005 03:01 PM
strcat and direntp->d_name segmentation fault [I'm guessing the problem, plz propose] zeppelin Programming 3 04-12-2004 02:52 PM


All times are GMT -5. The time now is 12:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration