lundi 2 mars 2015

Segmentation Fault:11 Error messages in C Terminal



While compiling in the terminal I keep getting the error Segmentation Fault: 11. The goal is to make dark circles on a picture of a boarder, ect with command.


Command used:


$ ./a.out -c 470 355 100 < balloons.ascii.pgm > TestImages/balloons_c100_4.pgm


It uses main.c program that relates to pgmUtility.c


This is Main.c



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "pgmUtility.h"

#define ROWS 4
#define COLS 100

void usage(void)
{
printf("Usage\n");
printf(" -h Help Dialog\n");
printf(" -e edgeWidth < OldImageFile > NewImageFile\n");
printf(" -c centerRow centerCol radius < OldImageFile > NewImageFile\n");
printf(" -e edgeWidth -c radius centerRow centerCol < OldImageFile > NewImageFile\n");
exit (8);

}


int main(int argc, char * argv[]) {

FILE *fp;
FILE *out;
int i, j;
int flag1 = 0; //-e switch (edge draw)
int flag2 = 0; //-c switch (circle draw)
int numRows, numCols, centerRow, centerCol, radius, edgeWidth;
char originalImage[100], newImageFile[100];


char **header = (char**) malloc (sizeof(char*)*4);
int **pixels;


//command line argument parsing
//turn flag switches on or off

if(argc < 3)
usage();
if(argc > 7)
usage();

for(i = 1; i < argc; i++) {

if(strncmp(argv[i], "-e", 2) == 0) {
//set flag on
//get edge with values)
if(atoi(argv[i+1]) == 0) {
usage();
}
edgeWidth = atoi(argv[i+1]);
if(argv[i+2] != NULL) {
if(atoi(argv[i+2]) != 0) {
usage();
}
}
flag1 = 1;
}
if(strncmp(argv[i], "-c", 2) == 0) {
//set flag on
//get radius and center values
if(atoi(argv[i+1]) == 0) {
usage();
}
centerRow = atoi(argv[i+1]);
centerCol = atoi(argv[i+2]);
radius = atoi(argv[i+3]);
flag2 = 1;
strcpy(originalImage, argv[5]);
strcpy(newImageFile, argv[6]);
fp = fopen(originalImage, "r");
out = fopen(newImageFile, "w");

}
if(strncmp(argv[i], "-h", 2) == 0) {
usage();
}

}

//allocate memory for header array
header = (char **)malloc(ROWS * sizeof(char));
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
header[i] = (char *)malloc(COLS * sizeof(char *));
}
}

//read pgm file
pixels = pgmRead(header, &numRows, &numCols, fp);
if(pixels == NULL)
usage();

switch(flag1) {
case 1 :
if(flag2 == 1) {
//execute circle draw and edge draw
pgmDrawCircle(pixels, numRows, numCols, centerRow, centerCol, radius, header);
pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
}
else {
//execute only edge draw only
pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
}
break;
case 0 :
if(flag2 == 1) {
//execute circle draw
pgmDrawCircle(pixels, numRows, numCols, centerRow, centerCol, radius, header);

}
break;
default :
usage();
break;
}


//write new pgm file
pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out);

//Garbage Collection
//Fix this
//free(pixels);
//free(header);

for(i = 0; i < numRows; i++) {
int *current= pixels[i];
free(current);
}
for(i = 0; i < ROWS; i++) {
char *current = header[i];
free(current);
}
return 0;

}


This is pgmUtility.c



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

#include "pgmUtility.h"

#define ROWS 4
#define COLS 100


// Implement or define each function prototypes listed in pgmUtility.h file.
// NOTE: You can NOT change the input, output, and argument type of the functions in pgmUtility.h
// NOTE: You can NOT change the prototype (signature) of any functions listed in pgmUtility.h

int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in ){
int r, c;
int **array;

for(r = 0; r < ROWS; r++) {
fgets(header[r], COLS, stdin);
if(header == NULL)
return NULL;
}

//sscanf parses the numRows and numCols
sscanf(header[ROWS - 2], "%d %d", numCols, numRows);

//read in pixel map
array = (int **)malloc(*numRows * sizeof(int *));
for(r = 0; r < *numRows; r++) {
array[r] = (int *)malloc(*numCols * sizeof(int));
}

for(r = 0; r < *numRows; r++) {
for(c = 0; c < *numCols; c++) {
fscanf(in, "%d", *(array + r) + c );
}
}
fclose(in);
return array;

}
int pgmDrawCircle(int **pixels, int numRows, int numCols,
int centerRow, int centerCol, int radius, char **header)
{

int r, c;
int p1[2];
p1[0] = centerRow;
p1[1] = centerCol;
int p2[2];

for(r = 0; r < numRows; r++) {
for(c = 0; c < numCols; c++) {
p2[0] = r;
p2[1] = c;
if( distance(p1, p2) <= radius ) {
pixels[r][c] = 0;
}
}
}

return 0;

}

int pgmDrawEdge(int **pixels, int numRows, int numCols, int edgeWidth, char **header)
{
int r, c;

for(r = 0; r < numRows; r++) {
for(c = 0; c < numCols; c++) {
if(r <= edgeWidth || c <= edgeWidth) {
pixels[r][c] = 0;
}
if(r >= (numRows-edgeWidth) || c >= (numCols-edgeWidth)) {
pixels[r][c] = 0;
}
}
}

return 0;

}

int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){


//iterate straight through pixels
//setup with a loop to insert a new line every "numCols" and keep printing until "numRows + 1" is reached (as soon as numRows + 1 break loop)
int i, j;
for(i = 0; i < 4; i++){
//printf("%s", *header[i]);
fprintf(out, "%c", *header[i]);

}
//for(i = 0; i < 4; i++)
//fprintf(out, "*I=%d**%s**", i, header[i]);


for(j = 0; j < numRows; j++){
for(i = 0; i < numCols; i++)
fprintf(out, "%d ", pixels[i][j]);
fprintf(out, "\n");

}
fclose(out);
return 0;



}

double distance(int p1[ ], int p2[ ])
{

return sqrt( pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2) );

}



Aucun commentaire:

Enregistrer un commentaire