ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi peoples,
I have a bit of code that compiles perfectly but when I run it I get a segmentation fault (core dumped) which is not fun. I have broken up my code (I had some nested functions etc) and found that the error is being generated at the below line of code.
Code:
temp = strcat("evusrseq=\"",temp1);
*not the most descriptive variable names but only used to break up the code to find the line that breaks. temp & temp1 are both strings.
** this code is being compiled by gcc v 2.95.3 on SunOS 5.9 if that makes any difference.
Basically this bit of c code looks fine to me as I have used strcat in other places with no problem so if anyone can see any reason that this would seg fault I would appreciate knowing why also
(I am hoping it is something obvious that I am just missing that when found makes me look like an idiot)
The destination should be a pointer to a character string instead of a pointer to a constant. You also need to make sure that you don't end up with a string larger then the memory you allocated. That could cause a runtime error which is harder to find.
No, it is not! It will fail if MAX_STRING == strlen("evusrseq=\""). Also, strncat's third argument is not the length of the buffer, but maximum amount of characters to read from the source. I.e. strncat is totally broken.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.