Help - Search - Members - Calendar
Full Version: Question For X-port
Scenyx Entertainment Community > Xbox1 Forums > Software Forums > XPort Projects
madmab
I know this is probably considered a basic C/C++ coding question but I really do not know of any decent place to go to ask it. So I figured I would start here.

It's regarding character strings/sequences, whatever the heck they are called and how they use memory. In particular m_menuText..

In your code m_menuText is defined as

WCHAR *m_menuText[200] ;

Now is this telling the compiler that we want something that can store 200 characters worth of data? I'm just a little confused about how this is being interpreted. I'm presuming that the * turns m_menuText into a pointer to that allocated memory.

The reason I ask is because if you take, for example, a game select screen with 15 lines of data. You have to figure that it equals to possibly up to 630 characters worth of data (assuming that each file is 42 character long).

Seems to me that would exceed 200. So I'm just wondering if I'm misunderstanding what is happening here. Is the stack making up for this? It obviously does not lock up the xbox when in the game select screen.

I'm just asking because when working on the synopsis file I tried a similar approach. I was trying to create some type of variable to hold the first two lines of data stored in m_menuText but the xbox kept on erroring out on me. It was not really apparent what was going on based on the error I was getting.

Any help would be appreciated. I just wanna make sure I'm not misunderstanding the usage and writing some bad code that just happens to work. smile.gif
nes6502
It's an array that can hold 200 strings. Each string can be any length. For examle:

WCHAR *m_menuText[200] ;

swprintf( m_menuText[0], L"Set Memory Card Slots" );

will add the string "Set Memory Card Slots" to the first place.

swprintf( m_menuText[199], L"selected save state and start playing the recording." ) ;

will add the string "selected save state and start playing the recording." to the last place.




madmab
ok thanks nes6502. That was the original impression that I got so my next question would be if I do this..

char line[100] ;

Does that create an array that can hold 100 strings any size? Or a 100 character string?

If it creates a 100 character string, what do I do to create an array that can hold 100 strings?

The site I go to that explains all this stuff implies it is a 100 character string that can be divided into seperate elements. For example strings seperated by a null character.
XPort
QUOTE(madmab @ Dec 13 2008, 03:11 AM) *

ok thanks nes6502. That was the original impression that I got so my next question would be if I do this..

char line[100] ;

Does that create an array that can hold 100 strings any size? Or a 100 character string?

If it creates a 100 character string, what do I do to create an array that can hold 100 strings?

The site I go to that explains all this stuff implies it is a 100 character string that can be divided into seperate elements. For example strings seperated by a null character.


Sorry - missed the first one.

//array of 100 chars - each array item is a character
char line[100] ;

line[0] = line[1] = line[2] = 'a' ;
line[3] = 0 ;
printf( line ) ;

aaa

//array of 100 pointers to char - each array item is a (char*)
char* line[100]

line[0] = "blabla" ; //note that these are string constants - so memory is allocated for them already
line[1] = "foo" ;

line[2] = malloc( 400 ) ; // line[2] is now a (char*) that points to a block of allocated memory 400 bytes long
sprintf( line[2], "hello there" ) ;

You first have to allocate space to the pointers before you can write to them. Note that this is different than assigning the pointer to a value.

line[0] = "blabla" ; //this says that the char pointer value line[0] should be equal to the location in memory that stores the data "blabla"

sprintf( line[2], "hello there" ) ; //the value of line[2] is a pointer in memory - this says to go to that location in memory and write the data "hello there" to it.



madmab
ok thanks. That makes more sense now. I finally managed to find the maloc for m_menutext which for some reason I was overlooking.

Now I can correct a few potential "bugs". laugh.gif Pointers and character arrays/strings drive me crazy but one day I'llget used to them.

Just one last question. Swprintf is used to transfer data into a WCHAR string. How do I transfer an element of an array from one WCHAR string to another, or from a WCHAR string to just a char variable.

Thanks..
XPort
QUOTE(madmab @ Dec 13 2008, 08:54 PM) *

