avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] adding wrapper functions for new and delete operators


From: Bradley Jarvis
Subject: Re: [avr-libc-dev] adding wrapper functions for new and delete operators
Date: Tue, 08 Feb 2011 08:46:14 +1100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20110114 Lightning/1.0b2 Thunderbird/3.1.7

Hi David

On 02/07/11 18:44, David Brown wrote:
> On 05/02/2011 07:47, Bradley Jarvis wrote:
>> Hi
>> I use avr-gcc for c++ code (have so for years) and wanted to use new and
>> delete, particularly for libraries that I have written for and used in
>> an x86 environment which I didn't want to have to convert. So I wrote
>> the following wrapper functions, please feel free to add it to avr-libc
>>
>> To go into a header file (not sure what the standard c++ header file new
>> and delete function would normally exist in, that that is where the
>> declarations would reside):
>>
>> void *operator new(size_t);
>> void *operator new[](size_t);
>>
>> void operator delete(void *);
>> void operator delete[](void *);
>>
>>
>> To go into the source file:
>>
>> void *operator new(size_t s)
>> {
>>      return malloc(s);
>> }
>>
>> void *operator new[](size_t s)
>> {
>>      return malloc(s);
>> }
>>
>> void operator delete(void *m)
>> {
>>      free(m);
>> }
>>
>> void operator delete[](void *m)
>> {
>>      free(m);
>> }
>>
>>
>> I have used these functions for years to get new and delete
>> functionality into my avr-gcc code.
>>
>> BTW get job on the gcc avr support, it's a fantastic product. The only
>> other thing I would like to use that is not supported are exceptions, it
>> would make some software easier.
>>
>> Thanks Brad
>>
>
> I haven't yet done very much C++, but doesn't the new() operator have
> to call the function's constructor, and the delete() call it's
> destructor?  Or is that handled by the compiler automatically after it
> uses new() to get the memory area?
>
> mvh.,
>
> David
>
>
> _______________________________________________
> AVR-libc-dev mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/avr-libc-dev
>
>

The compiler must handle calling the constructor after the memory has
been allocated and the destructor before releasing memory. I suppose
this is because new can also be used to allocate memory for structures
and arrays of non object type.

The wrapper functions appear to work quite well and the class objects
created with new do behave like one created using gcc for the x86 arch
including running the constructor and destructor. I have tested the
above wrappers and used them for several years (at least from 2007,
possibly earlier) and have found no adverse problems.

>From my point of view it appears to be a fairly trivial update, but I am
not fully aware of how the x86 arch new and delete operators work at a
low level though.

Thanks, Brad




reply via email to

[Prev in Thread] Current Thread [Next in Thread]