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 / DGK C++; Getting Display Types

Author
Message
s_i
9
Years of Service
User Offline
Joined: 23rd May 2009
Location: Russia
Posted: 5th Jul 2014 19:03
@ TheComet

#1 -- tested, no leaks.
#2 -- tested, no leaks. (added "int n;")
The Tall Man
4
Years of Service
User Offline
Joined: 16th Nov 2013
Location: Earth
Posted: 5th Jul 2014 19:32 Edited at: 5th Jul 2014 19:44
Delete is unsafe for memory that you've not allocated. The reasons are obvious. You're apparently getting away with it for the time being, and maybe you could indefinitely if you'd like to take that gamble. Why leave that memory bug open? Why not just fix the source code? As for checklists though, I'm pretty sure I saw a db cleanup/delete/empty checklist command somewhere.

Also, frequently used new and delete pairs are slow, due to the windows memory manager, and could result in system memory fragmentation (meaning new allocations would become slower and slower, and it would be more difficult to allocate a large memory block in the future).

In my software, if I anticipate a need for a frequently used local new and delete pair, I will allocate once, a higher-scope variable that can be reused just for that purpose.

Judging what we see is the greatest blinder and self-limiter in the universe.

What we perceive is never reality. It is only a story we tell ourselves based on our current perspective, which has far more to do with our beliefs about ourselves than with anything else.
TheComet
10
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 5th Jul 2014 21:11
@s_i - Thanks. You were right all along, I just didn't see it to begin with.

@The Tall Man
Quote: "Also, frequently used new and delete pairs are slow, due to the windows memory manager, and could result in system memory fragmentation (meaning new allocations would become slower and slower, and it would be more difficult to allocate a large memory block in the future).

In my software, if I anticipate a need for a frequently used local new and delete pair, I will allocate once, a higher-scope variable that can be reused just for that purpose."


My code was testing for memory leaks which requires frequent use of new/delete.

Quote: "Delete is unsafe for memory that you've not allocated. The reasons are obvious. You're apparently getting away with it for the time being, and maybe you could indefinitely if you'd like to take that gamble."


Unless the library you're using actually requires you to clean up after it, which is the case with DarkGDK. We've established that DarkGDK allocates all strings using new[] in this function here:


And additionally allocates the string a second time for you whenever you call any of the string functions, but it won't take care of deleting them. That's up to you.

Quote: "Why leave that memory bug open? Why not just fix the source code? As for checklists though, I'm pretty sure I saw a db cleanup/delete/empty checklist command somewhere."


The only smart way to solve this problem would be to have DarkGDK return std::string instead of raw pointers. If anyone here is up for re-writing the entire string system of DarkGDK then be my guest.

WickedX
9
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jul 2014 08:40 Edited at: 6th Jul 2014 08:49
I was just about to complete this game and decided to bring the code up to date. This function returns the formatted game time as - MM:SS. I cannot seem to get around the necessity to use new and delete in the function. The dbText() function will not compile without removing the strings constantness. So, how else should this be coded?



Thanks
Dar13
10
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 7th Jul 2014 16:01
I would code it up like this:


It removes the need for the new/delete while also removing the need to access the members of the time struct. However, there's no way to get rid of the const_cast since Lee used regular char* instead of const char*.

WickedX
9
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 7th Jul 2014 21:43
I like that time format conversion function, much better. I did consider using a char array in the function. But, as I never seen a char array defined in a function like that, I thought it maybe sloppy code. How would the memory be allocated, at compile time or runtime on the stack or on the heap?

Thanks
Dar13
10
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 7th Jul 2014 22:01
That time function can even be done like this to avoid some magic numbers:


The array would be on the stack and would be deallocated upon exiting the function. The nice thing about stack arrays is that sizeof(arr) is equal to the value that is used to create (6 in this case). Just be careful that when the array is passed to another function it decays to a pointer rather than keeping that special information that allows sizeof() to correctly gauge its size.

WickedX
9
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 7th Jul 2014 22:07
Nice! Thank you.

Login to post a reply

Server time is: 2018-10-15 18:35:32
Your offset time is: 2018-10-15 18:35:32