ok thanks. That makes more sense now. I finally managed to find the maloc for m_menutext which for some reason I was overlooking.

Now I can correct a few potential "bugs". laugh.gif Pointers and character arrays/strings drive me crazy but one day I'llget used to them.

Just one last question. Swprintf is used to transfer data into a WCHAR string. How do I transfer an element of an array from one WCHAR string to another, or from a WCHAR string to just a char variable.

Thanks..


//copy one wchar_t* to another - note that there is usually a "w" version of common cstr functions
//wstrlen, wstrcpy, wstrcmp, etc
wstrcpy( wcstr1, wcstr2) ;

//copy whcar_t* into char* - note the capital S
sprintf( cstr, "%S", wcstr);
madmab
ok thanks! That clarifies alot. Just glancing over some of my older code it looks like I handled things correctly. Forgot that I traced down how m_menutext is allocated when modifying the YesNo function. It's just then I didn't completely understand what C was doing.

Now it all kinda makes sense. laugh.gif So I should be able to finishing modifying the synopsis code to do something I was not having any luck with previously.
madmab
Another question. This one is in regards to "files" in the emu it is defined like this

CODE
typedef struct _filenamestruct {
        WCHAR name[200];
        unsigned char filename[200];
        char isDir;
    } FILENAME;


    FILENAME *files;


Memory is allocated like this..

CODE
if (files != NULL)
{
    delete [] files;
    files = NULL;
}
files = new FILENAME[4096];


Is it possible for me to just allocate this one entry at a time?

files[i] = malloc (sizeof(FILENAME)) ;

If that is the case will the above "delete [] files" work for deallocating? Or would I have to individually deallocate each array entry with a FOR loop?

Thanks!

XPort
You cannot allocate the way you described because files[i] represents a value and not a pointer. If you want to allocate them in a piecemeal fashion, you can do it like so:

int numentries = 10 ;

//allocate memory for 10 entries
files = (FILENAME*)malloc( sizeof(FILENAME) * numentries) ;

numentries = 5 ;

//reallocate memory for 5 entries
files = (FILENAME*)realloc( sizeof(FILENAME) * numentries) ;

madmab
ok thanks for the info.

I don't necessary get it just yet. But if it works, it works... laugh.gif
XTecuterX73
i also have a question for you madmab or xport, i have been messing around with nes sources just seeing if i can do some things. I tried to add a mapper into mednafenx-nes and keep getting this error:

ines.obj : error LNK2001: unresolved external symbol "int __cdecl Mapper212_Init(struct __CartInfo *)" (?Mapper212_Init@@YAHPAU__CartInfo@@@Z)

any idea on what the heck this means, lmao. laugh.gif I'm assuming im not linking something right.

XT-
madmab
Looks like you just need to add the source file for that mapper into your project using "add existing item" in the solution explorer.
XTecuterX73
QUOTE(madmab @ Mar 5 2009, 05:43 AM) *

Looks like you just need to add the source file for that mapper into your project using "add existing item" in the solution explorer.


the source file>>what i did exactly was get the mapper http://fceumm.svn.sourceforge.net/viewvc/fceumm/,
i went over the code to make sure it was structured correctly for mednafenx. I then added the info to the ines.cpp and ines.h files and i believe i made the approriate changes. So what exactly is the source file? LMAO< sorry for being such a noob here but i think i almost got this, at least i think.

the name of the file was 212.c, i renamed it to 212.cpp, i even made sure to add into the makefile as boards\212.cpp. I'm pretty sure i was supposed to do that as well.

XT-
madmab
It can't find the function Mapper212_Init() to link to. Which is most likely in the mapper212.c/.cpp file. If it is, that means that visual studio cannot find it in your current project. Hence why you have to add it in. (Right click on the project name over in the solution explorer).

I don't know much about makefiles. But I think visual studio ignores those once you "import" a project.
XTecuterX73
ok thanks madmab, biggrin.gif i'll give that a try and see what happens. I guess i should of thought of that to begin with. laugh.gif

XT-
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.