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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmentation fault: need help 1

Status
Not open for further replies.

maxouille

Vendor
Jun 16, 2003
2
BE
hi,
i got a problem with this program, the compilation happens without problem but when i execute him, i allways get a segmentation fault (core dumped).
i dont know anymore what to do so if somobody could help me, i thank him very mush by advance
here is the code :

% more training.c



#include <stdio.h>
#define BANDS 21



main(int argc, char *argv[])

{

typedef unsigned char Pixel;

int rows = 2500;
int cols = 3500;
int scansize = 100000;
int classes = 39;
int bands = BANDS;
long nrpixels;
int i, j,k;
int cellvalue;
int nrscans;
int remainder;

char *filelist[BANDS] = { &quot;eum12.bin&quot;,&quot;eum13.bin&quot;,&quot;eum14.b
in&quot;,&quot;eum22.bin&quot;,&quot;eum23.bin&quot;,&quot;eum24.bin&quot;,&quot;eum32.bin&quot;,&quot;eum33.bin&quot;,&quot;eum34.bin&quot;,&quot;eum
42.bin&quot;,&quot;eum43.bin&quot;,&quot;eum44.bin&quot;,&quot;eum52.bin&quot;,&quot;eum53.bin&quot;,&quot;eum54.bin&quot;,&quot;eum62.bin&quot;,
&quot;eum63.bin&quot;,&quot;eum64.bin&quot;,&quot;bos3.blk&quot;,&quot;herby3.blk&quot;,&quot;bare3.blk&quot; };






Pixel scanlijn [BANDS][100000];


FILE *ifp[BANDS], *samples;



nrpixels = rows * cols;
remainder = nrpixels % scansize;
nrscans = (nrpixels - remainder) / scansize;



for (i = 0 ; i < bands ; i++) {

if ((ifp = fopen(filelist, &quot;rb&quot;)) == NULL)
printf (&quot;File could not be opened!\n&quot;);

}

if ((samples = fopen(&quot;recusepix3.blk&quot;, &quot;rb&quot;)) == NULL)
printf (&quot;File could not be opened!\n&quot;);




for (i = 0 ; i < nrscans ; i++) {


for (j = 1 ; j <= bands ; j++)
fread(scanlijn[j], sizeof (Pixel), scansize, ifp[j-1]);
fread (scanlijn[0], sizeof(Pixel), scansize, samples);



for (j=0 ; j < scansize ; j++) {

if (scanlijn[0][j] != 0) {

for (k = 0 ; k <= bands ; k++)
printf (&quot;%d\t&quot; , scanlijn[k][j]);
printf (&quot;\n&quot;);
}



//printf (&quot;%d\t%d\t%d\t%d\t%d\t%d\t%d\n&quot;
, scanlijn[0][j], scanlijn[1][j], scanlijn[2][j], scanlijn[3][j],scanlijn[4][j],
scanlijn[5][j], scanlijn[6][j]);


}


}

for (j = 1 ; j <= bands ; j++)
fread (scanlijn[j], sizeof(Pixel), remainder, ifp[j-1]);
fread (scanlijn[0], sizeof(Pixel), remainder, samples);

for (j=0 ; j < remainder ; j++) {

if (scanlijn[0][j] != 0) {

for (k = 0 ; k <= bands ; k++)
printf (&quot;%d\t&quot; , scanlijn[k][j]);
printf (&quot;\n&quot;);
}

//printf (&quot;%d\t%d\t%d\t%d\t%d\t%d\t%d\n&quot;
, scanlijn[0][j], scanlijn[1][j], scanlijn[2][j], scanlijn[3][j],scanlijn[4][j],
scanlijn[5][j],scanlijn[6][j]);




}



}
%

Maxime Grondel
Belgium
 
Repost your code, but this time uncheck the checkboxes next to Emoticons/Smileys and Process TGML.
 
Not sure if this would solve your problem. But i see some programming errors in here. Modify and try again.

if ((ifp = fopen(filelist, &quot;rb&quot;)) == NULL)

should look like...

if ((ifp = fopen(filelist, &quot;rb&quot;)) == NULL)

Because you are not just having a pointer, but each is an array too. ifp is a file pointer array that points to the address of each opened file.

Similarly filelist is an array too. And you have to specify which file you wish to open. If you do not use the subscript how would the next file be opened.

