Actually, the new() operator isn't a storage allocator, it's a storage 
locator.  You can write your own memory manager (like hal_malloc) to 
decide where to put things.  It's just that the default new operators 
also happen to allocate space before telling the caller a location - 
it's the easiest way to guarantee that there's storage for the object.

An interesting application for that feature is to have a new() operator 
for a class representing a piece of memory-mapped hardware.  You make 
the data layout match the hardware, and overload new() to point to the 
physical memory address of the hardware.  The vtables and other assorted 
"behind-the-scenes" data are kept in a different place (function 
pointers are managed on a class basis, not an instance basis - at least 
for non-virtual classes), so you don't have to worry about c++ screwing 
around with the hardware.

Note - I'm only pointing out some interesting language applications, I'm 
not suggesting that HAL be rewritten in c++ (yet :) ).

- Steve

ben lipkowitz wrote:

>On Wed, 10 Oct 2007, Kenneth Lerman wrote:
>  
>
>>I'm very comfortable using the standard C libraries, but have concerns about
>>using the C++ class libraries. Specifically, I'm concerned that their use of
>>memory might be somewhat less deterministic than the rest of emc. In a
>>previous life involving other projects of this nature, I heard strong
>>arguments both for and against using such libraries. I purposely avoided
>>using the map class in my code.
>>
>>Ken
>>    
>>
>
>I'm not too familiar with C++ memory allocation, but I thought I'd share 
>this nifty hack, courtesy of pete vavaroutsos:
>
>#ifdef __cplusplus
>// Overloaded new operator used to call constructors explicitly without
>// memory allocation. This prevents dynamic reallocation of memory which can
>// cause problems if we request memory that is not available - not a good
>// situation to be in when you are rapidly moving chunks of iron around.
>inline void *operator new (size_t s, void *ptr) { return(ptr); }
>inline void     *operator new [] (size_t, void *ptr) { return(ptr); }
>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Emc-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/emc-developers

Reply via email to