J
Jeff Rodriguez
Main just loops over this while it's not null. The segfault occurs at
this line:
*line[ iteration ] = (char)ch;
Also, please don't just fix the code. I would like to know why exactly
this isn't working so I can avoid problems with it in the future.
If there are any references I should check out let me know.
Full highlighted code at:
http://gurugeek.com/jeff/programming/fgetline/fgetline.c.html
Line 62 is where segfault occurs.
This only happens if I use 3 characters. Using 4+ characters results in
a bus error.
GDB Output:
(gdb) run
Starting program: /usr/home/jeff/public_html/programming/fgetline/a.out
a
as
asd
(no debugging symbols found)...(no debugging symbols found)...
Program received signal SIGSEGV, Segmentation fault.
0x804860f in fgetline ()
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) run
Starting program: /usr/home/jeff/public_html/programming/fgetline/a.out
asdf
(no debugging symbols found)...(no debugging symbols found)...
Program received signal SIGBUS, Bus error.
0x804860f in fgetline ()
(gdb)
-- SNIP --
/* {{{ Code Fold: fgetline()
*/
unsigned int fgetline( FILE *fp, char **line )
{
const unsigned int BUFFER_SIZE = 1024; /* Base buffer size
*/
unsigned int iteration = 0; /* Number of characters
read */
char *tmp = NULL; /* Temp ptr to catch
realloc() */
int ch; /* Currently read character
*/
/* {{{ Code Fold: Loop until we reach EOF or a newline
*/
while ( (ch = fgetc(fp)) != '\n' && !feof(fp))
{
/* If there is no remainder for iteration / BUFFER_SIZE we've
reached */
/* a multiple of BUFFER_SIZE and it's time to reallocate the
buffer */
if ( !(iteration % BUFFER_SIZE) )
{
/* {{{ Code Fold: (Re)allocte memory
*/
tmp = realloc(
(*line),
(iteration / BUFFER_SIZE + 1)
* BUFFER_SIZE
* sizeof(char)
);
if ( tmp == NULL )
{
puts("Memory (re)allocation failed\n");
exit(EXIT_FAILURE);
}
/* If the address of our string changed, give *line that
address */
*line = tmp;
/* }}} Code Fold: (Re)allocte memory
*/
}
*line[ iteration ] = (char)ch;
iteration++;
}
/* }}} Code Fold: Loop until we reach EOF or a newline
*/
// Null terminate the array to make it a C string
line[ iteration + 1 ] = '\0';
if ( iteration == 0 )
{
return NULL;
}
else
{
return iteration;
}
}
/* }}} Code Fold: fgetline()
*/
-- SNIP --
this line:
*line[ iteration ] = (char)ch;
Also, please don't just fix the code. I would like to know why exactly
this isn't working so I can avoid problems with it in the future.
If there are any references I should check out let me know.
Full highlighted code at:
http://gurugeek.com/jeff/programming/fgetline/fgetline.c.html
Line 62 is where segfault occurs.
This only happens if I use 3 characters. Using 4+ characters results in
a bus error.
GDB Output:
(gdb) run
Starting program: /usr/home/jeff/public_html/programming/fgetline/a.out
a
as
asd
(no debugging symbols found)...(no debugging symbols found)...
Program received signal SIGSEGV, Segmentation fault.
0x804860f in fgetline ()
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) run
Starting program: /usr/home/jeff/public_html/programming/fgetline/a.out
asdf
(no debugging symbols found)...(no debugging symbols found)...
Program received signal SIGBUS, Bus error.
0x804860f in fgetline ()
(gdb)
-- SNIP --
/* {{{ Code Fold: fgetline()
*/
unsigned int fgetline( FILE *fp, char **line )
{
const unsigned int BUFFER_SIZE = 1024; /* Base buffer size
*/
unsigned int iteration = 0; /* Number of characters
read */
char *tmp = NULL; /* Temp ptr to catch
realloc() */
int ch; /* Currently read character
*/
/* {{{ Code Fold: Loop until we reach EOF or a newline
*/
while ( (ch = fgetc(fp)) != '\n' && !feof(fp))
{
/* If there is no remainder for iteration / BUFFER_SIZE we've
reached */
/* a multiple of BUFFER_SIZE and it's time to reallocate the
buffer */
if ( !(iteration % BUFFER_SIZE) )
{
/* {{{ Code Fold: (Re)allocte memory
*/
tmp = realloc(
(*line),
(iteration / BUFFER_SIZE + 1)
* BUFFER_SIZE
* sizeof(char)
);
if ( tmp == NULL )
{
puts("Memory (re)allocation failed\n");
exit(EXIT_FAILURE);
}
/* If the address of our string changed, give *line that
address */
*line = tmp;
/* }}} Code Fold: (Re)allocte memory
*/
}
*line[ iteration ] = (char)ch;
iteration++;
}
/* }}} Code Fold: Loop until we reach EOF or a newline
*/
// Null terminate the array to make it a C string
line[ iteration + 1 ] = '\0';
if ( iteration == 0 )
{
return NULL;
}
else
{
return iteration;
}
}
/* }}} Code Fold: fgetline()
*/
-- SNIP --