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 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.
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.
"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
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.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/>
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.
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 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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |