[HELP] SDL Segmentation |
[HELP] SDL Segmentation |
Ryan C. Gordon
Guest
|
Perhaps make sure (fin != NULL) after the fopen() call? This doesn't look like an SDL bug. --ryan. |
|||||||||||||
|
[HELP] SDL Segmentation |
David Olofson
Guest
|
(This is not SDL related, but anyway...)
On Sunday 08 January 2006 20:07, Ivan wrote: [...]
fgets() writes a NUL byte *after* the last byte read, so this will segfault if the file is longer than 254 bytes. As a general rule when dealing with the string functions of the C library: Be very careful with those NUL terminators and buffer size limits... //David Olofson - Programmer, Composer, Open Source Advocate .------- http://olofson.net - Games, SDL examples -------. | http://zeespace.net - 2.5D rendering engine | | http://audiality.org - Music/audio engine | | http://eel.olofson.net - Real time scripting | '-- http://www.reologica.se - Rheology instrumentation --' |
|||||||||||||||
|
[HELP] SDL Segmentation |
Justin Coleman
Guest
|
Sorry to bring this back up, but I want to clarify this fgets bit...
On 1/8/06, David Olofson <david at olofson.net> wrote:
According to Borland (C++ Builder 6) docs, fgets will stop after reading n-1 bytes, leaving room for its null terminator itself. It wouldn't segfault if the line was longer than 254 bytes, it would just stop reading at that point. Are there other compilers where this behavior is different? I'm using fgets for some of the data I load, and I can always just make a bigger buffer, but it'd be easier to do it right the first time. -Justin |
|||||||||||||||||
|
[HELP] SDL Segmentation |
David Olofson
Guest
|
On Monday 09 January 2006 14:27, Justin Coleman wrote:
I was wondering why that was not totally clear in the Linux documentation - but it turns out it is; I just didn't read it carefully enough: :-D "fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer." So, no, fgets() won't write more than 'size' bytes. I certainly hope this applies to all other relevant implementations as well... //David Olofson - Programmer, Composer, Open Source Advocate .------- http://olofson.net - Games, SDL examples -------. | http://zeespace.net - 2.5D rendering engine | | http://audiality.org - Music/audio engine | | http://eel.olofson.net - Real time scripting | '-- http://www.reologica.se - Rheology instrumentation --' |
|||||||||||||||||||
|