gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > libfat & C++ I/O

#119112 - nio101 - Mon Feb 19, 2007 8:59 pm

Hello friends,

I've tried to use C++ I/O functions to read a text file on my EZF4, and I get some random errors, sometimes 1 character coming from out of nowhere, sometimes some characters are lost...

I use the latests R20/libnds/libfat...

I do it that way :

Code:
#include <string>                  
#include <fstream>

ifstream inputstream("test.sgf");
   
string buffer;
   
while (!inputstream.eof())
{
   getline(inputstream, buffer,';');
   iprintf("buff=.%s.",buffer.c_str());
}


And "buffer" doesn't always contain what it should !?!... If somebody can help...

BTW, I've tried the classic C-stdio way (fopen/getc/fclose), and it works well...

Thanks.

#119153 - OOPMan - Tue Feb 20, 2007 8:41 am

I don't know whether things have changed at all recently, but I recall hearing that various elements of the stdc++ are not really supported by devkitPro. Things like iostreams and the like...

Anyone care to verify this?
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#119165 - wintermute - Tue Feb 20, 2007 12:07 pm

OOPMan wrote:
I don't know whether things have changed at all recently, but I recall hearing that various elements of the stdc++ are not really supported by devkitPro. Things like iostreams and the like...


devkitPro not being a toolchain wouldn't know anything about C++ at all :P

devkitARM on the other hand, being the ARM toolchain supplied by the devkitPro project might.

This code should work fine I'm not sure why it doesn't atm. The iostream code eventually ends up at the same place as C stdio.

One thing I will say is that this

Code:

#include <stdio.h>
int main() {
  consoleDemoInit(); // Dov's lazy console init, handy for quick tests
  printf("Hello World");
   while(1) {
      swiWaitForVBlank();
   }
}


generates a 69k nds file ( using iprintf makes it 52k)
while

Code:

#include <nds.h>
#include <iostream>


int main() {
   consoleDemoInit();
 
   std::cout << "Hello World!";
   while(1) {
      swiWaitForVBlank();
   }
}


generates a 270k nds file.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog


Last edited by wintermute on Wed Feb 21, 2007 1:05 am; edited 1 time in total

#119176 - OOPMan - Tue Feb 20, 2007 1:53 pm

My bad wintermute :-)

Useful to know that C++ does actually work, although that size hit is pretty nasty...

Still, it does mean I can, theoretically, use tinyxml :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#119209 - tepples - Tue Feb 20, 2007 8:39 pm

It's not necessarily the C++ language itself that generates a hit but some of its features:
  • Exception handling will bring in a lot of the C and C++ standard libraries that your program might not otherwise use on order to properly abort the program and toss an error message on the screens if main() doesn't catch everything. Based on my tests while developing AXE, compiling and linking without -fno-exceptions might add 50 KiB to the ARM7 or ARM9 executable.
  • Some people have claimed that use of RTTI (required for dynamic_cast) causes a size hit. Disable this at compile and link time with -fno-rtti.
  • But the biggest pig by far is <iostream>, as wintermute points out. On Windows (MinGW), hello_world_cstdio.cpp makes a 5 KiB executable, while hello_world_iostream.cpp makes a roughly 250 KiB executable. Tests on the Game Boy Advance reveal a similar magnitude of difference, so this is likely to be the case for any platform that links libstdc++ statically. Examining the result of 'nm' run on the iostream version shows that it's bringing in formatters for types that my program doesn't use, including the entire date formatter.

It's possible to use C++ classes, inheritance, polymorphism, templates, etc. while using C-style exceptions (that is, result codes) and C-style I/O (that is, <cstdio>). But to be fair, even using C (or those parts of in C++ that are also parts of C) varies in space efficiency depending on how much abstraction you want to include:
  • As wintermute points out, floating-point arithmetic takes space. If you use printf() instead of iprintf(), you link in a formatter with support for %e, %f, and %g formats, which links in the floating-point arithmetic library.
  • The formatter takes space even without support for floating-point arithmetic. If you use iprintf() instead of puts(), you link in the formatter.
  • The console takes space. I made a stripped-down console library called AGBTTY, and hello_world_agbtty.gba was significantly smaller than hello_world_libgba_console.gba.

FAQ time?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.


Last edited by tepples on Thu Aug 02, 2007 12:53 pm; edited 1 time in total

#119290 - Payk - Wed Feb 21, 2007 4:15 pm

I am using tinyxml :D works great
there is just one thing that has to be commented out. If you need to know which part i mean:contact me.

#119309 - OOPMan - Wed Feb 21, 2007 7:52 pm

Hmmm, I found expat, which is entirely C, so I may end up using that instead :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...