looking for some advice on a problem I've been trying to solve for hours. The program reads from a text file and does some formatting based on commands given within the file. It seems to work for every file I've tried except 2, which are both fairly large. Here's the offending code:
/* initalize memory for output */
output.data = (char**)calloc(1,sizeof(char*));
/* initialize size of output */
output.size = 0;
/* iterate through the input, line by line */
int i;
for (i = 0; i < num_lines; i++)
{
/* if it is not a newline and if formatting is on */
if (fmt)
{
/* allocate memory for a buffer to hold the line to be formatted */
char *line_buffer = (char*)calloc(strlen(lines[i]) + 1, sizeof(char));
if (line_buffer == NULL)
{
fprintf(stderr, "ERROR: Memory Allocation Failed\n");
exit(1);
}
/* copy the unformatted line into the buffer and tokenize by whitespace */
strcpy(line_buffer, lines[i]);
char* word = strtok(line_buffer, " \n");
/* while there is a word */
while (word)
{
/* if the next word will go over allocated width */
if (current_pos + strlen(word) + 1 > width)
{
/* make ze newline, increase output size */
strcat(output.data[output.size], "\n");
output.size++;
------->>>>> output.data = (char**)realloc(output.data, sizeof(char*) * (output.size + 1));
Using gdb I've figured out the error is on the line with the arrow pointing to it, only thing is I can't figure out why it occurs. It only happens when the text file that is being formatted is large (716 lines), and it seems to happen on the final iteration (num_lines = 716). Any thoughts would be hugely appreciated. Thanks!
EDIT: Sorry folks, should have mentioned that I'm pretty new to this! Fixed some of the errors.