By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

STL containers and managing memory allocation in embedded systems

P: n/a
The memory allocation issue in embedded systems is usually critical..

How can one manage that?

1. Via 'new'
char* p = new (nothrow) char [SOME_SIZE];
if (p == 0)
{
// He we know that it is impossible to allocate the requested memory
// We can do something relevant.
}
2. But how to manage memory allocation in containers for embedded
systems?

For instance,
vector<Foo> v;
Foo f;
v.push_back(f);

push_back() returns no values.

So, how can we know (except try-catch) that it is impossible to
allocate the requested memory in push_back()?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 31 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Alex:

When you say 'vector<Foo> v;' actually you are saying:
vector<Foo,allocator<Foo> > v;
because of the default second parameter of the vector template
definition:

template <class T, class Allocator = allocator<T> >
class vector.....

And the member allocate() of allocator "...Throws the exception
bad_alloc if the storage is unavailable. This function uses operator
new(size_t)..."

Hope this help.

Mar 31 '06 #2

P: n/a
In comp.arch.embedded Alex Vinokur <al****@users.sourceforge.net> wrote:
The memory allocation issue in embedded systems is usually critical..
It's usually beyond critical --- it's inacceptable, period. An
embedded system has nobody to complain to if an operation as strictly
internal as a memory allocation fails. So it had better not go there
at all.

If you can't afford to allow exception handling, you generally can't
allow C++ style dynamic memory handling. It's as easy as that.
How can one manage that?
Primarily by not doing it.
2. But how to manage memory allocation in containers for embedded
systems?


Primarily by not using them.

--
Hans-Bernhard Broeker (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Mar 31 '06 #3

P: n/a

"Alex Vinokur" <al****@users.sourceforge.net> wrote in message news:11**********************@t31g2000cwb.googlegr oups.com...
[snip]
2. But how to manage memory allocation in containers for embedded
systems?

For instance,
vector<Foo> v;
Foo f;
v.push_back(f);

push_back() returns no values.

So, how can we know (except try-catch) that it is impossible to
allocate the requested memory in push_back()?

[snip]

By the way, is this check correct?
vector<Foo> v;
// Stuff

size_t the_size = v.size();
Foo f;
v.push_back (f);

if (v.size() != (the_size + 1))
{
cerr << "Unable to allocate memory via push_back()" << endl;
}
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 31 '06 #4

P: n/a

Alex Vinokur wrote:
By the way, is this check correct?
vector<Foo> v;
// Stuff

size_t the_size = v.size();
Foo f;
v.push_back (f);
The standard behaviour when any memory allocation fails is to throw a
std::bad_alloc exception, so if push_back needs to allocate but is
unable, you don't get to the code below at all.
if (v.size() != (the_size + 1))
{
cerr << "Unable to allocate memory via push_back()" << endl;
}


Gavin Deane

Mar 31 '06 #5

P: n/a

Gavin Deane wrote:
Alex Vinokur wrote:
By the way, is this check correct?
vector<Foo> v;
// Stuff

size_t the_size = v.size();
Foo f;
v.push_back (f);


The standard behaviour when any memory allocation fails is to throw a
std::bad_alloc exception, so if push_back needs to allocate but is
unable, you don't get to the code below at all.
if (v.size() != (the_size + 1))
{
cerr << "Unable to allocate memory via push_back()" << endl;
}


Gavin Deane


if I would like to restrict size of some objs (ex. vector<string>)
what can I do?
rewrite the allocator? is there any shortcut?

Mar 31 '06 #6

P: n/a

Gavin Deane wrote:
Alex Vinokur wrote:
By the way, is this check correct?
vector<Foo> v;
// Stuff

size_t the_size = v.size();
Foo f;
v.push_back (f);
The standard behaviour when any memory allocation fails is to throw a
std::bad_alloc exception, so if push_back needs to allocate but is
unable, you don't get to the code below at all.


Green Hills' Extended Embedded C++ compiler enables the user to cancel
exception handling. In this case, can the code below work? Does anybody
have such experience?
if (v.size() != (the_size + 1))
{
cerr << "Unable to allocate memory via push_back()" << endl;
}


[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 31 '06 #7

P: n/a

Alex Vinokur wrote:
Green Hills' Extended Embedded C++ compiler enables the user to cancel
exception handling. In this case, can the code below work? Does anybody
have such experience?
if (v.size() != (the_size + 1))
{
cerr << "Unable to allocate memory via push_back()" << endl;
}


The standard containers do not provide any guarantees for your case.
Create and use your own containers that do.

Mar 31 '06 #8

P: n/a
Alex Vinokur wrote:
"Alex Vinokur" <al****@users.sourceforge.net> wrote in message
[snip]
2. But how to manage memory allocation in containers for embedded
systems?

For instance,
vector<Foo> v;
Foo f;
v.push_back(f);

push_back() returns no values.

So, how can we know (except try-catch) that it is impossible to
allocate the requested memory in push_back()?

[snip]

By the way, is this check correct?

vector<Foo> v;
// Stuff

.... snip ...

If you are worried about memory and bloat, why are you using C++ in
the first place?
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 1 '06 #9

P: n/a
Alex Vinokur wrote:
The memory allocation issue in embedded systems is usually critical..

How can one manage that?

1. Via 'new'
char* p = new (nothrow) char [SOME_SIZE];
if (p == 0)
{
// He we know that it is impossible to allocate the requested memory
// We can do something relevant.
}
2. But how to manage memory allocation in containers for embedded
systems?

For instance,
vector<Foo> v;
Foo f;
v.push_back(f);

push_back() returns no values.

So, how can we know (except try-catch) that it is impossible to
allocate the requested memory in push_back()?

The only save way if you disable exceptions is to provide your own
allocator and assert.

--
Ian Collins.
Apr 1 '06 #10

P: n/a
Maxim Yegorushkin wrote:
Alex Vinokur wrote:
Green Hills' Extended Embedded C++ compiler enables the user to cancel
exception handling. In this case, can the code below work? Does anybody
have such experience?

> if (v.size() != (the_size + 1))
> {
> cerr << "Unable to allocate memory via push_back()" << endl;
> }


The standard containers do not provide any guarantees for your case.
Create and use your own containers that do.


Or own allocators for STL containers?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Apr 1 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.