Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Guru Rubberman's Avatar
    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:
    Code:
        pointer = (int **) calloc(M, sizeof(int));
    It should be
    Code:
        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.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    8
    Quote Originally Posted by Rubberman View Post
    The problem is here:
    Code:
        pointer = (int **) calloc(M, sizeof(int));
    It should be
    Code:
        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.
    Thanks for the reply! unfortunately, I'm still getting a segmentation fault.

  4. #4
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by ostangel View Post
    Thanks for the reply! unfortunately, I'm still getting a segmentation fault.
    So, post your code changes.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Apr 2011
    Posts
    8
    Quote Originally Posted by Rubberman View Post
    So, post your code changes.
    I basically just implemented your previous suggestion I.e. Instead of int, I did int* but it is still somehow giving me a segmentation fault.

  6. #6
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by ostangel View Post
    I basically just implemented your previous suggestion I.e. Instead of int, I did int* but it is still somehow giving me a segmentation fault.
    I've heard that before, but as we say, the devil's in the details!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Apr 2011
    Posts
    8
    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;
    }
    [/QUOTE]

  8. #8
    Linux Guru Rubberman's Avatar
    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
    Code:
    pointer = (int **) calloc(M, sizeof(* int));
    should be
    Code:
    pointer = (int **) calloc(M, sizeof(int*));
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  9. #9
    Just Joined!
    Join Date
    Apr 2011
    Posts
    8
    Quote Originally Posted by Rubberman View Post
    Code:
    pointer = (int **) calloc(M, sizeof(* int));
    should be
    Code:
    pointer = (int **) calloc(M, sizeof(int*));
    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;
    }

  10. #10
    Linux Guru Rubberman's Avatar
    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!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...