Skip to main content
GameDev.net gamedev.net
🔒 Locked

Noob question.

Started by CAREFiSH Dec 1, 2012 at 12:00 AM 6 replies 1.3k views
Original Post
CAREFiSH
CAREFiSH
Hey there people from GameDev!

I'm a relatively new game developer, started learning C++ about 6 months ago. And I decided to do a simple string class exercise.
Now I'm running into a problem, I can't seem to get it to work without it crashing.
I'll post the pastebins for the code.
.h: http://pastebin.com/dbBWGDRV
.cpp: http://pastebin.com/tGSFz6K7
And finally, the main:
[source lang="cpp"]#include "CFString.h"
#include
int main(int argc, char* args[])
{
CFString bla = "hoi";
printf(bla);
return 0;
}[/source]
Now, it's crashing and I don't really understand why. Any pointers/hints?

EDIT:: I've tried to add a '0' after the strncpy, I've tried setting strdata to NULL.
SiCrane
SiCrane
The problem is probably in one of your strlen() calls. strlen() uses the null terminator to determine string length. However, you call strlen() on strings you haven't added the null terminator to in order to figure out where to put the null terminator. You'd probably be better off calling strlen() once in your constructors and store that length in a variable rather the repeatedly calling strlen().
CAREFiSH
CAREFiSH
Hmm, I will do that! And it's fixed! In the CFString::CFString(const CFString& str) function there's a strlen() on strdata, that should've been str.strdata Thanks anyway!
kuramayoko10
kuramayoko10
I have a question.
How does that printf(bla) in your main works?

I tested doing that here, but couldn't compile like that, since printf needs a format string.
printf's prototype is: int printf(const char *, ...)

Is that a typo? Or did you implement your own print function that receives a CFString object?
Just out of curiosity smile.png
Programming is an art. Game programming is a masterpiece!
SiCrane
SiCrane
His string class has a operator const char *() function which allows an implicit cast to const char *.
kuramayoko10
kuramayoko10

His string class has a operator const char *() function which allows an implicit cast to const char *.

But for the printf to work doesn't it need the "format string" and then the stuff you want to print?

Is this a safe thing to do?
g++ gave me some warnings at least..
Programming is an art. Game programming is a masterpiece!
SiCrane
SiCrane
It's legal code to have a format string come from a variable; however, it can be a security risk. The data in his string was a valid format string for being called with no other arguments because it didn't contain any %'s inside. However, if it had contained any %'s inside it, then it would have started reading information from the stack, which would be bad. Since the number of arguments printf() is called with is determined at compile time, and the format string could be changed at runtime, gcc will warn you about it.
kuramayoko10
kuramayoko10
Ah ok.
Thanks for clarifying that
Programming is an art. Game programming is a masterpiece!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.