Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Mon Nov 24, 2014 9:16 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sat Mar 16, 2013 5:51 am 
BANNED

Joined: Sun Jun 24, 2012 12:49 am
Posts: 504
Is delete[] for objects without destructors like freeing a contiguous block of memory from the heap?

e.g.
Code:
float* meh = new float [19];
delete [] meh;


in comparison to

Code:
float* meh = (float*)malloc(4*19);
free(meh);


?

I guess I could just use the assembly-output compiler option and look at that...


Top
 Profile  
 
PostPosted: Sat Mar 16, 2013 12:54 pm 
Gamer Geek

Joined: Fri Dec 07, 2012 2:54 pm
Posts: 35
I know the first example clears the memory from the heap and I suppose the second one does the same.
Destructors I believe is useful if you create objects on the heap inside the class.

BTW reason I'm not posting lately is bc of the massive amount of assignments we're getting.


Top
 Profile  
 
PostPosted: Sat Mar 16, 2013 9:48 pm 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3821
Location: South Africa
Pieman wrote:
Is delete[] for objects without destructors like freeing a contiguous block of memory from the heap?

e.g.
Code:
float* meh = new float [19];
delete [] meh;


in comparison to

Code:
float* meh = (float*)malloc(4*19);
free(meh);


?

I guess I could just use the assembly-output compiler option and look at that...

I'd imagine they boil down to the same thing after optimization. My gut tells me that delete [] would free each individual element of the array. But compiler optimizations would probably be smart enough to just free the whole block if there's no destructor method.

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Sun Mar 17, 2013 12:31 am 
BANNED

Joined: Sun Jun 24, 2012 12:49 am
Posts: 504
Thanks for the thoughts. :)


Top
 Profile  
 
PostPosted: Sun Mar 17, 2013 8:53 am 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 366
Location: Here (where else?)
IGTHORN wrote:
My gut tells me that delete [] would free each individual element of the array. But compiler optimizations would probably be smart enough to just free the whole block if there's no destructor method.
The C++ manual states that objects created with new[] should also get deleted together with delete[]. You gut feeling would be one valid implementation internally, but it is incorrect to make such assumptions.

I don't know what the new[] and delete[] operators do in this case, it may be useful to have a look at its API.



To be honest, I don't understand the reason of the question of the OP. Using free versus delete is a consequence of using malloc versus new during construction. Compared to the effort of doing memory administration in the first place, I don't think the difference is measurable in the case of not having destructors. If it is measurable, you must be doing a lot of memory operations, and you may want to consider using a cheaper solution, eg use a pool of free objects or so.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun Mar 17, 2013 6:51 pm 
BANNED

Joined: Sun Jun 24, 2012 12:49 am
Posts: 504
Yeah, I decided to not worry about it. Logically, an array allocated with new[] and remembered with a pointer must be a contiguous allocated block, so it doesn't make sense for numerous free actions to occur -- or does it? If you use the native WinAPI heap management functions, is it possible to handle allocations in more complex ways? The answer to this side-question is not very important for my needs, but I'm just curious about the real mechanics of heap management. Don't even mind, I can look it up for myself.


Top
 Profile  
 
PostPosted: Tue Mar 19, 2013 5:50 pm 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 366
Location: Here (where else?)
Managing dynamic memory is a research area, think of the innovations that Java introduced in this area.
Even for C++, exotic things as garbage collectors exist :)

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Tue Mar 19, 2013 9:57 pm 
BANNED

Joined: Sun Jun 24, 2012 12:49 am
Posts: 504
Quote:
think of the innovations that Java introduced in this area.

lol, I was just curious about whether it's possible to split an allocation into multiple blocks and free/manipulate the new partitions using WinAPI.

http://msdn.microsoft.com/en-us/library ... 85%29.aspx
I don't have time to look into this stuff, at the moment.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group