I'm writing a script that needs to read every line in a very large file (110,000L) into a dynamic array (and then it does something that is irrelevant to my question). Here's the relevant code:
FILE *orig;
FILE *dest;
orig = fopen("../../dicts/mpron/pronDict.txt","r+");
dest = fopen("../../dicts/editedPronDict.txt","w");
char **arr = malloc(sizeof(char *)*128);
int size = 0;
int capacity = 128;
char *inputLine = malloc(sizeof(char)*128);
fgets(inputLine,128,orig);
while(!feof(orig)){
arr[size] = malloc(sizeof(char)*128);
strcpy(arr[size],inputLine);
size++;
if(size == capacity){
realloc(arr,sizeof(char *)*(capacity*2));
capacity*=2;
fprintf(stderr,"New size: %i\n",capacity);
}
}
It's pretty basic stuff, and I thought it would run perfectly. However, I get this output:
./organizeMpron
New size: 256
New size: 512
organizeMpron(2088,0x7fff7c53e300) malloc: *** error for object
0x7fc2aa001200: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
So it only doubles in capacity twice before crashing. I tried changing the initial capacity of the array to 64, and strangely enough I get a completely different output:
./organizeMpron
New size: 128
New size: 256
New size: 512
New size: 1024
New size: 2048
New size: 4096
New size: 8192
New size: 16384
New size: 32768
New size: 65536
New size: 131072
Segmentation fault: 11
Does anyone have any clue what might be going on here? I've written dynamic collections in this fashion several times and I have no idea where this code is breaking. Thank you in advance.