Hey, I have here a piece of code that works in parts but does not work together. If I test all three parts at once, I get a segmentation fault immediately before the program is supposed to test for a (null) value in the string array. However, if I comment out the first part of the program, testing the first set of functions, the other two test fine. The same applies to commenting out the tests of the second set of functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ht, wd; /* height and width for array in part 1 */
/*functions for part 1*/
int * create2DArray(int height, int width)
{
int * array = malloc(height * width * sizeof(int));
return array;
}
void set2DElement(int * array, int loc_x, int loc_y, int value)
{
array[loc_x + wd*loc_y] = value;
}
int get2DElement(int * array, int loc_x, int loc_y)
{
return array[loc_x + wd*loc_y];
}
void free2DArray(int * array)
{
free(array);
}
/* functions for part 2 */
char ** createStringArray(int num)
{
char **stringPtr = malloc(num * sizeof(char *));
return stringPtr;
}
void setStringArray(char ** stringPtr, int position, char * setstring)
{
stringPtr[position] = malloc(strlen(setstring) * sizeof(char));
strcpy (stringPtr[position], setstring);
}
char * getStringArray(char ** stringPtr, int position)
{
if(stringPtr[position] == NULL){
return " ";
}
return stringPtr[position];
}
void freeStringArray(char ** stringPtr, int num)
{
int x = 0;
for (x = 0; x < num; x++)
{
free(stringPtr[x]);
}
free(stringPtr);
}
/* functions for part 3 */
int ** createArray(int width, int height)
{
int x = 0;
int ** matrix = malloc(height * sizeof(int *));
for (x = 0; x < height; x++)
{
matrix[x] = malloc (width * sizeof(int));
}
return matrix;
}
void freeArray(int ** matrix, int height)
{
int x = 0;
for (x = 0; x < height; x++)
{
free(matrix[x]);
}
free(matrix);
}
int main(int argc, char *argv[]) {
int *array;
int width, height;
int value;
char **stringPtr;
int **matrix;
printf("Testing Part 1\n");
width = 5;
height = 6;
array = create2DArray(height, width);
printf("Store value 7 at [3,4].\n");
set2DElement(array, 3, 4, 7);
value = get2DElement(array, 3, 4);
printf("Retrieve value %d from [3,4]\n\n", value);
printf("Store value 200 at [0,4].\n");
set2DElement(array, 0, 4, 200);
value = get2DElement(array, 0, 4);
printf("Retrieve value %d from [0,4]\n\n", value);
printf("Store value 3 at [2,2].\n");
set2DElement(array, 2, 2, 3);
value = get2DElement(array, 2, 2);
printf("Retrieve value %d from [2,2]\n\n", value);
free2DArray(array);
printf("Testing Part 2\n");
stringPtr = createStringArray(100);
printf("Store string - fred\n");
setStringArray(stringPtr, 44, "fred");
printf("Store string - barney\n");
setStringArray(stringPtr, 80, "barney");
printf("Get string - %s\n", getStringArray(stringPtr, 44));
printf("Get string - %s\n", getStringArray(stringPtr, 80));
printf("Get string - %s\n\n", getStringArray(stringPtr, 3));
freeStringArray(stringPtr, 100);
printf("Testing Part 3\n");
matrix = createArray(100, 100);
printf("Store 33 44 55\n");
matrix[22][76] = 33;
matrix[83][29] = 44;
matrix[99][65] = 55;
printf("Retrieve %d %d %d\n", matrix[22][76], matrix[83][29],
matrix[99][65]);
freeArray(matrix, 100);
return(1);
}
Am I using malloc() or free() in some way that precludes the program working? Can anybody compile this code and get it to work all the way through?