stupid c++ noob question

stupid c++ noob question

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
greenail
Spring Developer
Posts: 80
Joined: 13 Dec 2005, 20:16

stupid c++ noob question

Post by greenail »

i'm fucking with M$ VS 2005 express an i got most everything to build but I noticed that it complains about snprintf, and M$ wants you to use sprint_s as a replacement in groupai.cpp What are the toughs about this? it also complains about strcpy.... any ideas? I'm a total c++ noob and i'm amazed i even got anything to build. i grabbed it from the svn repos. I can get the groupAI's to build if i change snprintf.



P.S.

I also get a ton of boost thread (recursive_mutex etc though it appeared boost built correctly) include errors, and ft2build.h errors when try ing to build rts, not sure why.
User avatar
Pxtl
Posts: 6112
Joined: 23 Oct 2004, 01:43

Post by Pxtl »

*panic* there are people using the old C libraries in C++? Particularly the string libraries?

*hyperventilates* *fans self*.

There are two ways to think about C++. First, is that it's a new version of C with new features - a superset of C. Second, is that it is a totally new language that happens to be backwards compatible with C.

Both of these statements are technically true, but they produce different ways of thinking. The first way opens you up to buffer overruns, roaming pointers, and memory hell. The latter will keep you safe.

use the string class and the stringstream. char* is left for the anachronisms and for optimization purposes. Arrays are for optimization _only_ - all new code should be done with vectors, preferably using at() instead of [] for access until you're sure there's no fencepost errors.
User avatar
Veylon
AI Developer
Posts: 174
Joined: 21 Sep 2005, 19:45

Post by Veylon »

Would it make you feel better if I told you that I originally had to hack the string class in order to make my AI work?
Or that I used to use FILE* for my I/O?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

hmm I should really switch from using FILE*.....
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

printf rules at formatting strings... you can very easily select lots of options for it, and I've got no idea how to do all those things with std::string. I don't think you can actually...

fopen rules too, because it's just really simple and doesn't require massive headers that drive up the compile time.
Any serious big project is going to use it's own filesystem anyway, so the majority of the code isn't even going to touch C-style file I/O...
greenail
Spring Developer
Posts: 80
Joined: 13 Dec 2005, 20:16

Post by greenail »

according to vc++ in vs2005 sprintf and snprintf are unsafe and generate compile errors.
User avatar
Pxtl
Posts: 6112
Joined: 23 Oct 2004, 01:43

Post by Pxtl »

Zaphod wrote:printf rules at formatting strings... you can very easily select lots of options for it, and I've got no idea how to do all those things with std::string. I don't think you can actually...

fopen rules too, because it's just really simple and doesn't require massive headers that drive up the compile time.
Any serious big project is going to use it's own filesystem anyway, so the majority of the code isn't even going to touch C-style file I/O...
Well, there are millions of safe std::string libraries available, but if you need to use the internal libraries, you wrap the string in a stringstream and use the string operations to format.

Code: Select all

mystringstream << "info:" << mymanipulators << mydouble;
and so on. The manipulators provide the formatting. For example, for 5 significant figures (rounded off) use "setprecision(5)", or

Code: Select all

mystringstream << "info:" << setprecision(5) << mydouble;
For good 3rd party libs, a good example is:
http://www.boost.org/libs/format/doc/format.html

Boost libs pwn.
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

IMO, The only good thing about std::string is that is can handle any string size you throw at it... but when formatting numbers you usually don't even need that. sprintf(txt, "%3.5f", val); is fast and simple, doesn't call the memory manager (Like the string constructor does)
Boost libs pwn.
*pukes*
Have you checked how insanely they increase code size, and compile time is huge even with precompiled headers. All this extra code seriously kills the instruction cache.
Templates are nice, but not combined with the C++ include model and when used so extensively like in boost ...
Module import instead of #include, like in java, C# or D sounds much better to me, but those are not really standard languages in terms of portability and people that know them, or is just slow like java.
Post Reply

Return to “Engine”