?
Solved

strcat Segmentation fault (core dumped), C++

Posted on 2004-11-06
9
Medium Priority
?
455 Views
Last Modified: 2008-01-16
I have a segment of code in C++, when I run exec it give me an "Segmentation fault (core dumped)". Can someone help me to fix the problem.

char* str1;
str1 = (char*) malloc (3);
str1 = "abc";

char* str2;
str2 = (char*) malloc (3);
str2 = "def";

strcat(str1, str2);

It gives me run time error "segmentation fault"
0
Comment
Question by:david_2005
  • 4
  • 4
9 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 750 total points
ID: 12513683
char* str1;
str1 = (char*) malloc (3+3+1);   // should be enough place for 2 strings and NULL in the end
strcpy(str1,m "abc");

char* str2;
str2 = (char*) malloc (3+1);     // place for 3 characters and null
strcpy(str2, "def");

strcat(str1, str2);
0
 

Author Comment

by:david_2005
ID: 12513770
Thank you for your help.

I have other question

when I do  system("echo nutsh | ls")  where 'nutsh' is my executable file, it will output the 'ls' result for me on the screen. I want to know is it actually exec the 'nutsh' file or it just exec the 'ls' form "/bin/ls"
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12513835
it sends the string "nutsh" to ls. However "ls" ignores what it's getting from input and just lists the directory.
In other words, you are executing /bin/echo and /bin/ls but not nutsh.
Probably this is not what you intended. What is your aim?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:david_2005
ID: 12513848
I want to execute an external executable file (nutsh),

here is how my code and exec file links:

http://www.cox-internet.com/nguyen/work.cpp

http://www.cox-internet.com/nguyen/nutsh

Please tell me what have I done wrong, and how can I fix it

Thank you in advance
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12514246
First of all , remove the "echo" from sysem as in system("./tmp/netsh |...")
0
 

Author Comment

by:david_2005
ID: 12514263
I just wonder we may have to use  execl()  to fix the probem
0
 
LVL 11

Assisted Solution

by:lbertacco
lbertacco earned 750 total points
ID: 12516162
Well, if you want to redirect output (that is: use the pipe "|") it's much easier to use "system" then "execl".
0
 

Author Comment

by:david_2005
ID: 12516185
Thank you for your help.
I got this problem solve by myself already.
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12517354
Well, you should give points anyway since we answered your question and told you where the problem with your code was.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question