Sign in to follow this  
flashingchicken

Weird Bug

Recommended Posts

I am making a space launching game, and am using SDL, but i have come across a weird bug that i cant resolve. When i run the debugger, it says that player->thrust_percent=50, which is correct, but, the result of str is "h=0.00, thrust=0.000000". I am wondering if i am not referencing the class correctly. Thanks, John <code>void Draw_Scene(SDL_Surface* screen, Spaceship *player) {char * str; str=new char[1023]; sprintf(str, "h=%4.2f, thrust=%f",player->height-RADIUSEARTH, player->thrust_percent ); SDL_FillRect(screen, NULL, 0x000000); Draw_Text(screen, str, screen->w/2, screen->h/2, 255,255,255); SDL_Flip(screen); free(str);} </code> <code> class Spaceship{ //theta = angle from verticle to axis of ship //phi = angle frme verticle to angle of V //gamma = angle from top of world to space ship public: double theta, phi, gamma; long double height; double mass, drag,velocity; float thrust_percent; Spaceship() { theta=0; phi=0; gamma=0; height=0; mass=0; drag=0;velocity=0; thrust_percent=0; } </code>

Share this post


Link to post
Share on other sites
How do you call Draw_Scene? Are you sure %f works with double and long doubles?

There's a slightly easier way to handle string output than sprintf. Well, first of all, don't use sprintf, use snprintf. Second, don't allocate the character array with new, that will only tax the malloc code further, just allocate it on the stack.

If you use an iostream (more specifically, a stringstream), you can avoid snprintf. snprintf is OK, if you pass the parameters 100% correctly. %f expects a float, what happens if you pass a double or long double? Using an iostream, you don't lose type information, and everything tends to work itself out without the need for error-prone format strings. I think this might be your problem.

Share this post


Link to post
Share on other sites
Changed Draw_Scene to this, works great. Thanks. Now, if i can just keep from deleting my work, i should be set!

std::stringstream stream1;
stream1<<"h="<<player->height-RADIUSEARTH<<" thrust="<<player->thrust_percent;
SDL_FillRect(screen, NULL, 0x000000);
Draw_Text(screen, stream1.str().c_str(), screen->w/2, screen->h/2, 255,255,255);
SDL_Flip(screen);

Share this post


Link to post
Share on other sites
I know that you fixed it, but this:
str=new char[1023];
free(str);}

was probably your problem.

If you use new[], you need to use delete [].
free goes with malloc(), not new/new[]

Share this post


Link to post
Share on other sites
No, that wasn't the problem. free() might also successfully deallocate a new[], but certainly won't call destructors. A memory leak maybe, but it wasn't the problem he was asking about.

Share this post


Link to post
Share on other sites
I think my primary problem was the format flags. The free vs delete is just bad practice on my part.
But, now that i can see everything, i can see that my physics is messed up, wanna help with my math?.... heheh

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this