LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why do i get segmentation fault upon exiting main() (https://www.linuxquestions.org/questions/programming-9/why-do-i-get-segmentation-fault-upon-exiting-main-186718/)

J-Ral 05-27-2004 07:33 PM

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!

Thank you,

J-ral

itsme86 05-27-2004 07:53 PM

Well, without the code for getJobNodes(), readJobNodes(), and makeTree() I really can't help you.

J-Ral 05-27-2004 08:21 PM

Ok, they are big methods....

Should i post them here? Or send them to you?

J-Ral 05-27-2004 08:25 PM

Actually, they're not that big...i was thinking of something else.

Here they are:

void getJobNodes(){
int char_count =0;
int maxDigits = 7;
int jobNo;
char ch;
char command[MAXCMD];

printf("Please enter a valid job number: \n");

scanf("%d",&jobNo);
sprintf(command, "getnodes %d > tmp_out.txt",jobNo);
system(command);
}

void addName(char *name, int index){
int length = strlen(name);

strncpy(nodeNames[index],name,length);
//null terminate the name
(nodeNames[index])[length+1] = '\0';
}

void readJobNodes(){

FILE *ifp; //input file pointer
int i,numOfLines,k,j;
int nodeNo = 0;

char *name;
static char *tempString;
int tempLength;
STRING nullTerminator = "\0";

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);

//printf("Commands:\n%s\n",command);
printf("numnodes %d\n",numNodes);

for(j = 1;j<numNodes-1;j++){
printf("in loop");
nodeName = nodeNames[j];
//printf("%s\n",command,nodeNames[j]);
sprintf(command,"rsh %s getproclist %s > %s_proclist.txt",nodeName,user,nodeName);
printf("%s\n",command);

comm = system(command);

if( !( comm==(-1) || comm==127 ) ){
printf("Remote shelled to: %s\n",nodeName);

}

printf("exiting loop...");
}

printf("returning from makeTree...\n");

return;

}

itsme86 05-28-2004 02:13 AM

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.

Ma3oiS 05-28-2004 04:27 AM

Hi,

check if you have allocated memory for variables user and masternode.
And why you free(nodeProcs);? You didn't use nodeProcs at all in your program.

Ma3oiS


All times are GMT -5. The time now is 05:14 PM.