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 / int to char* for dbText()

Author
Message
Lucky777
14
Years of Service
User Offline
Joined: 28th Nov 2009
Location:
Posted: 30th Jun 2010 08:06
Hey there forum!

I'm trying to make a game of Pong. My score is an integer value (kinda obvious!), but I want to use dbText() to display it on the screen, but the problem is that dbText() uses a variable with the type char*, and you can't convert it by only "typecasting" it.

So my question is, How can I convert an int to a char*

Thx people for your help.... I learned C++ like a year ago for a school project and I didn't practice, so this question might look simple to you, but nt to me
Red Eye
16
Years of Service
User Offline
Joined: 15th Oct 2008
Location:
Posted: 30th Jun 2010 13:23 Edited at: 30th Jun 2010 13:25
Declaration:
int myvar = 100;

Converting:
char* myvartochar = dbStr(myvar);

Forgot to say u can also use atoi, or itoa.

Greets,

Red Eye

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 30th Jun 2010 14:05
Be sure to call 'delete' for everytime you call dbStr() or you will have a memory leak.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 30th Jun 2010 14:38
itoa is great, but if you want to print multiple variables at one call, use spritf (or sprintf_s would be even better ), a google search will be enough to find out what you need about it

Lucky777
14
Years of Service
User Offline
Joined: 28th Nov 2009
Location:
Posted: 30th Jun 2010 19:28
ok thx!

but I have 1 more questions for you people XD, do I have to delete something each time I use itoa, or will it overwrite the buffer?
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 30th Jun 2010 20:12
You could also do this:



Just put the integer that you want into the dbStr() function. It's what I'm doing for my project.

@ Matty,

Had no idea that you had to call delete when you call dbStr(), thanks.
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 30th Jun 2010 20:14 Edited at: 30th Jun 2010 20:17
Quote: "Just put the integer that you want into the dbStr() function"


If you do that, then you can't delete the newly-allocated memory.

char* myvartochar = dbStr(myvar);
dbText(x, y, myvartochar);
delete[] myvartochar;

Quote: "but I have 1 more questions for you people XD, do I have to delete something each time I use itoa, or will it overwrite the buffer?"


No, DarkGDK (always) allocates a new string while itoa and sprintf edit the one you provide.

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
pirogoth
16
Years of Service
User Offline
Joined: 6th Apr 2008
Location: Good Old California
Posted: 30th Jun 2010 20:43
Since we're using C++ and not C, lets do it the C++ way, hrm? =P

String Streams

Try this:


This completely removes the need for any manual memory management. It's also quite fast. It also completely removes potential missuses of sprintf and it's ilk.

If you MUST use the printf family of functions, please use the safe versions. Example: printf_s, sprintf_s, etc.

-Piro
Lucky777
14
Years of Service
User Offline
Joined: 28th Nov 2009
Location:
Posted: 30th Jun 2010 22:02
Well thx again! XD

I'm able to display my score (YEY!), but I wanted to make function that would return the string....but I can't do it.... a tried returning a std::string, but you can't convert const char to char*. I also tried returning a char*....which didn't work either...

So if you don't mind, I'm asking for your help one last time!

thx in advance!
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 30th Jun 2010 22:09
These threads come up from time to time, I still dont know which method is the best. I currently use sprintf_s, is stringstream faster?

pirogoth
16
Years of Service
User Offline
Joined: 6th Apr 2008
Location: Good Old California
Posted: 30th Jun 2010 22:36
@matty halewood
Honestly, stringstream is the slower of the two options. However, if your string operations are a bottleneck in your application then one of two things is going on:

1) You're clearly doing something wrong (and probably using character arrays when unnecessary).
2) You have the most optimized application in history. Pat yourself on the back. =P

It really comes down to this. Using character arrays in C++ is largely unnecessary and only gives more opportunities to accidentally (and most often, unknowingly) abuse them leading to extremely hard to track bugs later on in development.

One of the more common issues is a buffer overun. Have you ever had a bug and during a debug session found that memory is being corrupted? You then sit there for hours trying to track down why? Usually do to walking off the end of an array, and most often a character array.

There's nothing wrong with pointers or arrays. They just shouldn't be used in situations where a list or a vector would be most suited (which is most of the time).

C++ strings and other such containers and data types should be used in place of manual work when possible. They are fairly optimized and most importantly, they reduce the amount of work you need to do. Giving you the opportunity to spend that time on more important things such as writing your application instead of writing support libraries and routines.
pirogoth
16
Years of Service
User Offline
Joined: 6th Apr 2008
Location: Good Old California
Posted: 1st Jul 2010 08:26
@Lucky777
Mind showing us a little code snippet, showing what you're trying to do?

Using my earlier example...


It sounds like you're trying to do this:



That'd be the wrong way to go about it. Try this:


Or just return myStream.str() from your function call.
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 1st Jul 2010 16:58
Quote: "but you can't convert const char to char*"


If you're talking about displaying the string, you have to explicitly cast the string to char*, since dbText takes char*, when it has no reason to.

dbText(x, y, (char*)myStream.str().c_str());
or
dbText(x, y, (char*)myString.c_str());

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
Serial Velocity
16
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 3rd Jul 2010 20:16 Edited at: 3rd Jul 2010 20:22
It might be easier to create and use macros like this:



or even wrap it into a set of functions:



GDKUtilities, a utility library for DarkGDK, avaliable here
helpfull programmer
15
Years of Service
User Offline
Joined: 30th Jul 2009
Location:
Posted: 10th Jul 2010 00:48
I am probably putting my foot in this, but couldn't you just do:

int foo = 1;
dbText(x_cord,y_cord,dbStr(foo));

and be done with it??? instead of confufiling around with all these strings? I might be wrong, if so please tell me... but this seems so much more simpler.

Happy-programming
from helpfullprogrammer.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 10th Jul 2010 01:01
@helpfull: Yes you could do that, but in the way that you suggest, it will cause memory leaks and eventually crash. Using your approach, a better way would be:



The commands that return strings (char*) results allocate that memory using the c++ new command (or maybe malloc); in either case, if you don't release it yourself, all of those little allocations will add up and eat away at your memory...

JTK
helpfull programmer
15
Years of Service
User Offline
Joined: 30th Jul 2009
Location:
Posted: 10th Jul 2010 01:06
See that's where my understanding is lacking... all this deleting char's but not int's it just doesn't make sense, surly if c++ handles int's in a certain way it should handle chars? and shouldn't DarkGDK free up the memory if it is their function?

Thanks for the quick reply
from helpfullprogrammer.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 10th Jul 2010 03:01
I understand what you are saying, GDK returns int values for many of their functions - you don't need to worry about those. However, when returning a pointer; that's the problem.

Internally (inside the compiler), when returning an int, the result is pushed into a CPU register (typically EAX [or derivative depending on what processor I suppose]). When the function returns, the calling function pop's that value from the CPU register. Therefore, the calling function is responsible for allocation/deallocation of the memory (the compiler handles this for you).

However, when returning a pointer - as in char*, the memory must be allocated on the heap (physical memory somewhere) and the address of that location is returned via the EAX register. Since that chunk of memory is flagged as in use by the OS, no future allocations can touch it until it is un-flagged. And since the GDK doesn't know what you will be doing with that memory location, it can't just simply delete it - you have to be the one to tell the OS that you're done with it.

Now, it may appear to you that this is only a problem when GDK returns a char* but in fact, it would be a problem if the GDK returned an int*, float* or whatever other pointer type you could think of.

Q: What about returning a reference&? Isn't that the same thing?
A: Well, yes and no. Yes, a reference is effectively an address to a spot in memory. But, when used in conjunction with a variable declared internally to a function, it will (via details handled by the compiler) be invalid since the compiler will allocate and release that memory from the OS without you - the programmer - having to worry about those details.

Therefore...



In the above code snippet, the compiler is performing the memory allocation for you; it requests 20-bytes of memory from the OS, which the OS flags as being used. Once you execute the return statement, code injected by the compiler effectively informs the OS that you are done using it...

To get around this, the GDK will specifically inform the OS itself that the memory is needed:



Whenever you call the operator new(), you must remember to call the operator delete() in order to free up the memory. The compiler didn't call the operator new() in this case, you the programmer did. Therefore, you (the programmer) are responsible for calling delete().

Of course, TGC is very aware of these differences in return types and for whatever reason, they have designed their GDK in such a way as to effectively say: if you ask for it, *you* own it! I can't say for sure, but I would guess that the reason for this decision is in support of the DarkBasic product (from which GDK originated)...

I hope this helps explain things a little better for you,

JTK
pirogoth
16
Years of Service
User Offline
Joined: 6th Apr 2008
Location: Good Old California
Posted: 10th Jul 2010 04:06 Edited at: 10th Jul 2010 04:07
@Serial Velocity
Macros are an excellent tool, but one that should be used sparingly. When you run into a bug introduced by a written macro, it makes debugging extremely difficult due to the way they work.

When you use macros, the pre-processor expands them (think search and replace) in your code. The compiler is run on this, rather than your actual code files. When the compiler hits a bug in one of your macros, or one involving one, the debugger won't be able to point to that line in the code as it exists only too the pre-processor and compiler. In some cases it even points to the wrong line of code.

I'm not saying you shouldn't use macros, but it's best to use macros when you want to expand the syntax of the language, not when you want to add functions.

As for creating helper functions, thats a good plan.

@JTK
That is one of the best explanations of the problems with memory management I've read. That is precisely why I suggest dealing with memory management only when necessary. Much lower chance of introducing bugs in your code (not counting logic errors of course).

My philosophy as a coder: Write your code without worry of optimizations or bottlenecks. Use the default library when at all possible. Constants are your friend. Avoid pointers when possible in favor of references (preferably constant references!). And only then, if you have determined that your code is not fast enough, profile your code and optimize as appropriate. You'll drastically cut down on your debugging time and you'll get far more work done in less time.

-Piro
helpfull programmer
15
Years of Service
User Offline
Joined: 30th Jul 2009
Location:
Posted: 10th Jul 2010 13:25
@JTK thank you very much

so instead of:


what should you do? I just don't see why you have to delete anything if you are using it within dbText and you arn't "storing" it anywere for further use.

Thank you very much for your understanding
from helpfullprogrammer.

P.S I have kinda stolen this thread, shall I create a new one?
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 10th Jul 2010 13:34
use it in dbText or not, you allocated memory, and you need to free it

helpfull programmer
15
Years of Service
User Offline
Joined: 30th Jul 2009
Location:
Posted: 10th Jul 2010 13:48
Ahhhh, sorry I skipped on of JDK's earlier posts.

Thank you everyone!
from helpfullprogrammer.

Login to post a reply

Server time is: 2024-11-19 20:18:19
Your offset time is: 2024-11-19 20:18:19