My goal is to load a char** from a file of words so that there is no wasted space (ie custom amount of cols for the exact amount of letters in each word). I am getting "bus error" when trying to run the program, gdb says:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
0x00001ea5 in loadArray (inFileName=0xbffffa0f "10-words.txt", array=0xbffff8dc, count=0xbffff8d8, capacity=0xbffff8d4) at SL2.c:68
68 *array[*count] = resizedtmp;
I do not know what this means, but I am assuming it is a bad use of malloc/pointers? I tried referring to array without the *, and this removed bus error and replaced it with a segmentation fault and a warning during compilation. I do not undersand why this error is occurring...I am new to C and have been working on this program all day, something that should have taken an hour or two tops. Thank you very much for your much needed help.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int loadArray(char *, char ***, int *, int *);
int printArray(char**, int);
int main(int argc, char* argv[]) {
char** A;
int size, capacity;
loadArray(argv[1], &A, &size, &capacity);
printArray(A, size);
return EXIT_SUCCESS;
}
int loadArray(char *inFileName, char ***array, int *count, int *capacity) {
/* complete this function. The function takes a file name (string)
the address of a char** (array of char*), return the size of the
file through count (how many words), and also the capacity (space allocated). Allocate 10 spaces
for the array of char* first and then double the size of the
array as needed. You dont need to sort the names. You need sort
in lab2 though.
Return 0 if the function successfully completes the allocation.
Else return -1 to indicate an error.
*/
*capacity = 10; /* current capacity */
*count = 0; /* current count */
int i = 0;
int length = 32;
char tmp[length];
FILE* fp;
*array = (char**)malloc(*capacity*sizeof(char*));
if (*array == NULL) { /* malloc failed */
printf("Malloc failed; exiting program.\n\n");
return -1;
}
fp = fopen(inFileName, "r");
if (fp == NULL) { /* File does not exist */
printf("File does not exist; exiting program.\n\n");
return -1;
}
while (fscanf(fp, "%s", &tmp) > 0) { /* add words to array */
if (*count >= *capacity) { /* array needs to be resized */
if (realloc(*array, 2*(*capacity)) != NULL) {
realloc(*array, 2*(*capacity));
*capacity *= 2;
}
else { /* realloc failed */
printf("Realloc failed; exiting program.\n\n");
return -1;
}
}
char* resizedtmp;
resizedtmp = (char *)malloc((strlen(tmp)+1)*sizeof(char)); /* +1 for \0 null character */
if (resizedtmp == NULL) {
printf("Malloc failed; exiting program.\n\n");
return -1;
}
strcpy(resizedtmp, tmp);
*array[*count] = resizedtmp;
(*count)++;
}
printf("The count is %d words\nThe capacity is %d bytes\n", *count, *capacity);
return 0;
}
int printArray(char** array, int size) {
/* prints the file, each word separated by a space. */
int j = 0;
for (j; j<=size; j++) {
printf("%s ", array[j]);
}
return 0;
}