473,796 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2521
Alex:

When you say 'vector<Foo> v;' actually you are saying:
vector<Foo,allo cator<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.embed ded Alex Vinokur <al****@users.s ourceforge.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.s ourceforge.net> wrote in message news:11******** **************@ t31g2000cwb.goo glegroups.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.s ourceforge.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.c om, 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/googlegroupsrep ly/>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1518
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 http://www.cs.kuleuven.ac.be/~ares/tmp/array_2_hpp.html It is a 2d array that dynamically allocates its storage. It cannot be resized. It offers element access methods for both sequential access and 2d element access.
3
5222
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 17 bytes so 15 bytes are wasted... Is this true...If anybody knows the algorithm details of malloc() plase share the same. Is this one of the reason why malloc() is not generally preferred in embedded systems..?
7
1859
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 code that basically acts as a server and that will be running for weeks or months and probably even longer, memory management is a topic that is quite crucial.
8
5115
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
8
2006
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 "Top-Bottom" is arranging memory resource from higher address to lower address.
24
19097
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
53
26407
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 variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
11
1931
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 allows the handling of much bigger amounts of data. However, there are cases where this turns out to be inefficient for at least two reasons: A) Allocations and deallocations on heap are much slower. B) Memory access on stack is more cache...
5
3348
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 fragmentation in my system which is leading to out of memory after running few scripts. So I decided to re-initialise the python with out restarting the whole python. I tried to use Py_Finalise() after completion of each script , then call Py_Initialise as...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.