You should have got compiler, but may be you have set to ignore the errors or something. Or some setting that you accidently set. But since your compiler did not complain and there was memory mishandling you got this error on your run, instead of compilation.

hope this helps.


Cheers
KK
 
Hmmm...
Strange, i modified this and it doesnt show up...
Anyways this is how it should look like

if ((ifp = fopen(filelist, &quot;rb&quot;)) == NULL)

Cheers
KK
 
if ((ifp{i} = fopen(filelist{i}, &quot;rb&quot;)) == NULL)

Use [] instead of {}. Html conversion is going on



Cheers
KK
 
Put a [tt][ignore]
Code:
[/ignore][/tt] before your code, and a [tt][ignore]
[/ignore][/tt] immediately after. This will keep your [tt][ignore][/ignore][/tt] subscript from making everything italic.

 
Until you post all your code in the clear (not with various bits missing due to italics), its hard to say

But some points to ponder
> Pixel scanlijn [BANDS][100000];
Is just over 2MB in size, which is an awfully large local variable.
Try
static Pixel scanlijn [BANDS][100000];

> for (j = 1 ; j <= bands ; j++)
This steps off the end of your array on the last iteration. Spraying 100K of data where it doesn't belong is not good.
 
hi,
first of all thanks a lot of helping me and sorry I was out for two days.
I tried the static Pixel scanlijn and this changes nothing at the segementation fault.Like You could see, i wrote this program in order to select values different from 0 in the array &quot;recusepix3.blk&quot;, then it has to select the value of the correspondent pixels in the 21 other files(arrays) of filelist,and finally print out this 21 values in a text file, and of line; then he can go to the next value different of 0 in &quot;recusepix3.blk&quot;

Again, thanks for your help
Here is my reposted code :

Code:
#include <stdio.h> 
#define BANDS 21 



main(int argc, char *argv[]) 

{ 

typedef unsigned char Pixel; 

int rows = 2500; 
int cols = 3500; 
int scansize = 100000; 
int classes = 39; 
int bands = BANDS; 
long nrpixels; 
int i, j,k; 
int cellvalue; 
int nrscans; 
int remainder; 

char *filelist[BANDS] = { 
&quot;eum12.bin&quot;,&quot;eum13.bin&quot;,&quot;eum14.bin&quot;,&quot;eum22.bin&quot;,&quot;eum23.bin&quot;,&quot;eum24.bin&quot;,&quot;eum32.bin&quot;,&quot;eum33.bin&quot;,&quot;eum34.bin&quot;,&quot;eum42.bin&quot;,&quot;eum43.bin&quot;,&quot;eum44.bin&quot;,&quot;eum52.bin&quot;,&quot;eum53.bin&quot;,&quot;eum54.bin&quot;,&quot;eum62.bin&quot;,&quot;eum63.bin&quot;,&quot;eum64.bin&quot;,&quot;bos3.blk&quot;,&quot;herby3.blk&quot;,&quot;bare3.blk&quot; 
}; 






Pixel scanlijn [BANDS][100000]; 


FILE *ifp[BANDS], *samples; 



nrpixels = rows * cols; 
remainder = nrpixels % scansize; 
nrscans = (nrpixels - remainder) / scansize; 



for (i = 0 ; i < bands ; i++) { 

if ((ifp[i] = fopen(filelist[i], &quot;rb&quot;)) == NULL) 
printf (&quot;File could not be opened!\n&quot;); 

} 

if ((samples = fopen(&quot;recusepix3.blk&quot;, &quot;rb&quot;)) == NULL) 
printf (&quot;File could not be opened!\n&quot;); 




for (i = 0 ; i < nrscans ; i++) { 


for (j = 1 ; j <= bands ; j++) 
fread(scanlijn[j], sizeof (Pixel), scansize, ifp[j-1]); 
fread (scanlijn[0], sizeof(Pixel), scansize, samples); 



for (j=0 ; j < scansize ; j++) { 

if (scanlijn[0][j] != 0) { 

for (k = 0 ; k <= bands ; k++) 
printf (&quot;%d\t&quot; , scanlijn[k][j]); 
printf (&quot;\n&quot;); 
} 



//printf (&quot;%d\t%d\t%d\t%d\t%d\t%d\t%d\n&quot;, scanlijn[0][j], 
scanlijn[1][j], scanlijn[2][j], scanlijn[3][j],scanlijn[4][j], 
scanlijn[5][j], scanlijn[6][j]); 


} 


} 

