S
SP
The following code compiles with no errors, but fails with
"Segmentation Fault" when I run it .
I tracked the problem down to the code which populates the
image.pixel_p array and I assume that the real problem is with the
memory allocation.
The values for the rows and columns are correct and I print them to
make sure.
I added a print statement to the code that is crashing and it gets
about 90% through the array before it fails.
Thanks in advance for your help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fgetdata.h"
int main( int argc, char *argv[])
{
struct png_file{
char *name_p; /* pointer to name of image file */
char *type_p; /* image type identifier */
int max_row; /* total rows of image */
int max_col; /* total columns of image */
int max_val; /* max pixel value of image */
int **pixel_p; /* pointer to 2D array space to store image */
};
struct png_file image;
FILE *image_fp;
int i, args, success;
int row, col;
char *delimiters =" \t\r\n\f\v\a\b\\?\\\'\"!%^&*()=+/<>,.|[]{}#~";
char *line = NULL;
size_t size = 0;
image_fp = fopen("chess.pgm", "r");
if(!image_fp)
{
fprintf(stderr, "unable to open file\n");
return 1;
}
/* get header info, skip comment lines */
i = 0;
success = 0;
while(i<3)
{
fgetline(&line, &size, (size_t)-1, image_fp, 0);
if(line[0] != '#')
{
if(i == 0)
{
image.type_p = malloc(strlen(line)+1 *
sizeof(image.type_p));
if(image.type_p)
{
strcpy(image.type_p, line);
}
else
{
fprintf(stderr, "Mem allocation for 'magic_str_p'
failed\n");
return 1;
}
success = 1;
}
if(i == 1)
{
args = sscanf(line ,"%d %d", &image.max_row,
&image.max_col);
if ( args == 2) success = 1;
}
if(i == 2)
{
args = sscanf(line ,"%d", &image.max_val );
if ( args == 1) success = 1;
}
if(success) i++;
success = 0;
}
}
fprintf(stderr, "Image Type: %s\n", image.type_p);
fprintf(stderr, "Image Rows x Columns: %i x %i\n", image.max_row,
image.max_col);
fprintf(stderr, "Pixel Max Value: %i\n", image.max_val);
image.pixel_p = malloc(sizeof(image.pixel_p) * image.max_row) ;
if( image.pixel_p == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
}
for( row = 0; row <= image.max_col; row++)
{
image.pixel_p[row] = malloc(sizeof(image.pixel_p) *
image.max_col);
if( image.pixel_p == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
}
}
if(image.pixel_p)
{
for( row=0; row < image.max_row; row++)
{
for( col=0; col<image.max_col; col++)
{
fgetword(&line, &size, delimiters, (size_t)-1,
image_fp, 0);
image.pixel_p[row][col] = atoi(line);
fprintf(stderr, "pixel[%i][%i] word:%s
Val:%i\n", row, col, line, image.pixel_p[row][col]);
}
}
}
fclose(image_fp);
free(image.type_p);
for( row = 0; row < image.max_col; row++)
{
free(image.pixel_p[row]);
}
free(image.pixel_p);
return EXIT_SUCCESS;
}
"Segmentation Fault" when I run it .
I tracked the problem down to the code which populates the
image.pixel_p array and I assume that the real problem is with the
memory allocation.
The values for the rows and columns are correct and I print them to
make sure.
I added a print statement to the code that is crashing and it gets
about 90% through the array before it fails.
Thanks in advance for your help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fgetdata.h"
int main( int argc, char *argv[])
{
struct png_file{
char *name_p; /* pointer to name of image file */
char *type_p; /* image type identifier */
int max_row; /* total rows of image */
int max_col; /* total columns of image */
int max_val; /* max pixel value of image */
int **pixel_p; /* pointer to 2D array space to store image */
};
struct png_file image;
FILE *image_fp;
int i, args, success;
int row, col;
char *delimiters =" \t\r\n\f\v\a\b\\?\\\'\"!%^&*()=+/<>,.|[]{}#~";
char *line = NULL;
size_t size = 0;
image_fp = fopen("chess.pgm", "r");
if(!image_fp)
{
fprintf(stderr, "unable to open file\n");
return 1;
}
/* get header info, skip comment lines */
i = 0;
success = 0;
while(i<3)
{
fgetline(&line, &size, (size_t)-1, image_fp, 0);
if(line[0] != '#')
{
if(i == 0)
{
image.type_p = malloc(strlen(line)+1 *
sizeof(image.type_p));
if(image.type_p)
{
strcpy(image.type_p, line);
}
else
{
fprintf(stderr, "Mem allocation for 'magic_str_p'
failed\n");
return 1;
}
success = 1;
}
if(i == 1)
{
args = sscanf(line ,"%d %d", &image.max_row,
&image.max_col);
if ( args == 2) success = 1;
}
if(i == 2)
{
args = sscanf(line ,"%d", &image.max_val );
if ( args == 1) success = 1;
}
if(success) i++;
success = 0;
}
}
fprintf(stderr, "Image Type: %s\n", image.type_p);
fprintf(stderr, "Image Rows x Columns: %i x %i\n", image.max_row,
image.max_col);
fprintf(stderr, "Pixel Max Value: %i\n", image.max_val);
image.pixel_p = malloc(sizeof(image.pixel_p) * image.max_row) ;
if( image.pixel_p == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
}
for( row = 0; row <= image.max_col; row++)
{
image.pixel_p[row] = malloc(sizeof(image.pixel_p) *
image.max_col);
if( image.pixel_p == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
}
}
if(image.pixel_p)
{
for( row=0; row < image.max_row; row++)
{
for( col=0; col<image.max_col; col++)
{
fgetword(&line, &size, delimiters, (size_t)-1,
image_fp, 0);
image.pixel_p[row][col] = atoi(line);
fprintf(stderr, "pixel[%i][%i] word:%s
Val:%i\n", row, col, line, image.pixel_p[row][col]);
}
}
}
fclose(image_fp);
free(image.type_p);
for( row = 0; row < image.max_col; row++)
{
free(image.pixel_p[row]);
}
free(image.pixel_p);
return EXIT_SUCCESS;
}