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 / Entry = Null

Author
Message
Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 15th Jan 2010 01:12 Edited at: 15th Jan 2010 01:15
hi ive been trying to make a system where u type and it shows, and i dunno why this isnt working, so i thought id post it here:



however, Button[textboxclick].TextBox, gets spammed with "(null)" from Textm. and so very quickly it makes a string too long and crashes.

how can i stop this?

ok..

it seems, if i hold a key before i click the textbox, itll work, so i added
Textm = "";
near the top, but i still get the error if i click the textbox before i have pressed a key.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 15th Jan 2010 01:34
If you use a GDK function that returns a char*, always wrap the call in a function like this:



Not only does this make it so you don't have to handle deallocation of the strings, but it also prevents issues like the ones you're having because the returned value can never be NULL/0.

Also remember that c-style strings, that is 'char*' ones, are arrays of chars and aren't really anything special, thus 'Textm != "(null)"' will not work, because you cannot compare arrays like this. This is merely comparing the pointers to the arrays, and in this case that will always fail the equality test. Instead, if you use std::string, you can use the '.compare()' method, where a value of 0 indicates identical strings. It's best that you follow a tutorial on c-style strings, as well as std::string.

Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 15th Jan 2010 01:37 Edited at: 15th Jan 2010 01:45
the command returns an int, for the object selected.



this is when i define the string:



can u explain to me how

will work in this situation, or cant it?

(ps is it always a good idea to delete chars after using them then?)

thanks in advance, and ty.

(pps im a noob at cpp who doesnt understand std::string (is it like a function), so ill be very thankful if u dumb it down =))

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 15th Jan 2010 02:07 Edited at: 15th Jan 2010 02:12
std::string is a class that handles strings way better than char*. Basically you '#include <string>' to load the definitions and you can use it like so:



You can find more info on this at site such as this, the way you use the string with dbPrint and such may look complex, but that's just because GDK is stupid, so you have to cast away the constness of the string, as well as returning the 'const char*' via .c_str().

The reason the delete[] operator is used in my function is because the GDK return strings are all allocated using new[], so they have to be deallocated somewhere, and it's easiest to do that in the function right after I copy the string over: 'output = raw;'.

Unless you have some very specific requirements, you should use std::string to store every single string in your game, this saves so much time and prevents so many potential issues. And all of your functions expecting strings should take the argument 'const std::string&' as shown above, this ensures the class itself is never copied as it's passed by reference, as such, it means the syntax is the same as passing it by value 'std::string'. Also, making it const-correct makes it harder for the function taking the parameter to modify the value, which can be a very useful feature.

[edit] and to actually answer your question, using my function will ensure the returned string won't be NULL, thus you don't have to include any special code to handle that case.

[edit2] 'Textm = "";', this will effectively do nothing, textm is a pointer to an array of the type char(-128 to 127), and the line 'Textm = dbEntry();' will overwrite the pointer to the array, which can be NULL because dbEntry() can return this.

Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 15th Jan 2010 14:59 Edited at: 15th Jan 2010 15:00


i added that to my code, and it appears, but when i added the += entry part, it gives me very weird memory errors.


and your first function i ran i get:


dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 15th Jan 2010 18:59
Oops, in my lib I've typedefed all the basic types(other than bool) to be the simpler i/c/u/f(size in bits). Just replace c8 with char.

Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 17th Jan 2010 00:53 Edited at: 17th Jan 2010 00:58
that system, i fear, doesn't work.


Textm returns the correct key.
Entry returns something along the lines of... (my keyboard cant type the keys).. aka computer gibberish.


[IMPORTANT EDIT]
it might be working, just so u dont post in futile, i realised it was img not being a string, i did:
dbText(curx,cury,(char*)Entry.c_str()); cury += 15;
and it returned correctly, so i think i got it working.


Thankyou so much for your help, i shall use this std::string thing from now on =), its alot easier to use.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Jan 2010 02:20
%s expects a 'const/char*', thus passing a std::string will not work as expected. To pass this, pass the myStdString.c_str() as that will return one. This is also one reason why using such C functions(sprintf, etc) isn't a good idea as it lacks any type checking.

Login to post a reply

Server time is: 2024-10-01 23:41:12
Your offset time is: 2024-10-01 23:41:12