Why do i get segmentation fault upon exiting main()
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.
Why do i get segmentation fault upon exiting main()
Hi,
I can't seem to figure out why I am getting the following run-time error when i run my C program.
This is a trace from the debug tool DDD:
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program received signal SIGSEGV, Segmentation fault.
0x4008b27d in _IO_default_xsgetn () from /lib/libc.so.6
(gdb)
This happens at the very end of my main method:
int main(){
getJobNodes();
readJobNodes();
makeTree();
free(nodeProcs);
printf("returning from main...");
return 0;
}
I know the general reason why segementation faults occur - trying to access memory which is outside the scope of the program - common when string handling is done incorrectly.
But this has got me really stumped!!
Any help would be most appreciated! If more information is needed PLEASE say so....i'll do whateva it takes to get some help with this one!
if((ifp=fopen("tmp_out.txt","r"))==NULL){
printf("Unable to open tmp_out.txt.\nFile may not exist.\nExiting...");
exit(1);
}
//read in the user
if((tempString = getString(ifp)) != "EOF"){
tempLength = strlen(tempString);
strncpy(user,tempString,tempLength);
strncat(user,nullTerminator,2);
}
printf("The user is: %s\n",user);
//read in the master node
if((tempString = getString(ifp)) != "EOF"){
tempLength = strlen(tempString);
strncpy(masternode,tempString,tempLength);
strncat(masternode,nullTerminator,2);
numNodes++;
}
printf("The master node is: %s\n",masternode);
printf("Other compute nodes are: \n");
//read in the other nodes if any
for(i = 0; (tempString = getString(ifp)) != "EOF" ; i++){
addName(tempString,i);
printf("%s\n",nodeNames[i]);
numNodes++;
}
}
/* RSH to each node the job is running on and build
the process tree for the user running the job
on that node.
*/
void makeTree()
{
char *command;
char *nodeName;
STRING hostName;
int j,comm;
int hostNameLength = 50;
printf("The master node is: %s\n",masternode);
//rsh to master node
sprintf(command,"rsh %s getproclist %s > %s_proclist.txt",masternode,user,masternode);
system(command);
printf("%s\n",command);
I think I see one problem right here in readJobNodes():
//read in the user
if((tempString = getString(ifp)) != "EOF"){
tempLength = strlen(tempString);
strncpy(user,tempString,tempLength); strncat(user,nullTerminator,2);
}
The bolded line should be user[tempLength] = '\0';
strncpy() won't null-terminate user for you since you're only copying strlen(tempString) bytes from tempString, so your strncat() call doesn't actually know where the end of user is. Also, in addName() you should get rid of the +1 for the null-termination. So the line (nodeNames[index])[length+1] = '\0'; should be changed to (nodeNames[index])[length] = '\0';. With the +1 you're leaving room for one byte of garbage before the null-terminator.
I kind of stopped there. Let me know if you're still having problems after fixing all your strncpy() issues.
Here's an exerpt from 'man strncpy':
The strncpy() function is similar, except that not more
than n bytes of src are copied. Thus, if there is no null
byte among the first n bytes of src, the result wil not be
null-terminated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.