LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SDL Video Modes to string array (https://www.linuxquestions.org/questions/programming-9/sdl-video-modes-to-string-array-21591/)

verigoth 05-22-2002 08:24 PM

SDL Video Modes to string array
 
this is probably not the right place to post this, but i really need help. my problem is this: SDL has a function to return available video modes, but it does so using it's own array. i need to convert this information into an array of strings (char **) to add it to a GList (GTK). the following should work, but doesn't. any other ideas?

SDL_Rect **modes;
char **temp;

for(int i=0; modes[i]; i++)
{
sprintf(temp[i], "%dx%d", modes[i]->w, modes[i]->h);
}

i get a seg-fault. no compilation errors or warnings...

thanks,
verigoth

Tinkster 05-22-2002 10:38 PM

G'day verigoth!

Not knowing the SDL I'm just citing the gcc-hotwo :)
because string and seg-fault match ;)
_________________________________________________________________

Writable strings (program seg faults randomly)

GCC has an optimistic view of its users, believing that they intend
string constants to be exactly that --- constant. Thus, it stores them
in the text (code) area of the program, where they can be paged in and
out from the program's disk image (instead of taking up swapspace),
and any attempt to rewrite them will cause a segmentation fault. This
is a feature!

It may cause a problem for old programs that, for example, call
mktemp() with a string constant as argument. mktemp() attempts to
rewrite its argument in place.

To fix, either (a) compile with -fwritable-strings, to get gcc to put
constants in data space, or (b) rewrite the offending parts to
allocate a non-constant string and strcpy the data into it before
calling.

_________________________________________________________________

Cheers,
Tink (hope it helps)

verigoth 05-23-2002 05:00 PM

I just tried that, Tink, and still got a seg-fault. I changed the code to this:

SDL_Rect **modes;
char **temp;
char *t;

for(int i=0; modes[i]; i++)
{
printf("One\n");
sprintf(t, "%dx%d", modes[i]->w, modes[i]->h);
printf("Two\n");
strcpy(temp[i], t);
printf("Three\n");
}

the output is:
One
Two
Segmentation Fault: SDL Parachute Deployed

I also tried
temp[i] = strcpy(temp[i], t);
and got the same thing

thanks again,
verigoth
}

gui10 05-23-2002 09:24 PM

SDL_Rect is a structure that goes like this:
typedef struct{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;

why do you need this:
SDL_Rect ** modes;
when you can do:
SDL_Rect modes[someConstant];
or SDL_Rect * modes;

or unless you really have a 2-d array for modes, you have to access x and y members of modes by:
modes[i][k]->x
and
modes[i][k]->y
since you have a SDL_Rect ** modes

btw, did you assign modes some values?

Mik 05-24-2002 03:21 AM

You can't just create pointers and then copy stuff to them without allocating memory.

It should have crashed on the first sprintf statement. But probably because t was still pointing to some other valid memory it didn't crash yet but you just wrote over some of the memory in your program.
If you want to copy a string then do something like this:

char *t = new char[10];
sprintf(t, "%dx%d", modes[i]->w, modes[i]->h);

That will allocate 10 bytes of memory to write your string in. Ofcourse you should always make sure that this is enough to fit your string in. This would be 9 bytes for the string and 1 byte for the null character.

For the double array you will have to allocate memory for the array and for each string in the array.

char **temp;
temp = new char*[10];

That will allow you to fit 10 strings in the array. And then before you copy a new string you will have to allocate memory again like you did before for the t variable.

temp[0] = new char[10];
sprintf(temp[0], "string");

Make sure you call delete for every time you called the new when you don't need the memory anymore. If you don't then you will create memory leaks in your program.

Hope that makes it clear and hopefully I didn't make a bunch of typos.


All times are GMT -5. The time now is 06:03 AM.