in realloc(): warning: chunk is already free

J

Jeff

I have the following code:

--- SNIP ---
// {{{ Code Fold: Includes and Defines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// }}} Code Fold: Includes and Defines
// {{{ Code Fold: Function Prototypes
void get_line( FILE *fp, char * line );
// }}} Code Fold: Function Prototypes
// {{{ Code Fold: main()
int main( int argc, char *argv[] )
{
unsigned int count = 0;
while ( !feof(stdin) )
{
char *line;
count++;
printf("> ");
get_line(stdin, line);
//printf("%s\n", *line);
}
printf("\nTotal lines: %u\n", count);
return 0;
}
// }}} Code Fold: main()
// {{{ Code Fold: get_line()
void get_line( FILE *fp, char * line )
{
char ch;
unsigned short iteration = 0;
while ( (ch = fgetc(fp)) != '\n' && !feof(fp))
{
// {{{ Code Fold: Allocate memory if this is the first
iteration
if ( iteration == 0 )
{
line = ( char * ) calloc( 2, sizeof(char));
if ( line == NULL )
{
puts("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
}
// }}} Code Fold: Allocate memory if this is the first
iteration
// {{{ Code Fold: Reallocate memory if this is the first
iteration
else
{
if ( realloc(line, (iteration + 2) * sizeof(char)) == NULL
)
{
puts("Memory reallocation failed\n");
exit(EXIT_FAILURE);
}
}
// }}} Code Fold: Reallocate memory if this is the first
iteration
line[ iteration ] = ch;
printf("line[%u] = '%c'\n", iteration, line[ iteration ] );
iteration++;
}
//printf("%s\n", line);
}
// }}} Code Fold: get_line()
--- SNIP ---

It compiles fine, no warnings of any kind, and starts fine. However,
any string longer than 15 characters breaks the program:

--- SNIP ---
01234567890123456789
line[0] = '0'
line[1] = '1'
line[2] = '2'
line[3] = '3'
line[4] = '4'
line[5] = '5'
line[6] = '6'
line[7] = '7'
line[8] = '8'
line[9] = '9'
line[10] = '0'
line[11] = '1'
line[12] = '2'
line[13] = '3'
line[14] = '4'
a.out in realloc(): warning: chunk is already free
Memory reallocation failed
--- SNIP ---


I'm extremely new to C in general, and searches on google only seem to
reveal people having this problem with existing programs. (i.e. ssh,
httpd, su, etc)

So any help is greatly appreciated!
 
R

Robert Stankowic

Jeff said:
I have the following code:

[....]

Just a few corrections - I also reformatted your code and removed the
comments, which just made the code more difficult to read (others will point
out more, I guess :))
BTW
Please use "C" - style comments when posting, i this case the code still
compiles even if the comment lines wrap, and it will compile not only with a
C99 compiler, but also with C89


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

void get_line( FILE *fp, char **line );
/*You should pass a _pointer_ to your char pointer in order to be able to
use the allocated memory in main()*/

int main( int argc, char *argv[] )
{
char *line = NULL;
unsigned int count = 0;

while ( !feof(stdin) )
{
count++;
printf("> ");
get_line(stdin, &line);
}
printf("\nTotal lines: %u\n", count);
/*Don't forget to:*/
free(line);
return EXIT_SUCCESS;
}


void get_line( FILE *fp, char **line )
{
int ch;
int iteration = 0;
char *tmp = NULL;

/*No need for an extra malloc(), realloc() with a NULL pointer as
argument 1 acts as a malloc()*/
while ( (ch = fgetc(fp)) != '\n' && !feof(fp))
{
/*Using a temporary pointer here makes sure, that the previously
allocated memory is still accessible if realloc fails*/
if ((tmp = realloc((*line), (iteration + 2) * sizeof(char))) == NULL)
{
/*if realloc() fails, (*line) still points to valid memory, so you
can take some sensible action here if you want*/
puts("Memory reallocation failed\n");
exit(EXIT_FAILURE);
}
else
{
*line = tmp;
}

(*line)[ iteration ] = (char)ch;
printf("line[%u] = '%c'\n", iteration, (*line)[ iteration ] );
iteration++;
}

}

HTH
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top