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-12-2008, 11:59 PM
|
#1
|
|
Member
Registered: Feb 2006
Posts: 33
Rep:
|
strcat usage with char* and char []
Hi,
I wrote a simple program in C under linux but now it segfault on me when I run it, below is a snippet of the code:
char *fullstr;
char *pStr = "second";
char arStr[10] = "third";
if( pStr != NULL )
{
strcat( fullstr, pStr );
strcat( fullstr, arStr ); <--- this causes segfault
}
Any idea?
|
|
|
|
12-13-2008, 08:37 AM
|
#2
|
|
Member
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138
Rep:
|
Try this...
Code:
char *fullstr;
fullstr = new char[strlen(pStr) + strlen(arStr)];
strcpy(fullstr, pStr);
strcat(fullstr, arStr);
|
|
|
|
12-13-2008, 02:32 PM
|
#3
|
|
Member
Registered: Jun 2007
Location: London, UK
Distribution: Ubuntu, RHEL, Fedora
Posts: 46
Rep:
|
The reason that your program is segfaulting is because you haven't allocated any memory for "fullstr". On my system your program (quite rightly) falls over at the first strcat rather than the second.
elprawn's example will work, but you can only use "new" in C++ rather than C. In C you would have to use malloc (or one of its related cousins such as calloc etc), thus:
Code:
fullstr = malloc(strlen(pStr)+strlen(arStr));
Don't forget to free the memory when you've finished with it:
|
|
|
|
12-13-2008, 05:25 PM
|
#4
|
|
Member
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138
Rep:
|
Ah yeah in C. Sorry!
|
|
|
|
12-13-2008, 07:46 PM
|
#5
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962
Rep: 
|
!= NULL isn't ever necessary except for clarity (unless you have a class in C++ without an operator bool or similar.)
Also:
Code:
fullstr = calloc(strlen(pStr)+strlen(arStr)+1);
Or:
Code:
fullstr = malloc(strlen(pStr)+strlen(arStr)+1);
fullstr[0] = 0x00;
//or...
memcpy(fullstr, pStr, strlen(pStr) + 1);
strcat looks for the end of the first string; it doesn't automatically start from the beginning for the first call to it. You have a 1-(255/256)^n chance of not writing out of bounds with the first call to it, where n is the size of the second string + 1, if you don't make sure a null-character is in place.
ta0kira
Last edited by ta0kira; 12-13-2008 at 08:13 PM.
|
|
|
|
12-13-2008, 10:35 PM
|
#6
|
|
Senior Member
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Rep:
|
Quote:
Originally Posted by elprawn
Code:
fullstr = new char[strlen(pStr) + strlen(arStr)];
|
Incorrect.
Where is the extra byte for trailing zero?
|
|
|
|
12-13-2008, 11:11 PM
|
#7
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962
Rep: 
|
Quote:
Originally Posted by ErV
Incorrect.
Where is the extra byte for trailing zero?
|
Already got that one.
ta0kira
|
|
|
|
12-13-2008, 11:12 PM
|
#8
|
|
Member
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138
Rep:
|
Quote:
Originally Posted by ErV
Incorrect.
Where is the extra byte for trailing zero?
|
Are you talking about null termination? I realised this afterwards but someone else already pointed it out.
|
|
|
|
| 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 02:52 AM.
|
|
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
|
|