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 / DarkGDK exits on keypress??? what

Author
Message
bangity
16
Years of Service
User Offline
Joined: 13th Apr 2008
Location:
Posted: 14th Apr 2008 02:51


Please look at the code above. Is there any other wait to print text on screen with a variable in between the char*?

This should works fine but whenever the Keypress returns 1, the application exits out.. I don't know why this happened. Or someone else has a better way of doing this..

Thanks
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 14th Apr 2008 04:26 Edited at: 14th Apr 2008 04:33
You can't do that with *num.

char *num = ""

declares a pointer to a string literal that is empty. What you need to realize is that this program structure limits the length of the string to exactly one character, the null character that terminates the string. Anything other than an empty string that's copied to where the pointer points will over flow into some other area and probably destroy what's there.

when you do

Quote: " strcat(txt, itoa(count, num, 10));"


you're overwriting the next area, the content of which isn't known unless you look at the compiled code, which could cause the program to misstep during runtime.

If you want to reserve more room you can do something like

char *num = " ";

with enough spaces to accommodate the largest number of characters you expect to use. Or, you can declare the following

char num [25];

to reserve 25 character spaces

ACCCCCHHHHHHHH. Just realized you did the same thing with txt.

You assigned a literal string to txt. That fixes the length of the string and anything that you append to it overflows into other data area.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 14th Apr 2008 04:38
Try the following:


You might also look at the standard library sprintf () function for formatting text into a string. There's also a way to deal with the (s) you have appened. But I won't go into that unless you want.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
bangity
16
Years of Service
User Offline
Joined: 13th Apr 2008
Location:
Posted: 14th Apr 2008 17:45
thanks.. I totally forgot about the overflow of the memory thing..

I used *num while did not initialized it to a valid memory address..

but there should be an runtime error for this one.. why is it not there? because of DarkGDK?
bangity
16
Years of Service
User Offline
Joined: 13th Apr 2008
Location:
Posted: 14th Apr 2008 17:49
and how do I make it so that the dbtext don't overlap on each other? Do I have to clear and background everytime?
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 14th Apr 2008 21:03
Quote: "thanks.. I totally forgot about the overflow of the memory thing..

I used *num while did not initialized it to a valid memory address..

but there should be an runtime error for this one.. why is it not there? because of DarkGDK?"


Acutally num is initialized to a valid memory address. But the space reserved for it to use is limited to the one character. Be cause your string was empty it only had the null terminator that marks the end of the string.

What happens when you assign a character pointer to a string literal is that the system sets the string up in somewhere and allocates only enough space to contain the string. Then your statement set the value of a character pointer to the location of that string. If the pointer is used to modify the string it needs to keep within the bounds of the space that the literal was assigned.

Quote: "and how do I make it so that the dbtext don't overlap on each other? Do I have to clear and background everytime?"


You can do that or, as I do, just put a dbBox over the area before writing with dbText(). If the screen were automatically cleared for other reasons before this then that wouldn't be necessary.

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

Login to post a reply

Server time is: 2024-09-29 17:29:08
Your offset time is: 2024-09-29 17:29:08