Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird argc change in main funciton

Status
Not open for further replies.

csripriya1

Programmer
May 13, 2003
35
US
Hi,
I am having a weird problem here. I was getting a segmentation fault immediately after a function call. The last command in that particular function was also executed very successfully. When it comes to the main function, it dumps a core. I used electric fence and gdb to debug it. After the segfault, on back trace, I got this message
#0 main (argc=0, argv=0x0) at Combi_main.cpp:165

Whereas my main function has 6 arguments. When I tried to print the number of arguments before this function call, I got the output as 6. I wonder what's going wrong.

Any suggestions and help will be highly appreciated.

Thanks a lot,
Priya
 
I was trying to reproduce the error. But I ended up with another problem. Now I get segmentation fault in the function servant, towards the end. When I use the electric fence with gdb I get the following error message:

Program received signal SIGSEGV, Segmentation fault.
0x08055c51 in servant(float*, int, int, float*, char*, char*, int) (chrom=0x6265445f,
nchrom=3106677, chrom_len=4, fit=0x4015afd4,
workdir=0xbfffdd10 "/home/chellapp/Combi_chem/Non_mpi_eugene_Debug/",
outputdir=0xbfffdcc0 "/home/chellapp/Combi_chem/Non_mpi_eugene_Debug/", arguments=6)
at servant.cpp:41
41 fit[i+1]=i+1;

We can see that the second argument nchrom has a huge number. When I print nchrom, just before function call as well as in the function, it prints 10. I am not sure what's wrong with this servant function call. Any help is highly appreciated.

This is the piece of my main function
#include <iostream>
using namespace std;
int main(const int argc, const char *argv[])
{
char inputfile[80],outputfile[80],zscorefile[80];
char workdir[80],outputdir[80];

if(argc<6)
{
cerr<<"Use: .exe combi.inp echrom.txt zscore workdir outputdir"
<<endl;
exit(1);
}

strcpy(inputfile,argv[1]);
strcpy(outputfile,argv[2]);
strcpy(zscorefile,argv[3]);
strcpy(workdir,argv[4]);
strcpy(outputdir,argv[5]);


ofstream outfile(outputfile,ios::eek:ut);
if(!outfile)
{
cerr<<outputfile<<" File could not be opened"<<endl;
exit(1);
}

ofstream ofile(zscorefile,ios::eek:ut);
if(!ofile)
{
cerr<<zscorefile<<" File could not be opened"<<endl;
exit(1);
}

//Get the user definitions here
user myself;
myself.get_user_info(inputfile);

//Get the template
tmpl Templ;
Templ.getData(myself.Tmplfile,myself.ntmpl);

//Get the size of fragment libraries
int *nfgs=NULL;
int totalfgs;
nfgs=new int[myself.ntmpl_sub];
totalfgs=get_fgs(nfgs,myself.ntmpl_sub,myself.libarr,
myself.libsubarr,myself.subarr);

chromosome *chrs =NULL;
float *chrom=NULL;
chrs=new chromosome[myself.nchrom+1];
chrom=new float [myself.nchrom*myself.ntmpl_sub+1];
cout<<"chrom is allowed to have "<<myself.nchrom*myself.ntmpl_sub+1<<" values "<<endl;


master(chrs,chrom,myself.nchrom,nfgs,myself.ntmpl_sub);

float *fit_nrg=NULL;
fit_nrg= new float[myself.nchrom+1];

cout<<"fit_nrg is allowed to have "<<myself.nchrom+1<<" values "<<endl;

servant(chrom,myself.nchrom,myself.ntmpl_sub,fit_nrg,workdir,outputdir,argc);

cout<<"Complete: "<<endl;
delete []chrom;
delete []chrs;
delete []nfgs;


return 0;
}

This is my servant function:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>

#include "Combi_main.h"

using namespace std;

int servant(float chrom[], int nchrom, int chrom_len,
float fit[], char workdir[], char outputdir[],int arguments)
{
int i,j,nc;
int dummy;
char str1[80];

for(i=0;i<nchrom;i++)
{
fit[i+1]=i+1;
cout<<" Chromosome fitness in servant "<<i+1<<" : "<<fit[i+1]<<endl;
}

return 0;
}


 
These are dangerous:
Code:
strcpy(inputfile,argv[1]);
strcpy(outputfile,argv[2]);
strcpy(zscorefile,argv[3]);
strcpy(workdir,argv[4]);
strcpy(outputdir,argv[5]);
You should use strncpy() and limit the size to 80 so you don't overflow your arrays. Or even better, use STL strings instead of char arrays.

I'm not sure what you're doing here, but if sizeof( fit_nrg ) <= nchrom, you'll overflow fit.
Code:
fit[i+1]=i+1;
You can switch to vectors instead of arrays to prevent some problems (as well as removing the need for newing memory).
 
Don't use float in scientific calculations! Use double only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top