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 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.
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.
"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
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
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?
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
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.
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/>
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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..?
|
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.
|
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...
|
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.
| |
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.
|
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?
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |