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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-16-2004, 04:11 PM
|
#1
|
|
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109
Rep:
|
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.
|
|
|
|
12-16-2004, 04:28 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
12-16-2004, 04:32 PM
|
#3
|
|
Member
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104
Rep:
|
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.
|
|
|
|
12-16-2004, 04:35 PM
|
#4
|
|
Member
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145
Rep:
|
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.
|
|
|
|
12-16-2004, 04:37 PM
|
#5
|
|
Member
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104
Rep:
|
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.
|
|
|
|
12-16-2004, 04:37 PM
|
#6
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
12-16-2004, 04:38 PM
|
#7
|
|
Member
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145
Rep:
|
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 
|
|
|
|
12-16-2004, 08:53 PM
|
#8
|
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
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 
|
|
|
|
12-17-2004, 01:28 AM
|
#9
|
|
Member
Registered: Sep 2004
Posts: 273
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:45 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|