Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | bilsch <king621@comcast.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Long Question: What might cause the seg fault? |
| Date | 2016-06-21 15:04 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <nkcdmc$usf$1@dont-email.me> (permalink) |
Please see code samples #1 and #2 below. Code sample #1 works fine. The
output from sample #1 is shown below.
Code sample #2 below, is part of a much larger program. I think sample
#2 is equivalent to function fdmode() in code sample #1. Sample #2 gets
segmentation fault during the second iteration of function fdmode(). The
output from sample #2 is also shown below. The seg fault occurs the
second time fdmode() in sample #2 is called. The seg fault happens
after the following lines:
fdsel=quorem.rem;
printf("fdsel = %i\n",fdsel);
QUESTION: I don't think code sample #2 contains the underlying cause of
the seg. fault. There is something wrong in the larger program of which
sample #2 is a small part. What might this be?
Note: there is no other dynamic memory allocation than that shown.
Thanks. Bill S.
************* output of sample #1 (works fine) *************
fdsel = 1
w1=0.001000 w2=8.521000 w3=0.251593 nfgraf=2
fdsel = 0
w1=9.411000 w2=3.141593 w3=8.438000 nfgraf=2
fdflag = 0
**************** output of sample#2 ***********************
9.411, 1.937, 8.438, 0.001, 4, 7, 550, 325, 200, 200, 10, 6000},
7.388, 3.212, 4.428, 0.001, 3, 12, 650, 75, 350, 200, 15, 9900},
5.064, 2.101, 3.429, 0.001, 5, 1, 600, 300, 200, 125, 10, 6000},
37.500, 8.508, 0.001, 0.001, 5, 8, 600, 325, 200, 150, 20, 2400},
0.074, 6.224, 0.043, 0.010, 8, 5, 300, 300, 200, 200, 20, 2400},
0.537, 0.188, 5.493, 0.001, 6, 11, 600, 50, 200, 200, 0, 6000},
4.853, 3.434, 9.815, 0.001, 5, 1, 600, 350, 200, 200, 15, 6000},
1.861, 0.054, 0.981, 0.007, 11, 4, 400, 400, 200, 150, 0, 8000},
3.051, 7.751, 0.798, 0.001, 10, 4, 175, 400, 150, 150, 0, 8000},
0.066, 0.490, 9.919, 0.001, 10, 3, 600, 300, 200, 125, 0, 6000},
0.915, 0.573, 6.685, 0.010, 1, 7, 600, 300, 200, 200, 47, 5000},
3.300, 0.084, 0.810, 0.010, 6, 7, 650, 325, 200, 200, 6, 8000},
6.988, 0.035, 6.775, 0.001, 7, 3, 600, 300, 200, 200, 0, 8000},
6.306, 0.720, 0.058, 0.001, 9, 10, 150, 175, 150, 150, 6, 2400},
0.612, 0.517, 7.163, 0.001, 10, 6, 150, 350, 150, 150, 6, 8000},
8.130, 0.036, 0.325, 0.010, 1, 5, 600, 300, 200, 200, 0, 5000},
fdsel = 14
nfgraf=16
fdsel = 5
Segmentation fault (core dumped)
[code]
****************** code sample #1 **********************
int i=0, nfgraf=0, nx=0, n1=0, n2=0, rnum, fdflag=1, fdsel;
float w1=0.5, w2=2.0, w3=0.14159;
float *fw1,*fw2,*fw3;
char ch;
div_t quorem; // a structure
FILE *fp1;
int fdmode(void);
int main(int argc, char **argv){
srand(time(0)); // current time is seed for random gen
if ((fp1 = fopen("adata", "r"))== NULL)
{ printf("error opening file named 'adata'\n");
fclose(fp1);
goto end;
}
i=0;
while (ch != EOF)
{ if ((ch=fgetc(fp1)) == ' ') // ascii 0x20 between numbers
i++;
}
quorem=div(i,3); // 3 numbers in each record.
nfgraf=quorem.quot;
if (quorem.rem != 0 || quorem.quot == 0)
{ printf("file named 'adata' is not evenly divisible by 3\n");
fclose(fp1);
goto end;
}
rewind(fp1);
while (fdflag != 0)
{
fdflag=1;
fdmode();
}
return 0;
// end of main()
}
int fdmode(void)
{
if (nx==0) // nx was init. to 0
{
if ((fw1 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
if ((fw2 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
if ((fw3 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
for (n1=0;n1<nfgraf;n1++) // nfgraf starts at 1
{
fscanf(fp1,"%f %f %f",&fw1[n1],&fw2[n1],&fw3[n1]);
printf("%2.3f, %2.3f, %2.3f\n\n",fw1[n1],fw2[n1],fw3[n1]);
}
fclose(fp1);
} // end of if (nx==0)
rnum = rand(); // this part random selects data record
quorem = div(rnum,nfgraf);
fdsel=quorem.rem; // fdsel is zero based
printf("fdsel = %i\n",fdsel);
w1 = fw1[fdsel];
w2 = fw2[fdsel];
w3 = fw3[fdsel];
printf("w1=%f w2=%f w3=%f nfgraf=%i\n",w1,w2,w3,nfgraf);
nx++;
if (nx == nfgraf)
{
free(fw1);
free(fw2);
free(fw3);
fdflag=0;
// fdflag=1 when fdmode is called
printf("fdflag = 0\n");
}
return 0;
}
****************** code sample #2 **********************
int fdmode(void){
int fdsel,i;
float *fw1,*fw2,*fw3,*fk1;
int *fxf,*fyf,*fxof,*fyof,*fxm,*fym,*fdelay,*fglen;
if (nx==0) // nx was init. to 0
{
if ((fw1 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
if ((fw2 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
if ((fw3 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
if ((fk1 = malloc(nfgraf * sizeof(float))) == NULL)
printf("malloc error\n");
if ((fxf = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fyf = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fxof = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fyof = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fxm = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fym = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fdelay = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
if ((fglen = malloc(nfgraf * sizeof(int))) == NULL)
printf("malloc error\n");
for (i=0;i<nfgraf;i++) // nfgraf starts at 1
{
fscanf(fp1,"%f %f %f %f %i %i %i %i %i %i %i %i "
,&fw1[i],&fw2[i],&fw3[i],&fk1[i],&fxf[i],&fyf[i],&fxof[i],&fyof[i],&fxm[i],&fym[i],&fdelay[i],&fglen[i]);
printf("%2.3f, %2.3f, %2.3f, %2.3f, %i, %i, %i, %i, %i, %i, %i, %i},\n"
,fw1[i],fw2[i],fw3[i],fk1[i],fxf[i],fyf[i],fxof[i],fyof[i],fxm[i],fym[i],fdelay[i],fglen[i]);
}
fclose(fp1);
}
rnum = rand();
quorem = div(rnum,nfgraf);
fdsel=quorem.rem; // fdsel is zero based
printf("fdsel = %i\n",fdsel);
w1 = fw1[fdsel];
w2 = fw2[fdsel];
w3 = fw3[fdsel];
k1 = fk1[fdsel];
xf = fxf[fdsel];
yf = fyf[fdsel];
xof = fxof[fdsel];
yof = fyof[fdsel];
xm = fxm[fdsel];
ym = fym[fdsel];
delay = fdelay[fdsel];
glen = fglen[fdsel];
printf("nfgraf=%i\n",nfgraf);
nx++;
if (nx == nfgraf)
{
free(fw1);
free(fw2);
free(fw3);
free(fk1);
free(fxf);
free(fyf);
free(fxof);
free(fyof);
free(fxm);
free(fym);
free(fdelay);
free(fglen);
fdflag=0;
printf("fdflag=0\n");
}
return 0;
}
[/code]
Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread
Long Question: What might cause the seg fault? bilsch <king621@comcast.net> - 2016-06-21 15:04 -0700
Re: Long Question: What might cause the seg fault? BartC <bc@freeuk.com> - 2016-06-21 23:51 +0100
Re: Long Question: What might cause the seg fault? BartC <bc@freeuk.com> - 2016-06-21 23:53 +0100
Re: Long Question: What might cause the seg fault? bilsch <king621@comcast.net> - 2016-06-22 01:55 -0700
Re: Long Question: What might cause the seg fault? John Gordon <gordon@panix.com> - 2016-06-22 00:25 +0000
Re: Long Question: What might cause the seg fault? Jerry Stuckle <jstucklex@attglobal.net> - 2016-06-21 19:22 -0400
Re: Long Question: What might cause the seg fault? John Gordon <gordon@panix.com> - 2016-06-22 00:17 +0000
Re: Long Question: What might cause the seg fault? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-21 19:35 -0400
Re: Long Question: What might cause the seg fault? bilsch <king621@comcast.net> - 2016-06-22 03:36 -0700
Re: Long Question: What might cause the seg fault? John Gordon <gordon@panix.com> - 2016-06-22 00:29 +0000
Re: Long Question: What might cause the seg fault? bilsch <king621@comcast.net> - 2016-06-22 03:38 -0700
Re: Long Question: What might cause the seg fault? Barry Schwarz <schwarzb@dqel.com> - 2016-06-21 21:20 -0700
csiph-web