for (j = 1 ; j <= bands ; j++) 
fread (scanlijn[j], sizeof(Pixel), remainder, ifp[j-1]); 
fread (scanlijn[0], sizeof(Pixel), remainder, samples); 

for (j=0 ; j < remainder ; j++) { 

if (scanlijn[0][j] != 0) { 

for (k = 0 ; k <= bands ; k++) 
printf (&quot;%d\t&quot; , scanlijn[k][j]); 
printf (&quot;\n&quot;); 
} 

//printf (&quot;%d\t%d\t%d\t%d\t%d\t%d\t%d\n&quot;, scanlijn[0][j], 
scanlijn[1][j], scanlijn[2][j], scanlijn[3][j],scanlijn[4][j], 
scanlijn[5][j],scanlijn[6][j]); 




} 



}
 
You're still stepping off the ends of your arrays.
Here is your code - indented and annotated
Code:
#include <stdio.h>
#include <stdlib.h>

#define BANDS 21

int main(int argc, char *argv[]) {
    typedef unsigned char Pixel;

    int rows = 2500;
    int cols = 3500;
    int scansize = 100000;
    int classes = 39;   // unused
    int bands = BANDS;
    long nrpixels;
    int i, j, k;
    int cellvalue;      // unused
    int nrscans;
    int remainder;

    char *filelist[BANDS] = {
        &quot;eum12.bin&quot;, &quot;eum13.bin&quot;, &quot;eum14.bin&quot;, &quot;eum22.bin&quot;,
        &quot;eum23.bin&quot;, &quot;eum24.bin&quot;, &quot;eum32.bin&quot;, &quot;eum33.bin&quot;,
        &quot;eum34.bin&quot;, &quot;eum42.bin&quot;, &quot;eum43.bin&quot;, &quot;eum44.bin&quot;,
        &quot;eum52.bin&quot;, &quot;eum53.bin&quot;, &quot;eum54.bin&quot;, &quot;eum62.bin&quot;,
        &quot;eum63.bin&quot;, &quot;eum64.bin&quot;, &quot;bos3.blk&quot;, &quot;herby3.blk&quot;,
        &quot;bare3.blk&quot;
    };
    Pixel scanlijn[BANDS][100000];
    FILE *ifp[BANDS], *samples;

    nrpixels = rows * cols;
    remainder = nrpixels % scansize;
    nrscans = (nrpixels - remainder) / scansize;

    for (i = 0; i < bands; i++) {
        if ((ifp[i] = fopen(filelist[i], &quot;rb&quot;)) == NULL) {
            printf(&quot;File could not be opened!\n&quot;);
            exit( 1 );
        }
    }

    if ((samples = fopen(&quot;recusepix3.blk&quot;, &quot;rb&quot;)) == NULL) {
        printf(&quot;File could not be opened!\n&quot;);
        exit( 1 );
    }

    for (i = 0; i < nrscans; i++) {
        for (j = 1; j <= bands; j++)
            fread(scanlijn[j], sizeof(Pixel), scansize, ifp[j - 1]);    // scanlijn[j] array overstep
        fread(scanlijn[0], sizeof(Pixel), scansize, samples);

        for (j = 0; j < scansize; j++) {
            if (scanlijn[0][j] != 0) {
                for (k = 0; k <= bands; k++)
                    printf(&quot;%d\t&quot;, scanlijn[k][j]);                     // scanlijn[k] array overstep
                printf(&quot;\n&quot;);
            }
        }
    }

    for (j = 1; j <= bands; j++)
        fread(scanlijn[j], sizeof(Pixel), remainder, ifp[j - 1]);       // scanlijn[j] array overstep
    fread(scanlijn[0], sizeof(Pixel), remainder, samples);

    for (j = 0; j < remainder; j++) {
        if (scanlijn[0][j] != 0) {
            for (k = 0; k <= bands; k++)
                printf(&quot;%d\t&quot;, scanlijn[k][j]);                         // scanlijn[k] array overstep
            printf(&quot;\n&quot;);
        }
    }

    // all done, close files
    for (i = 0; i < bands; i++) fclose( ifp[i] );
    fclose( samples );

    return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top