Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Factory pattern and MIRSA C++ Guidelines on Dynamic Allocation

86 views
Skip to first unread message

Mark

unread,
Jul 1, 2016, 11:45:08 PM7/1/16
to

I'm perusing MIRSA guidelines for an embedded project and 18-4-1 imposes a requirement that forbids dynamic allocation. Specifically, 'Dynamic heap memory allocation shall not be used.'

The factory pattern is suitable for one aspect of my code but the factory design is predicated upon dynamic allocation - at least my experience. Can I use factory design with static allocation? Did a search and I think the answer is no but thought I check in the forum



Link:

http://www.ldra.com/attachments/article/74/MISRA-CPP_2008_COMPLIANCE.pdf?reg=short

Chris M. Thomasson

unread,
Jul 2, 2016, 12:59:21 AM7/2/16
to
This just might be of no use, but it gets around dynamic allocation!

;^)


https://groups.google.com/d/msg/comp.lang.C++/7oaJFWKVCTw/sSWYU9BUS_QJ

https://groups.google.com/forum/#!original/comp.lang.C++/7oaJFWKVCTw/sSWYU9BUS_QJ

http://pastebin.com/f37a23918
(the code/hack)


I have used it in environments that did not support dynamic allocations.



Marcel Mueller

unread,
Jul 2, 2016, 4:31:46 AM7/2/16
to
On 02.07.16 05.44, Mark wrote:
> The factory pattern is suitable for one aspect of my code but the factory design is predicated upon dynamic allocation - at least my experience. Can I use factory design with static allocation?

Yes.

The storage for the target need to be provided by the caller and the
factory can use placement new to do its job. You might also use this
with polymorphism as long as the target storage is large enough to keep
any variant.


Marcel

Mr Flibble

unread,
Jul 2, 2016, 1:13:58 PM7/2/16
to
The factory pattern does not require dynamic freestore-based allocation;
you can reserve space for all supported concrete objects that the
factory creates.

As far as C++ is concerned and at its most basic level all the factory
pattern is is the calling of virtual functions.

/Flibble

Mark

unread,
Jul 5, 2016, 9:43:30 PM7/5/16
to
On Saturday, July 2, 2016 at 1:13:58 PM UTC-4, Mr Flibble wrote:

> >
>
> The factory pattern does not require dynamic freestore-based allocation;
> you can reserve space for all supported concrete objects that the
> factory creates.
>
> As far as C++ is concerned and at its most basic level all the factory
> pattern is is the calling of virtual functions.
>
> /Flibble

Do you have a reference or can you provide sample code illustrating factory design use withouth dynamic freestore-based allocation?
Thanks
Mark

Scott Lurndal

unread,
Jul 6, 2016, 8:39:00 AM7/6/16
to
Simply add 'new' and 'delete' operator overrides to the class
that the factory creates and have them allocate from a fixed length
array of objects defined at compile time. Or create a pool allocator
and initialize the pool at compile time.

Öö Tiib

unread,
Jul 7, 2016, 10:26:31 AM7/7/16
to
Can you be more specific what you see there as a challenge?
Reference ... perhaps C++ FAQ ... may be that one:
https://isocpp.org/wiki/faq/dtors#placement-new


0 new messages