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"
david_2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
david_2005Author Commented:
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
lbertaccoCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

david_2005Author Commented:
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
lbertaccoCommented:
First of all , remove the "echo" from sysem as in system("./tmp/netsh |...")
0
david_2005Author Commented:
I just wonder we may have to use  execl()  to fix the probem
0
lbertaccoCommented:
Well, if you want to redirect output (that is: use the pipe "|") it's much easier to use "system" then "execl".
0
david_2005Author Commented:
Thank you for your help.
I got this problem solve by myself already.
0
lbertaccoCommented:
Well, you should give points anyway since we answered your question and told you where the problem with your code was.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.