why segmentation fault?
HI~ GUYS I wrote a C program and I tried to compile it but it always remind me that there is a segmentation fault in line 49.Can anyone help me to find it out? thank you~
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 typedef struct _HEADER { 6 7 int num1; 8 int num2; 9 int num3; 10 int num4; 11 12 }HEADER; 13 14 void **Allocate2D(long int NBytes, int rows, int columns); 15 void readheader(HEADER *,FILE *); 16 void writeheader(HEADER *,FILE *); 17 18 19 int main(int argc,char *argv[]) 20 { 21 22 short ****datatable; 23 24 int i,j; 25 HEADER header; 26 27 /*if(argc !=2){ 28 printf("Cannot open the file for read\n"); 29 }*/ 30 31 32 FILE *fp1=fopen(argv[1],"r"); 33 FILE *fp2=fopen("newdata.dat","w"); 34 35 printf("The processing file is %s \n",argv[1]); 36 37 38 datatable = (short ****)Allocate2D(sizeof(short **),2,2); 39 40 for (i=0;i<2;i++){ 41 for(j=0; j<2; j++){ 42 datatable[i][j] = (short **)Allocate2D(sizeof(short),2,3); 43 } 44 } 45 46 readheader(&header,fp1); 47 48 for(i=0; i<2 ;i++){ 49 for (j=0; j<4; j++)fread(&datatable[i][j][0][0],sizeof(short),6,fp1); 50 } 51 fclose( fp1 ); 52 53 if ( fclose( fp1 ) == 0 ) 54 { printf("Success to close input file\n"); 55 } 56 57 58 /* sprintf(error_msg,"File1 Error '%s'\n",argv[1]); 59 perror( error_msg ); 60 exit(1);*/ 61 62 writeheader(&header,fp2); 63 64 printf("Can we get here?\n"); 65 66 for(i=0; i<2 ;i++){ 67 for (j=0; j<4; j++)fwrite(&datatable[i][j][0][0],sizeof(short),6,fp2); 68 fclose(fp2); 69 } 70 71 if(ferror(fp2)) { 72 printf("File2 Error\n"); 73 exit(1); 74 } 75 if ( fclose( fp2 ) == 0 ) 76 { printf("Success to close output file\n"); 77 } 78 return(0); 79 } 80 81 void readheader(HEADER *header,FILE *fp1) 82 { 83 printf("Here is the readheader function\n"); 84 fread(&(header->num1),sizeof(int),1,fp1); 85 fread(&(header->num2),sizeof(header->num2),1,fp1); 86 fread(&(header->num3),sizeof(int),1,fp1); 87 fread(&(header->num4),sizeof(int),1,fp1); 88 } 89 90 void writeheader(HEADER *header,FILE *fp2) 91 { 92 printf("Here is the writeheader function\n"); 93 fwrite(&(header->num1),sizeof(int),1,fp2); 94 fwrite(&(header->num2),sizeof(header->num2),1,fp2); 95 fwrite(&(header->num3),sizeof(header->num3),1,fp2); 96 fwrite(&(header->num4),sizeof(header->num4),1,fp2); 97 } 98 99 100 void **Allocate2D(long int NBytes, int rows, int columns) 101 { 102 void **pntr; 103 int i; 104 105 pntr = (void **)malloc(sizeof(void *)*rows); 106 pntr[0] = (void *)malloc(NBytes*rows*columns); 107 for(i=1; i<rows; i++)pntr[i] = pntr[i-1]+(columns*NBytes); 108 return pntr; 109 } |
Quote:
If you really need to construct 4D array this way, then that'll be something like this: Code:
short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4){ Code:
void free_4D(short**** array, int dimension1, int dimension2, int dimension3){ Something like this: Code:
struct array4d{ I recommend you to read some book about pointers, memory and C and read some real code examples. Also, you don't free memory in your program, which is a mistake. |
Indirection gone wild!:eek:
|
what do u mean? please? :)
|
Quote:
I prefer OOP container classes or 1D arrays with methods to access them as N-dimensional data. Quote:
I have no idea where you might get basic knowledge of using 1D array for storing N-dimensional data, how N-dimensional arrays are stored in memory, etc. I've picked that quite some time ago on old 8bit computer, I don't know which books cover that now. |
Sorry , but is this reference right?
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct _HEADER { int num1; int num2; int num3; int num4; }HEADER; void readheader(HEADER *,FILE *); void writeheader(HEADER *,FILE *); short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4); int main(int argc,char *argv[]) { int i,j; HEADER header; short ****datatable; datatable = (short ****)construct_4D(2,2,3,4); /*if(argc !=2){ printf("Cannot open the file for read\n"); }*/ FILE *fp1=fopen(argv[1],"r"); FILE *fp2=fopen("newdata.dat","w"); printf("The processing file is %s \n",argv[1]); for (i=0;i<2;i++){ for(j=0; j<2; j++){ datatable[i][j] = (short **)construct_4D(2,2,3,4); } } readheader(&header,fp1); for(i=0; i<2 ;i++){ for (j=0; j<4; j++)fread(&datatable[i][j][0][0],sizeof(short),6,fp1); } fclose( fp1 ); if ( fclose( fp1 ) == 0 ) { printf("Success to close input file\n"); } writeheader(&header,fp2); printf("Can we get here?\n"); if(ferror(fp2)) { printf("File2 Error\n"); exit(1); } if ( fclose( fp2 ) == 0 ) { printf("Success to close output file\n"); } return(0); } short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4){ short**** result; int i1, i2, i3; result = (short****)malloc(sizeof(short***)*dimension1); for (i1 = 0; i1 < dimension1; i1++){ result[i1] = (short***)malloc(sizeof(short**)*dimension2); for (i2 = 0; i2 < dimension2; i2++){ result[i1][i2] = (short**)malloc(sizeof(short*)*dimension3); for (i3 = 0; i3 < dimension3; i3++){ result[i1][i2][i3] = (short*)malloc(sizeof(short)*dimension4); } } } return result; } |
Quote:
Quote:
Read some books about C/memory functions. |
Also look for existing "C" libraries designed to manage 3-D and 4-D arrays!
|
All times are GMT -5. The time now is 02:44 PM. |