class design question

M

mickey

I know this may sound ridiculous but I need sanity check. I am
developing in c++ for an embedded device where memory is tight and I
have a class which always upon initialization dynamically allocates a
10kb buffer. Since this type of class object is always dynamically
allocated, I am wondering if there is any advantage to allocating the
buffer dynamically. I am thinking the buffer can simply be defined and
statically allocated as a class member? One obvious drawback is if I
ever decide to allocate the class statically and that would be
impossible in the confines of the embedded device stack. Feedback is
welcomed, thanks.
 
V

Victor Bazarov

mickey said:
I know this may sound ridiculous but I need sanity check. I am
developing in c++ for an embedded device where memory is tight and I
have a class which always upon initialization dynamically allocates a
10kb buffer. Since this type of class object is always dynamically
allocated, I am wondering if there is any advantage to allocating the
buffer dynamically. I am thinking the buffer can simply be defined and
statically allocated as a class member? One obvious drawback is if I
ever decide to allocate the class statically and that would be
impossible in the confines of the embedded device stack. Feedback is
welcomed, thanks.

There is a difference between having a member array of 10K or dynamically
allocate the array and have a member pointer to the first element (the way
you describe it is currently). Namely, the difference is _where_ the data
happen to reside. For an automatic object ("local" to a function or as
a subobject of another "local" object), the storage is allocated _usually_
on the "stack". Dynamically allocated memory is taken from "free store".
On your system those are likely different and the former can be simply too
limited compared to the latter.

V
 
W

WittyGuy

Allocating the memory dynamically will eventually use heap for
allocation, which is supposed to be most effective way of using memory
until you free those allocated memory. So for the embedded device, to
be more memory conscious, I think, dynamic way of memory handling will
give much advantage over statically allocation of memory.

--Wg-
 
J

Jay Nabonne

Allocating the memory dynamically will eventually use heap for
allocation, which is supposed to be most effective way of using memory
until you free those allocated memory. So for the embedded device, to
be more memory conscious, I think, dynamic way of memory handling will
give much advantage over statically allocation of memory.

Actually, on embedded devices, a lot of dynamic memory usage can be a
detriment - you may eventually fragment the (relatively small,
non-virtual-memory) heap, resulting in system failure. Not to say dynamic
allocations are bad; you just have to be careful about what you're doing.

(I'm a little unclear on the "most effective way of using memory" comment,
but we don't really have to go there.)

- Jay
 
M

mickey

Jay said:
Actually, on embedded devices, a lot of dynamic memory usage can be a
detriment - you may eventually fragment the (relatively small,
non-virtual-memory) heap, resulting in system failure. Not to say dynamic
allocations are bad; you just have to be careful about what you're doing.

(I'm a little unclear on the "most effective way of using memory" comment,
but we don't really have to go there.)

- Jay

Completely true and that is one of things I try to avoid, memory
fragmentation, which can be the cause of failure in an embedded device.
Most desktop developers seem to have little understanding of that.
But having the static member array also requires a larger contiguous
memory chunk as opposed to the dynamic allocation scheme.

-Mickey
 
D

Daniel T.

mickey said:
Completely true and that is one of things I try to avoid, memory
fragmentation, which can be the cause of failure in an embedded device.
Most desktop developers seem to have little understanding of that.
But having the static member array also requires a larger contiguous
memory chunk as opposed to the dynamic allocation scheme.

Those of use who used to program for MacOS 9 and earlier know full well
about memory fragmentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top