473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL containers and managing memory allocation in embedded systems

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
10 2480
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
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

"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

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

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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ares Lagae | last post by:
Hello, I am trying to create a container the stl way, and I have a couple of questions. The code of the container in question can be found at ...
3
by: dsptechie | last post by:
I wanted to how exactly malloc() function works. I came to know that malloc() allocates memory only in powers of 2. i.e if asked for say 17 bytes , in the process, it allocates 32 bytes and returns...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
8
by: Cuthbert | last post by:
Hi folks, This question is a little deep into memory management of microprocessor. How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top? For example, the...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
11
by: jimxoch | last post by:
Hi list, Most STL containers are storing their data on the heap. (although some std::string implementations are notable exceptions) Of course, using the heap as storage increases flexibility and...
5
by: vishnu | last post by:
Hi there, I am embedding python 2.5 on embedded system running on RTOS where I had strict memory constraints. As python is a huge malloc intensive application, I observed huge memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.