Link to home
Start Free TrialLog in
Avatar of david_2005
david_2005

asked on

strcat Segmentation fault (core dumped), C++

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"
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of david_2005
david_2005

ASKER

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"
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?
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
First of all , remove the "echo" from sysem as in system("./tmp/netsh |...")
I just wonder we may have to use  execl()  to fix the probem
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your help.
I got this problem solve by myself already.
Well, you should give points anyway since we answered your question and told you where the problem with your code was.