[HELP] SDL Segmentation

Hi,
i’ve some problems with this code:
when i launch it goes in Segmentation …

where i was wrong ???

PS : for the obj file take whatever you like :slight_smile:

— filename: test.c —
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

typedef struct {

float x,y,z;

}VTX;

typedef struct {

int vtx_num;
VTX *vertex;

}MODEL;

MODEL *read(char *name){

MODEL *md;

md=malloc(sizeof(MODEL));
FILE *fin;
char string[255];
int cnt=0;
fin=fopen(name,"rt");

while(fgets(string,255,fin)!=NULL){

	if((string[0]=='v')&&(string[1]==' '))
		cnt++;	
}
md->vtx_num=cnt;


return md;

}

int main (void){

SDL_Surface *screen;
MODEL *mdl;
mdl=malloc(sizeof(MODEL));
mdl=read("prova.obj");

SDL_Init(SDL_INIT_VIDEO);	

}

thanks in advance,

Ivan

fin=fopen(name,“rt”);

while(fgets(string,255,fin)!=NULL){

Perhaps make sure (fin != NULL) after the fopen() call?

This doesn’t look like an SDL bug.

–ryan.

(This is not SDL related, but anyway…)

[…]

char string[255];
[…]
while(fgets(string,255,fin)!=NULL){

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 --'On Sunday 08 January 2006 20:07, Ivan wrote:

David Olofson <david olofson.net> writes:

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

Perhaps make sure (fin != NULL) after the fopen() call?

This doesn’t look like an SDL bug.

–ryan.

Thanks David (and Ryan ^_^) for your help
(i konw that i’should use a dynamic assignment) but …

if i comment (or delete) the SDL initialization,
my code don’t goes in Segmentation;
if you try (even adding a printf("%d\n",mdl->vtx_num)
to see if “all” indeed works) it works … :stuck_out_tongue:

thanks in advance,

(i’m really really sorry for my english …
i hope that you understand what i’m trying to say)

Ivan

Hi Ivan,

You’re overriding the C-Library function read() with an incompatible one
of your own!

Try changing it to read_model() or something,

Good Luck!
John Popplewell.On Sun, Jan 08, 2006 at 10:24:27PM +0000, Ivan wrote:

David Olofson <david olofson.net> writes:

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

Perhaps make sure (fin != NULL) after the fopen() call?

This doesn’t look like an SDL bug.

–ryan.

Thanks David (and Ryan ^_^) for your help
(i konw that i’should use a dynamic assignment) but …

if i comment (or delete) the SDL initialization,
my code don’t goes in Segmentation;
if you try (even adding a printf("%d\n",mdl->vtx_num)
to see if “all” indeed works) it works … :stuck_out_tongue:

thanks in advance,

(i’m really really sorry for my english …
i hope that you understand what i’m trying to say)

Ivan

David Olofson <david olofson.net> writes:

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

Perhaps make sure (fin != NULL) after the fopen() call?

This doesn’t look like an SDL bug.

–ryan.

Thanks David (and Ryan ^_^) for your help
(i konw that i’should use a dynamic assignment) but …

if i comment (or delete) the SDL initialization,
my code don’t goes in Segmentation;
if you try (even adding a printf("%d\n",mdl->vtx_num)
to see if “all” indeed works) it works … :stuck_out_tongue:

Coincidence. Two things happen when you init SDL:
1) Code changes and moves around, so things that slips
through otherwise doesn’t, and vice versa.
2) SDL catches the segmentation fault, so it has
a chance of closing the display and stuff.
(Certain platforms will sometimes kill the
display, or worse, if an application dies
without closing it’s fullscreen display…)

That is, when not initializing SDL, your code somehow manages to get
away with illegal memory accesses (ie they hit some unused memory
area, as opposed to hitting unmapped addresses) - or it would have
crashed anyway; it would just have been handled by the operating
system instead of SDL.

Try adding SDL_INIT_NOPARACHUTE to SDL_Init() to have a normal crasch
instead, or more interestingly, allow a debugger to trap the
segfault.

//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 --'On Sunday 08 January 2006 23:24, Ivan wrote:

Thanks to all of you ^_^,
Ryan,John,David.

I’m sorry for the stupid questions that i’ve asked ^_^,

I’m only a little n00b… hghghghgh,

anyway now i know where i was wrong, really really thanks :slight_smile:

(i’m really really sorry for my english … again)

Bye,

Ivan

Sorry to bring this back up, but I want to clarify this fgets bit…On 1/8/06, David Olofson wrote:

(This is not SDL related, but anyway…)

On Sunday 08 January 2006 20:07, Ivan wrote:
[…]

  char string[255];

[…]

  while(fgets(string,255,fin)!=NULL){

fgets() writes a NUL byte after the last byte read, so this will
segfault if the file is longer than 254 bytes.

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

Justin Coleman <jmcoleman gmail.com> writes:

Sorry to bring this back up, but I want to clarify this fgets bit…

(This is not SDL related, but anyway…)

[…]

  char string[255];

[…]

  while(fgets(string,255,fin)!=NULL){

fgets() writes a NUL byte after the last byte read, so this will
segfault if the file is longer than 254 bytes.

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

anyway,
i was looking around for tutorial to read obj
files and i’ve seen that *, NeHe in a tutorial
use something like my function, maybe (as someone has advised)
the problem is the overriding of the function read().

*The link:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=25

i’ll try and i’ll let you know

thanks again,

Ivan> On 1/8/06, David Olofson <david olofson.net> wrote:

On Sunday 08 January 2006 20:07, Ivan wrote:

Sorry to bring this back up, but I want to clarify this fgets bit…

(This is not SDL related, but anyway…)

[…]

  char string[255];

[…]

  while(fgets(string,255,fin)!=NULL){

fgets() writes a NUL byte after the last byte read, so this will
segfault if the file is longer than 254 bytes.

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.

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: :smiley:

"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 --'On Monday 09 January 2006 14:27, Justin Coleman wrote:

On 1/8/06, David Olofson <@David_Olofson> wrote:

On Sunday 08 January 2006 20:07, Ivan wrote: