0

I am getting an error when I run a C code, which converts the count number to brightness temperature of satellite data. It is giving me segmentation fault. I am pasting my code here. Can any one suggest me where the problem with the code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N150 150

int main(int argc, char *argv[]){
  int   lon_tl=80,  lat_tl=60;
  int   lon_br=200, lat_br=-60;
  float lon_inc=0.04;
  float lat_inc=0.04;

  FILE  *fp;
  char  outputfilename[N150];
  float tmp_NX, tmp_NY;
  int   i, j, k, nk, NX, NY, tmp_count, count[N150];
  short *data_all;
  float *dt, tmp_tbb, tbb[N150];

  if(argc!=2){
     fprintf(stderr,"Usage: geoss2bt.c inputfilename1\n");
     fprintf(stderr," e.g.: ./geoss2bt IMG_DK01IR1_200705010030.geoss\n");
     exit(1);
  }
  tmp_NX=(lon_br-lon_tl)/lon_inc;  tmp_NY=(lat_tl-lat_br)/lat_inc;
  NX=(int)(tmp_NX);  NY=(int)(tmp_NY);
  data_all=(short*)malloc(2*NY*NX);
  dt=(float*)malloc(4*NY*NX);
  for(i=0;i<NY*NX;i++){
     data_all[i]=-999;
     dt[i]=-999.;
  }

  if((fp=fopen(argv[1],"r"))==NULL){
     fprintf(stderr,"*** input file (%s) cannot open ***\n",argv[1]);
     exit(1);
  }
  fread(data_all,sizeof(short),NY*NX,fp);
  fclose(fp);

  if((fp=fopen("tbbtable.txt","r"))==NULL){
     fprintf(stderr,"*** TBB table (tbbtable.txt) cannot open ***\n");
     exit(1);
  }
  k=0;
  while(!feof(fp)){
     fscanf(fp,"%d %f",&tmp_count, &tmp_tbb);
     count[k]=tmp_count;  tbb[k]=tmp_tbb;
     k++;
  }
  nk=k;
  fclose(fp);

  for(i=0;i<NY;i++){
  for(j=0;j<NX;j++){
     if(data_all[NX*i+j]<count[0]){ dt[NX*i+j]=-999.; }
     else if(data_all[NX*i+j]==count[0]) { dt[NX*i+j]=tbb[0]; }
     else{
        for(k=1;k<nk;k++){
           if(data_all[NX*i+j]<=count[k]){
            dt[NX*i+j]=tbb[k]-(tbb[k]-tbb[k-1])*(count[k]-data_all[NX*i+j])/(count[k]-count[k-1]);
            goto LOOP;
           }
        }
     }
     LOOP:;
  }
  }

  sprintf(outputfilename,"tbb_%s",argv[1]);
  fp = fopen(outputfilename,"w");
  fwrite(dt,sizeof(float),NX*NY,fp);
  fclose(fp);

  free(data_all);  free(dt);
  return 0;
}
  • What is the input ? – Jabberwocky Sep 08 '16 at 06:40
  • 5
    Before asking for help have you actually done any basic debugging on your own. Like using a debugger and/or debug print statements to trace the execution of your program. The debugger will tell you exactly which line is causing the seg fault for starters. – kaylum Sep 08 '16 at 06:41
  • 1
    And are you deliberately trying to make your code hard to understand with poor indentation, no comments, multiple statements on a line, lack of spaces between tokens, non-obvious and too similar variable names, etc? – kaylum Sep 08 '16 at 06:46
  • @kaylum when I pasted my code here it asked me for 4 character space. I think there it went wrong with the indentation. This is my first question here so excuse for mistakes. – Sridhar Mantripragada Sep 08 '16 at 06:56
  • 1
    The `while`loop where you read from the file doesn't check for buffer overrun (`k >= 150`). – Klas Lindbäck Sep 08 '16 at 07:51

2 Answers2

2

In end of code, you write:

fopen(outputfilename,"w");
fwrite(dt,sizeof(float),NX*NY,fp);
fclose(fp);

I think it should be:

fp = fopen(outputfilename,"w");
fwrite(dt,sizeof(float),NX*NY,fp);
fclose(fp);

But, you code is prone to errors:

Do not hesitate to comment your code, it will never be useless.

Community
  • 1
  • 1
Mathieu
  • 8,840
  • 7
  • 32
  • 45
2

I'm not sure, but maybe this is also may cause problems. According to my calculations you are allocate is this two lines, approximately 54MB of memory :

data_all=(short*)malloc(2*NY*NX);
dt=(float*)malloc(4*NY*NX); 

Which may cause segmentation fault through stack overflow, make sure you set your stack size big enough.

Why stack overflow causes segmentation fault instead of stack overflow in Linux?

Community
  • 1
  • 1
Anatoly Strashkevich
  • 1,834
  • 4
  • 16
  • 32