Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Confused with strings and pointers...

Author
Message
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 16th Jul 2009 19:24 Edited at: 26th Nov 2009 02:43
I learned C++ last week, so now I'm working on a simple game with DarkGDK (Pacman).

I think I have a grasp of pointers and how they work, but I can't figure out how to use dbStr():



This code gives an unhandled exception when the program is closed, and the sprites don't show up.

What am I doing wrong?

Serial Velocity
16
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 16th Jul 2009 19:33 Edited at: 16th Jul 2009 19:35
Strings are hard to grasp, try using:

char temp[255];

instead of

char * temp;

and to add strings together, thats really difficult to do but luckily there is a command to do this for you, called sprintf_s. To use:

char str[255];
sprintf_s( str, 255, "Hello %d", "World" );


This snippet creates "Hello World" in the string called str. The %d tells sprintf_s to use the next parameter for this part of the string (which can be numbers too, no need for dbStr).

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 16th Jul 2009 19:40 Edited at: 16th Jul 2009 19:41
A few problems,

1 - Don't use dbStr or any of the GDK string functions unless you really have to as they are poo.

Instead, use itoa or sprintf(more flexible) to construct strings from integers, both require you to allocate buffers beforehand making them more flexible too.

So using the following would fix, you get this:



2 - Skipping ahead a bit, this is related to 1 but you deallocate 'temp' using 'delete' where you should be using 'delete []' because dbStr() returns a pointer to an array of characters allocated with 'new char[]'.

3 - "*temp + ".png"" you cannot add strings like this, or in this case you're trying to add a char to a pointer which is doomed to fail(well, just not do what you expect).

Use the aforementioned sprintf to do this for you, provided you have a buffer that can store the final string in.

/me throws poo at Serial Velocity for posting faster

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 16th Jul 2009 19:48
@Serial Velocity

I think you meant %s for a string because %d is for a decimal or integer.

http://cplusplus.com/reference/clibrary/cstdio/sprintf/

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 16th Jul 2009 22:06
Many thanks, it works now

Quote: "Don't use dbStr or any of the GDK string functions unless you really have to as they are poo."

Thanks for the tip



Diggsey: I have a spine and memory, but one memorable guy says he hates me. What am I?
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 17th Jul 2009 06:30
This was something I wanted to ask about with GDK, when we send one of their commands that returns a character array pointer(char*) such as char* dbStr() to another one of the commands directly.. such as :



is doing that creating an internal memory leak somewhere ? Or does GDK handles itself internally properly ?
Im just curious becuase I use that method a fair bit, and while if it is creating a memory leak it would be small, its still bad.

Also if anyone knows if sending commands to commands with GDK in this way is a bad practice or not, i'd like to know that too lol

Cheers

If it ain't broke.... DONT FIX IT !!!
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Jul 2009 08:37
Quote: "is doing that creating an internal memory leak somewhere ?"


Yes.

Quote: "Or does GDK handles itself internally properly ?"


No, and how would it?

Quote: "and while if it is creating a memory leak it would be small"


It adds up every time you do such a call.

BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 17th Jul 2009 16:35
It does empty all those undeleted memory blocks when you close it though, right?



Diggsey: I have a spine and memory, but one memorable guy says he hates me. What am I?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Jul 2009 18:20
Quote: "It does empty all those undeleted memory blocks when you close it though, right?"


Yes. But it's of course good practice to do this yourself.

Login to post a reply

Server time is: 2024-10-01 08:30:04
Your offset time is: 2024-10-01 08:30:04