LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2002, 08:24 PM   #1
verigoth
Member
 
Registered: May 2002
Posts: 179

Rep: Reputation: Disabled
Post 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
 
Old 05-22-2002, 10:38 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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)
 
Old 05-23-2002, 05:00 PM   #3
verigoth
Member
 
Registered: May 2002
Posts: 179

Original Poster
Rep: Reputation: Disabled
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
}
 
Old 05-23-2002, 09:24 PM   #4
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Rep: Reputation: 30
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?

Last edited by gui10; 05-23-2002 at 09:37 PM.
 
Old 05-24-2002, 03:21 AM   #5
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
String Array definiation problem nelnel Programming 1 09-16-2005 04:56 AM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 02:50 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration