Results 1 to 10 of 18
I have been learning C for about a month or so and right now:
I am trying to write a C program that will read a pixmap image in PGM ...
- 04-11-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 8
Segmentation Fault?
I have been learning C for about a month or so and right now:
I am trying to write a C program that will read a pixmap image in PGM file format and then rotate the image 180 degrees. I have the code written out, but when I try to run it, it tells me I have segmentation fault. I have been trying to figure it out for hours and even debug it. When debugging, it points me to the line: pointer[i] = (int) calloc(N, sizeof(int));
But I can't figure out how to fix it. Any help much appreciated! Code below..
Code:#include <stdio.h> #include <stdlib.h> /* M is the height of the image and N the width */ int ** memAlloc(int M, int N) { int i; int ** pointer; pointer = NULL; pointer = (int **) calloc(M, sizeof(int)); if(pointer = NULL) { printf("Memory Allocation unsuccessful"); } for(i = 0; i < M; i++) { pointer[i] = (int *) calloc(N, sizeof(int)); if(pointer[i] == NULL) { printf("Memory Allocation Unsuccessful"); } } return pointer; } void inFile(char fn[], int **temp, int M, int N) { int i, j, k; char ch[4]; /* header information */ unsigned int value; FILE * fp; fp = NULL; /* initialization of pointer */ fp = fopen(fn, "r"); if(fp == NULL) { printf("File not opened"); exit(1); } /* ch[0] and ch[1] holds the version. i holds the width, j holds the height and k holds the max gray value */ fscanf(fp, "%c%c\n%d %d\n%d", &ch[0], &ch[1], &i, &j, &k); for (i = 0; i < M; i++) { for(j = 0; j < N; j++) { fscanf(fp, "%d", &value); temp[i][j] = value; } } fclose(fp); } void outFile(char outfile[], char c, int v, int N, int M, int max, int ** img) { int i, j; FILE * fout; fout = NULL; /* initialization of pointer */ fout = fopen(outfile, "w"); /* writes the header of the image */ fprintf(fout, "%c%d\n", c, v); fprintf(fout, "%d %d\n", N, M); fprintf(fout, "%d\n", max); if(fout == NULL) { printf("\n Error Opening Output File"); //getch(); exit(1); } /* rotate image by 180 degrees */ for(i = 23; ((i < M) && (i >= 0)); i--) { for(j = 6; ((j < N) && (j >= 0)); j--) { fprintf(fout, "%d ", img[i][j]); } } /* for(i = 0; i < M; i++) { for(j = 0; j < N; j++) { fprintf(fout, "%d ", img[i][j]); } } */ close(fout); } int main(int argc, char* argv[]) { int i, j; int M, N; /* M is the height, N is the width */ int ** img = 0; FILE *file; file = NULL; /* initialization of pointer */ char c; /* first character of the header */ int v, max; /* v holds the version */ char infile[25]; char outfile[25]; printf("\nInput File Name(25 characters MAX with .pgm extension) :: "); gets(infile); printf("\nOutput File Name(25 characters MAX with .pgm extension) :: "); gets(outfile); file = fopen(infile, "r"); if(file == NULL) { printf("File not opened"); //getch(); exit(1); } fscanf(file, "%c%d\n%d %d\n%d", &c, &v, &N, &M, &max); fclose(file); img = memAlloc(M, N); inFile(infile, img, M ,N); outFile(outfile, c, v, N, M, max, img); system("PAUSE"); return 0; }Last edited by ostangel; 04-11-2011 at 04:19 AM.
- 04-11-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The problem is here:
It should beCode:pointer = (int **) calloc(M, sizeof(int));
Because an int may be smaller than an int*, you are probably walking on your heap in the loop where you allocate each row.Code:pointer = (int **) calloc(M, sizeof(int*));
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-11-2011 #3Just Joined!
- Join Date
- Apr 2011
- Posts
- 8
- 04-11-2011 #4
- 04-11-2011 #5Just Joined!
- Join Date
- Apr 2011
- Posts
- 8
- 04-11-2011 #6
- 04-11-2011 #7Just Joined!
- Join Date
- Apr 2011
- Posts
- 8
[/QUOTE]Code:#include <stdio.h> #include <stdlib.h> /* M is the height of the image and N the width */ int ** memAlloc(int M, int N) { int i; int ** pointer; pointer = NULL; pointer = (int **) calloc(M, sizeof(* int)); if(pointer = NULL) { printf("Memory Allocation unsuccessful"); } for(i = 0; i < M; i++) { pointer[i] = (int *) calloc(N, sizeof(int)); if(pointer[i] == NULL) { printf("Memory Allocation Unsuccessful"); } } return pointer; } void inFile(char fn[], int **temp, int M, int N) { int i, j, k; char ch[4]; /* header information */ unsigned int value; FILE * fp; fp = NULL; /* initialization of pointer */ fp = fopen(fn, "r"); if(fp == NULL) { printf("File not opened"); exit(1); } /* ch[0] and ch[1] holds the version. i holds the width, j holds the height and k holds the max gray value */ fscanf(fp, "%c%c\n%d %d\n%d", &ch[0], &ch[1], &i, &j, &k); for (i = 0; i < M; i++) { for(j = 0; j < N; j++) { fscanf(fp, "%d", &value); temp[i][j] = value; } } fclose(fp); } void outFile(char outfile[], char c, int v, int N, int M, int max, int ** img) { int i, j; FILE * fout; fout = NULL; /* initialization of pointer */ fout = fopen(outfile, "w"); /* writes the header of the image */ fprintf(fout, "%c%d\n", c, v); fprintf(fout, "%d %d\n", N, M); fprintf(fout, "%d\n", max); if(fout == NULL) { printf("\n Error Opening Output File"); //getch(); exit(1); } /* rotate image by 180 degrees */ for(i = 23; ((i < M) && (i >= 0)); i--) { for(j = 6; ((j < N) && (j >= 0)); j--) { fprintf(fout, "%d ", img[i][j]); } } /* for(i = 0; i < M; i++) { for(j = 0; j < N; j++) { fprintf(fout, "%d ", img[i][j]); } } */ close(fout); } int main(int argc, char* argv[]) { int i, j; int M, N; /* M is the height, N is the width */ int ** img = 0; FILE *file; file = NULL; /* initialization of pointer */ char c; /* first character of the header */ int v, max; /* v holds the version */ char infile[25]; char outfile[25]; printf("\nInput File Name(25 characters MAX with .pgm extension) :: "); gets(infile); printf("\nOutput File Name(25 characters MAX with .pgm extension) :: "); gets(outfile); file = fopen(infile, "r"); if(file == NULL) { printf("File not opened"); //getch(); exit(1); } fscanf(file, "%c%d\n%d %d\n%d", &c, &v, &N, &M, &max); fclose(file); img = memAlloc(M, N); inFile(infile, img, M ,N); outFile(outfile, c, v, N, M, max, img); system("PAUSE"); return 0; }
- 04-11-2011 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
should beCode:pointer = (int **) calloc(M, sizeof(* int));
Code:pointer = (int **) calloc(M, sizeof(int*));
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-12-2011 #9Just Joined!
- Join Date
- Apr 2011
- Posts
- 8
That was still giving me segmentation fault, unfortunately. So I decided to rewrite it will malloc instead of calloc. But now, memory is not being allocated properly in my for loop. It goes all the way till the part where I check if its NULL, and it prints out the error message.
Modified code below:
Code:#include <stdio.h> #include <stdlib.h> int ** memAlloc(int M, int N) { int i; int ** pointer; pointer = NULL; pointer = malloc(M * sizeof(int **)); if (pointer == NULL) { printf("Memory Allocation unsuccessful"); exit(1); } for(i = 0; i < N; i++) { pointer[i] = malloc(N * sizeof(int)); if(pointer[i] == NULL) { printf("Memory Allocation Unsuccessful"); exit(1); } } return pointer; } void inFile(char fn[], int **temp, int M, int N) { int i, j, k; char ch[4]; /* header information */ unsigned int value; FILE * fp; fp = NULL; /* initialization of pointer */ fp = fopen(fn, "r"); if(fp == NULL) { printf("File not opened"); exit(1); } fscanf(fp, "%c%c\n%d %d\n%d", &ch[0], &ch[1], &i, &j, &k); for (i = 0; i < M; i++) { for(j = 0; j < N; j++) { fscanf(fp, "%d", &value); temp[i][j] = value; } } fclose(fp); } void outFile(char outfile[], char c, int v, int N, int M, int max, int ** img) { int i, j; FILE * fout; fout = NULL; fout = fopen(outfile, "w"); fprintf(fout, "%c%d\n", c, v); fprintf(fout, "%d %d\n", N, M); fprintf(fout, "%d\n", max); if(fout == NULL) { printf("\n Error Opening Output File"); exit(1); } for(i = 23; ((i < M) && (i >= 0)); i--) { for(j = 6; ((j < N) && (j >= 0)); j--) { fprintf(fout, "%d ", img[i][j]); } } /* for(i = 0; i < M; i++) { for(j = 0; j < N; j++) { fprintf(fout, "%d ", img[i][j]); } } */ close(fout); } int main(int argc, char* argv[]) { int i, j; int M, N; int ** img = 0; FILE *file; file = NULL; char c; int v, max; char infile[25]; char outfile[25]; printf("\nInput File Name(25 characters MAX with .pgm extension) :: "); gets(infile); printf("\nOutput File Name(25 characters MAX with .pgm extension) :: "); gets(outfile); file = fopen(infile, "r"); if(file == NULL) { printf("File not opened"); exit(1); } fscanf(file, "%c%d\n%d %d\n%d", &c, &v, &N, &M, &max); fclose(file); img = memAlloc(M, N); inFile(infile, img, M ,N); outFile(outfile, c, v, N, M, max, img); system("PAUSE"); return 0; }
- 04-12-2011 #10Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I suppose at this point, we have to ask, what are the values you use for M and N?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
