Pages: 1
  Print  
Author Topic: Using standard c/c++ classes and explicit memory management in ENIGMA  (Read 9130 times)
Offline (Unknown gender) CCJ
Posted on: December 25, 2011, 11:30:59 pm
Member
Joined: Dec 2011
Posts: 2

View Profile Email
Hi all,
Is it possible to include standard c/c++ classes in ENIGMA in addition to or instead of EDL?  If possible I'd prefer to code in straight c++ using the ENIGMA API functions, rather than scripting in EDL.  Also, is explicit memory management via malloc/free and new/delete supported in EDL?  I'm not sure how Game Maker handled memory, but I hope ENIGMA allows developers to take the reins with application memory rather than relying on a garbage collector.  Specifically, what exactly is the code equivalent of the drag and drop 'destroy instance' icon in ENIGMA?  Is the memory freed immediately or simply marked as eligible for reclamation by a GC?

thanks,
CCJ
Logged
Offline (Unknown gender) TheExDeus
Reply #1 Posted on: December 26, 2011, 11:18:01 am

Developer
Joined: Apr 2008
Posts: 1860

View Profile
Quote
If possible I'd prefer to code in straight c++ using the ENIGMA API functions, rather than scripting in EDL.
That's the plan. You will be able to use most if not all C++. EDL will just allow additional cool things to make coding faster and easier. Almost every aspect of C++ is in already.

Quote
Specifically, what exactly is the code equivalent of the drag and drop 'destroy instance' icon in ENIGMA?
Just like in GM its: instance_destroy().

Quote
Is the memory freed immediately or simply marked as eligible for reclamation by a GC?
I think its freed immediately, although looking at the code it seems to unlink it for GC.
Logged
Offline (Male) Rusky
Reply #2 Posted on: December 26, 2011, 12:57:51 pm

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
EDL is translated directly to C++, so as long as you can do what you want with it there's no need to worry about efficiency. There is also no garbage collector in either GM or ENIGMA, and explicit new/delete is generally a bad way to manage memory.
Logged
Offline (Male) Josh @ Dreamland
Reply #3 Posted on: December 26, 2011, 01:47:51 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
To expand on what has been said, all C++ functions are game in ENIGMA provided you include them from Definitions, which IsmAvatar will be adding to the resource tree as an instantiable resource shortly. EDL is very similar to C++, but you must configure some settings to use otherwise ambiguous operators (such as ++). Currently, the string behavior setting is broken.

Instances are not freed immediately; their iterators are unlinked and any open iterators are updated to be kept valid. The instance is then added to a set for deletion at the end of the step. This way, you can do asinine garbage like with (a) { instance_destroy(); instance_create(x,y,obj_explosion); } without tripping any access violations.

The new parser I am working on will correctly integrate the new C++ syntax features. Presently, member access is limited due to the dynamic nature of GML, and ternary expressions can often cause compile error if you mix types in them (expression? var : int gives an ambiguous cast error, because var can cast to int as well as construct from int).
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) CCJ
Reply #4 Posted on: January 10, 2012, 12:14:19 pm
Member
Joined: Dec 2011
Posts: 2

View Profile Email
thanks all for the informative replies!

Quote
Instances are not freed immediately; their iterators are unlinked and any open iterators are updated to be kept valid. The instance is then added to a set for deletion at the end of the step. This way, you can do asinine garbage like with (a) { instance_destroy(); instance_create(x,y,obj_explosion); } without tripping any access violations.

ah, sounds good 

Quote
explicit new/delete is generally a bad way to manage memory
@Rusky: could you elaborate please?  If not with malloc/free and new/delete, how should memory be managed? 
Logged
Offline (Unknown gender) luiscubal
Reply #5 Posted on: January 10, 2012, 12:27:38 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Consider the following code:

Code: [Select]
MyClass foo; //the constructor of foo is called automatically
//do something with foo

In the case above, the variable "foo" is automatically destroyed when it goes out of scope.

With explicit new, it's very easy to forget an edge case and miss a "delete", which results in a memory leak.
Additionally, variables allocated as shown above("on the stack") tend to be friendlier to the computer(due to relatively advanced stuff like CPU cache, overhead of memory allocation and memory fragmentation) than data allocated with new("on the heap").
Finally, classes allocated on the stack are automatically constructed. The same does not apply to pointers. If you forget to add "= new MyClass()" after the pointer declaration, then using that pointer is undefined behavior.

There are, indeed, cases where variables allocated on the stack are not appropriate, and then "new"/"delete" is indeed an option, but there are also other classes provided by C++(known as "smart pointers") that make memory easier to manage.

In short, explicit new/delete is harder to use, slower and makes it easy to make critical mistakes.
« Last Edit: January 10, 2012, 12:32:46 pm by luiscubal » Logged
Offline (Male) Josh @ Dreamland
Reply #6 Posted on: January 10, 2012, 02:21:51 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
...
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) luiscubal
Reply #7 Posted on: January 10, 2012, 02:47:47 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Hey, Josh, you have your computer on Mute. Enable the sound again so we can hear what you're saying.

(Because, y'know, an ellipsis is not a very constructive comment)
Logged
Offline (Male) Rusky
Reply #8 Posted on: January 10, 2012, 05:54:55 pm

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
In Josh's terms, using classes like vector that do the new/delete for you requires less typing.

Also if you use exceptions, you kind of have to avoid vanilla new/delete because otherwise memory will leak.
Logged
Pages: 1
  Print