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 ---
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!
--- 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 ---
line[0] = '0'01234567890123456789
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!