STL容器和管理嵌入式系统中的内存分配 [英] STL containers and managing memory allocation in embedded systems

查看:59
本文介绍了STL容器和管理嵌入式系统中的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嵌入式系统中的内存分配问题通常很关键..


如何管理?


1.通过''新的''

char * p = new(nothrow)char [SOME_SIZE];

if(p == 0)

{

//他知道我们无法分配所需的内存

//我们可以做一些相关的事情。

}

2.但是如何在嵌入式

系统的容器中管理内存分配?


例如,

vector< FOO> v;

Foo f;

v.push_back(f);


push_back()不返回值。


那么,我们如何才能知道(除了try-catch)

在push_back()中分配所请求的内存?

-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

解决方案

Alex:


当你说''vector< Foo> v;''实际上你在说:

vector< Foo,allocator< Foo> > v;

因为矢量模板的默认第二个参数

定义:


模板<类T,类分配器= allocator< T> >

类向量.....


和成员allocate()分配器...抛出异常

bad_alloc如果存储不可用。这个函数使用运算符

new(size_t)..."


希望这个帮助。


在comp.arch.embedded中,Alex Vinokur< al **** @ users.sourceforge.net>写道:

嵌入式系统中的内存分配问题通常很关键..


它通常超出临界值 - 它'这段时间不可接受。一个

的嵌入式系统没有人会抱怨,如果一个操作严格内存分配失败,那么内部分配就会失败。所以最好不要去那里




如果你不能允许异常处理,你通常不能

允许C ++样式的动态内存处理。它就这么简单。

如何管理它?


主要是不这样做。

2.但是如何在嵌入式系统的容器中管理内存分配?




主要是不使用它们。


-

Hans-Bernhard Broeker(br ***** @ physik.rwth-aachen.de)

即使所有积雪都被烧毁,灰烬仍会存在。




Alex Vinokur <人**** @ users.sourceforge.net>在留言新闻中写道:11 ********************** @ t31g2000cwb.googlegr oups.com ...

[snip] < blockquote class =post_quotes> 2.但是如何在嵌入式
系统的容器中管理内存分配?

例如,
vector< Foo> v;
Foo f;
v.push_back(f);

push_back()不返回值。

那么,我们怎么知道(除了try-catch)在push_back()中分配所请求的内存是不可能的?



[snip]


顺便说一句,这个检查是否正确?

vector< Foo> v;

//东西


size_t the_size = v.size();

Foo f;

v.push_back(f);


if(v.size()!=(the_size + 1))

{

cerr<< 无法通过push_back()分配内存 <<结束;

}

-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


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

解决方案

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


这篇关于STL容器和管理嵌入式系统中的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