LinuxQuestions.org
Help answer threads with 0 replies.
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 01-25-2011, 10:50 AM   #1
Ranny
LQ Newbie
 
Registered: Feb 2009
Posts: 13

Rep: Reputation: 0
string in c - strcat causes segmentation fault


I am trying to concat strings, and probably doing something conceptually wrong. This code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
const MAXL=256;
char *buffer;  
char *tmpch;  

buffer=malloc(MAXL*sizeof(char));  
tmpch=malloc(MAXL*sizeof(char));  

buffer="aaa";
tmpch="bb";
puts(buffer);
strcat(buffer,tmpch);

return 0;
}
compiled with gcc 4.4 on a Linux machine (Ubuntu), runs and gives a seg fault. Why?

Thanks!
 
Old 01-25-2011, 10:56 AM   #2
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,573

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Code:
buffer="aaa";
strcat(buffer,tmpch);
The first quoted line points buffer to a string constant.

The second line tries to overwrite the string constant.

You need to set the value of buffer with strncpy, not the assignment operator.

Last edited by dugan; 01-25-2011 at 10:58 AM.
 
1 members found this post helpful.
Old 01-25-2011, 12:53 PM   #3
Ranny
LQ Newbie
 
Registered: Feb 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks! now it's working...
 
Old 01-25-2011, 12:58 PM   #4
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,573

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
I should point out to you that:
  1. you have memory leaks, because the malloc'd memory is never freed
  2. to protect yourself from yourself, you should use strncat and not strcat
 
  


Reply

Tags
gcc, segmentation fault, string


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
C GTK+: String returned by gtk_file_chooser_get_filename gives segmentation fault youarefunny Programming 2 10-05-2010 04:50 PM
strcat segmentation fault mesh2005 Programming 4 10-16-2006 07:59 AM
Segmentation fault cause by String type nelnel Programming 2 08-23-2005 09:46 PM
Segmentation fault on strcat() Ephracis Programming 8 12-17-2004 01:28 AM
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 05:12 AM.

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