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 / If char* Is Not 'Blank' Then...

Author
Message
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 7th Jun 2009 02:27
Let me start you off with the code:


Well, whats happening is that numcount just keeps going up, even if de is supposed to be empty. I'm not too sure why this is doing this.. =/

AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 7th Jun 2009 03:52


mikeMarek 597
15
Years of Service
User Offline
Joined: 12th Feb 2009
Location:
Posted: 7th Jun 2009 05:05
Shouldn't char types be declared with single quotes, not double?
WH8
15
Years of Service
User Offline
Joined: 17th Mar 2009
Location:
Posted: 7th Jun 2009 07:24 Edited at: 7th Jun 2009 07:26
char *blank can be used to point to a single character or to an array of characters. An array of characters is a string. When pointing to a single character, you would use single quotes around the character. When pointing to a string, you would use double quotes around the string.



In the above code, Letter is a pointer to a single character. String is a pointer to a null-terminated string. Note the quotes used for each initialization.

If I am incorrect or not completely correct in any way, please someone make the necessary corrections.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 7th Jun 2009 07:33 Edited at: 7th Jun 2009 07:42


Also, look at this line:

if(de != blank);

You're terminating the conditional before you specify the block of code that it's supposed to control. In theory the block of code will always be executed.

EDIT: I just noticed another problem with the code. Assuming that

curtxt = de

means that curtext is a char* and you're trying save the pointer to point to the same text as de then it won't work because when you

delete [] de;

you're deallocating the same space that curtext is pointing to. If, on the other hand, you're trying to copy the text into a character array then you want to use strcpy (curtext, de). If on the third hand curtext is a string then it's okay.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 7th Jun 2009 07:58 Edited at: 7th Jun 2009 08:00
Lilith, when I do it your way my program throws an exception
When I do it anyone elses way the counter just keeps going up no matter what..

Current Code:


Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 7th Jun 2009 12:27 Edited at: 7th Jun 2009 12:29
I tested this dbGetEntry and the problem seems to be that after the first test, the returned pointer is always non-zero, even if the string is empty. The other problem is that I can't use strcmp because it would require a const pointer and dbEntry return value is not constant. So, another comparison method is needed, and the simplest is to test if the first character of the return string (where that dbGetEntry pointer points) is a null terminator or not. Try this code:



P.S. In any case, storing the pointer that dbGetEntry returns and then trying to delete that is a bad idea, since this is a system-managed string that you are not supposed to delete. The dbCearEntryBuffer function in the above example will empty the input string after reading the next character.
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 7th Jun 2009 13:13 Edited at: 7th Jun 2009 13:21
kklouzal you should learn about C strings and pointers before trying to use them. You can't use them in the same way as standard or DBP strings.

I suggest reading the tutorials here and practising, you will eventually understand:
http://www.cplusplus.com/doc/tutorial/

Quote: "The other problem is that I can't use strcmp because it would require a const pointer and dbEntry return value is not constant."

You can use strcmp fine, without type casting.

Quote: "I tested this dbGetEntry and the problem seems to be that after the first test, the returned pointer is always non-zero, even if the string is empty."

When I tested it, it initially returned NULL.

Quote: "P.S. In any case, storing the pointer that dbGetEntry returns and then trying to delete that is a bad idea, since this is a system-managed string that you are not supposed to delete. The dbCearEntryBuffer function in the above example will empty the input string after reading the next character."

Actually the string returned by dbGetEntry (and so far as I know, all other DGDK commands) is not internal memory, it is allocated purely for the purpose of the return string so you need to deallocate it with delete[] to avoid a memory leak.


Here is some sample code, which also shows the pointer address. If you remove delete[] the pointer address changes with every dbGetEntry, which suggests that it is not constant internal memory and so needs to be deallocated:


Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 7th Jun 2009 14:13
Something every C/C++ programmer should know is that when handling strings you are actually just handling a block of memory.

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 7th Jun 2009 14:26
Quote: "Actually the string returned by dbGetEntry (and so far as I know, all other DGDK commands) is not internal memory, it is allocated purely for the purpose of the return string so you need to deallocate it with delete[] to avoid a memory leak."


Thanks for that correction, Michael!

For some reason, strcmp didn't want to compile when I tried it, but it works in your code.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 7th Jun 2009 18:14 Edited at: 7th Jun 2009 18:15
Quote: "When I tested it, it initially returned NULL."


Any chance that it returns NULL on an empty entry and that's what you need to test against. It would explain why doing the comparison my way would throw an exception. NULL is actually an invalid value so you can't dereference it.

Quote: "I can't use strcmp because it would require a const pointer and dbEntry return value is not constant."


strcmp doesn't require a constant pointer. It requires a pointer to an array of characters that will stay constant during the function call. I believe a constant pointer would be declared as

char const *

But it's been a while since I overly concerned myself with this sort of thing.

Also, if indeed the pointer value returned by dbEntry() is a NULL you shouldn't delete the array it points to since it points to nothing.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 8th Jun 2009 10:52 Edited at: 8th Jun 2009 11:13
(sorry for the late reply internet issues this weekend)

Thanks Mike P! I incorperated elements of your example into my code and it works flawlessly now! =D


Edit: Not just Mike P, Thanks everyone ;]

Login to post a reply

Server time is: 2024-11-05 23:00:08
Your offset time is: 2024-11-05 23:00:08