Jump to content

  • Log In with Google      Sign In   
  • Create Account


Noob question.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 CAREFiSH   Members   -  Reputation: 103

Like
0Likes
Like

Posted 30 November 2012 - 06:00 PM

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, 30 November 2012 - 06:02 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9337

Like
0Likes
Like

Posted 30 November 2012 - 06:22 PM

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


#3 CAREFiSH   Members   -  Reputation: 103

Like
0Likes
Like

Posted 30 November 2012 - 06:48 PM

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!

#4 kuramayoko10   Members   -  Reputation: 386

Like
0Likes
Like

Posted 30 November 2012 - 07:47 PM

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 Posted Image

Edited by kuramayoko10, 30 November 2012 - 07:52 PM.

Programming is an art. Game programming is a masterpiece!

#5 SiCrane   Moderators   -  Reputation: 9337

Like
0Likes
Like

Posted 30 November 2012 - 07:55 PM

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

#6 kuramayoko10   Members   -  Reputation: 386

Like
1Likes
Like

Posted 30 November 2012 - 08:08 PM

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!

#7 SiCrane   Moderators   -  Reputation: 9337

Like
2Likes
Like

Posted 30 November 2012 - 08:24 PM

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.

#8 kuramayoko10   Members   -  Reputation: 386

Like
0Likes
Like

Posted 30 November 2012 - 08:29 PM

Ah ok.
Thanks for clarifying that :)
Programming is an art. Game programming is a masterpiece!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS