Jump to content
  • Advertisement
Sign in to follow this  

Noob question.

This topic is 1998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 <cstdlib>
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. Edited by CAREFiSH

Share this post


Link to post
Share on other sites
Advertisement
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().

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 Edited by kuramayoko10

Share this post


Link to post
Share on other sites

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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!