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 / print multiple strings using dbPrint on one line

Author
Message
EDGECOM
18
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 13th Jul 2009 04:46
I don't know if there is already a thread on this but I figured I would post what I have found. I searched MSDN for like 2hr reading about char strings in c++ because from sreaching the fourms here everyone is saying to make your own lib to do char conversions I got to thinking there has to be a better way and I found this link below.

http://msdn.microsoft.com/en-us/library/69ze775t(VS.80).aspx

here is my c++ code I came up with



Note: just use strcat to keep adding to the string also if you want to use your own vars string make the function look like this



from reading on MSDN about strings using this method you could have
40 lines with 50 characters per lines or one really really long string

I hope this is usefull
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 13th Jul 2009 05:04 Edited at: 13th Jul 2009 05:09
You could also use sprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/



PS: Why are you casting a char array as (char*)?
It already is one!

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
prasoc
15
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 13th Jul 2009 09:18
or:




Your signature has been erased by a mod
EDGECOM
18
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 13th Jul 2009 14:10
@Bran flakes91093 my understanding of (char*) from reading on MSDN , google etc.. the (char*) is a pointer to the string at least thats my under standing of it because I could also write it like this char("my strring") but if you use it in dbPrint it don't work it needs the *

@prasoc I know I could do that also but I don't want to include another header file in my exe

it works for what I need atm I am writting custom MS SQL functions

thank you for the feedback
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jul 2009 15:55
@EDGECOM

The name of a char array is already a pointer to the first character in the array. You're casting it unnecessarily. Ultimately it doesn't make any difference to how the code is generated. I think the question regarding this was just to get you thinking about how C++ actually works.

Quote: "strcat (str,(char*)myvar);"


Unless myvar is already a character string, this won't just take any random variable type and convert it to a string. You need to use either existing functions or create your own. If myvar was an integer you'd need to use the itoa function to produce a pointer to a string that has converted the integer into that string. That would probably require an extra header. The sprintf function is a good work around.

BTW, including headers generally will not cause your program to be any larger except for the portions where you make calls or references to what the header supports. Including headers may make the compiler take a little bit of extra time while it reads the file but that time is relatively insignificant. Headers are not included in the .exe unless the header has a bit of code itself, which is generally not the case. Headers are typically used to provide prototypes for functions that might be called in your code so the compiler knows its footprint. But nothing is added unless a function is linked due to it's being called in your code.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
EDGECOM
18
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 13th Jul 2009 18:44 Edited at: 13th Jul 2009 18:46
I know I have to conver int to char well it looks like I need to burn my C++ books then then lol cause from what you two are saying (char*)mystring = another string within a string array :/ cause in my books they say its just a pointer to mystring o well



I hope this code is propper now I did not mean to miss inform anyone I was just posting what I though woulld help others

PS thx for the info on headers
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 13th Jul 2009 18:58
Also remember that (I think)all GDK functions that return char* dynamically allocate the buffers so you'll have to handle their destruction yourself. Calling "char* intprint=dbStr (timer_total);
"
as seen in your code above will cause a memory leak if you don't manage it yourself.

EDGECOM
18
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 13th Jul 2009 20:26
like this ?



http://msdn.microsoft.com/en-us/library/h6227113(VS.80).aspx
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jul 2009 20:58
Only if myvarorstring is a pointer to a dynamically allocated memory space.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
EDGECOM
18
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 13th Jul 2009 21:42
char* = dynamically allocated memory space ?



also what is a good starter book on C++ programming cause the c++ for dummies is what I have
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jul 2009 22:24
Pointers have several uses. Their notable properties are that they hold addresses. They're distinguished by the type of data of which they hold the address of. They're variable as opposed to array names or references. When incremented they

A declared array represents the address of the first element of the array.

int myInts [10];

Using myInts in any expression returns the address of the [0] element.

int *intP; // pointer to an integer

intP = myInts; // assigns the address of myInts to intP

intP++; // increments the address stored in intP to point to the second ([1]) element

intP = new int [10];
// asks the system to allocate room for ten ints and return the address of the allocated
// space which is assigned to intP
// you can now reference intP as if it were an array of ints

int abc = intP[4];

delete [] intP; // tells the system to reclaim the previously allocated space
// the address stored in intP is no longer valid for use

When using a pointer to dynamically allocated space you should not change the value of the pointer, though you can change the value of the space pointed to by the pointer.

*intP = 436; // changes the integer pointed to by intP

*(intP + 3) = 634; // changes the fourth element of the dynamically assigned array space.

The same can be done for characters. Pointers are often used to traverse nul terminated strings because it's usually faster to increment the pointer than it is to index the array.


dbStr() internally gets dynamically allocated space and returns the address of that space. You're duty is to assign it to a char *.

char *p = dbStr(xxxx);

The thing you need to keep in mind, and which isn't very obvious, is that you're also responsible for telling the system it can return the space back to the heap memory.

delete [] p;

Each call to dbStr() can potentially give you a different address and certainly will if you don't give back the memory you received previously. Never change or re-use a pointer to dynamically allocated memory until you've de-allocated the memory. If you need to track more than one memory space use additional pointers.

It's also a good practice to initialized these pointers to NULL and to set them to NULL when you've de-allocated the memory they referenced. Then you can use this value to test if the pointer is available for use.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
EDGECOM
18
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 13th Jul 2009 23:24
so your saying if I don't delete the dbprint and myvar/char c++ will keep adding strings in memory because its creating a new pointer everytime I call dbPrint or a myvar/char.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jul 2009 23:30
dbPrint shouldn't be a problem but if you use dbStr to convert other data to a string then, yes, you have to delete the allocated space or else you get memory leak. That's why the sprintf works so much better. You have to declare an accessible character array somewhere in your program that the function can deposit the formatted string into. But you don't have to worry about managing the memory. You do, however, need to make it large enough to hold the longest string of text that you're apt to generate.

Alternately, you can also create this array using a pointer and dynamically allocated memory at the start of your program and only have to delete it when the program closes.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-10-01 08:10:53
Your offset time is: 2024-10-01 08:10:53