473,785 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ STL in embedded systems

Are there any restrictions/problems for use of C++ STL in development
in embedded systems?
In particular:
* Does STL require too much space/memory?
* Is 'implementation of STL algorithms/methods' reenterable/reentrant?
* What is the cost to provide continuity of vectors in memory?
Any other problems?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jan 3 '06 #1
49 8973
"Alex Vinokur" <al****@users.s ourceforge.net> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Are there any restrictions/problems for use of C++ STL in development
in embedded systems?
Perhaps. That depends upon the implementation and
the platform (and perhaps other criteria, such as
the nature of your application).
In particular:
* Does STL require too much space/memory?
How much is too much? Also note that each specific
standard library implementation may consume different
amounts of resources.
* Is 'implementation of STL algorithms/methods' reenterable/reentrant?
That depends upon the implementation.
* What is the cost to provide continuity of vectors in memory?
Not sure what you mean my 'continuity'. However, the elements
of a std::vector object are required to be contiguous in memory.
Any other problems?


Perhaps.

-Mike
Jan 3 '06 #2
Alex Vinokur wrote:
Are there any restrictions/problems for use of C++ STL in development
in embedded systems?
I think I am confused by the use of the second 'in' here. Do you mean
'for' or 'on'? If you develop elsewhere and then install your program
to run on an embedded system, that's "for". If you try to compile your
program so that the compiler is running right there, it would be "on"
an embedded system. Which one are you interested in?
In particular:
* Does STL require too much space/memory?
You need to define "too much". However, generally speaking, the library
design is such that the objects do not introduce enormous overhead.

Since most of the Standard library is templatised, the compiler does need
more memory for processing the code that does use templates than code that
does not. So, the difference usually exists during compile-time and not
in run-time.
* Is 'implementation of STL algorithms/methods' reenterable/reentrant?
Generally, probably yes. However, the Standard says that non-reentrancy
is implementation-defined (17.4.4.5). Look in the documentation for your
compiler.
* What is the cost to provide continuity of vectors in memory?
I don't understand the question. Vectors are contiguous. Each vector
object requires some memory for itself and then some overhead exists for
dynamic array allocation. That's essentially the cost.
Any other problems?


I have no answer here, but I am sure others who program for embedded
systems, do.

V
Jan 3 '06 #3

Victor Bazarov wrote:
Alex Vinokur wrote:
Are there any restrictions/problems for use of C++ STL in development
in embedded systems?
I think I am confused by the use of the second 'in' here. Do you mean
'for' or 'on'? If you develop elsewhere and then install your program
to run on an embedded system, that's "for".


It should be 'for'.
Thanks.
If you try to compile your
program so that the compiler is running right there, it would be "on"
an embedded system. Which one are you interested in?
In particular:
* Does STL require too much space/memory?
You need to define "too much".


I would like to know of experience of people working with STL for
embedded systems.
Are there hard/insoluble problems related to issue?
However, generally speaking, the library
design is such that the objects do not introduce enormous overhead.

Since most of the Standard library is templatised, the compiler does need
more memory for processing the code that does use templates than code that
does not. So, the difference usually exists during compile-time and not
in run-time.
* Is 'implementation of STL algorithms/methods' reenterable/reentrant?


Generally, probably yes. However, the Standard says that non-reentrancy
is implementation-defined (17.4.4.5). Look in the documentation for your
compiler.
* What is the cost to provide continuity of vectors in memory?


I don't understand the question. Vectors are contiguous. Each vector
object requires some memory for itself and then some overhead exists for
dynamic array allocation. That's essentially the cost.


Let v be of vector<in> type.
For instance, at the moment v.size() = 10000.
Now we do the following thing:
for (int i; i < 20000; i++) v.push_back(i);

Must 'v' be reallocated/copied to provide its continuity for v.size()
== 30000.

P.S. By the way, does similar continuity problem exist for ordinary
array?
[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jan 3 '06 #4
Alex Vinokur wrote:
[...]
Let v be of vector<in> type.
For instance, at the moment v.size() = 10000.
What's v.capacity() ?
Now we do the following thing:
for (int i; i < 20000; i++) v.push_back(i);

Must 'v' be reallocated/copied to provide its continuity for v.size()
== 30000.
Unknown. Depends on the capacity.
P.S. By the way, does similar continuity problem exist for ordinary
array?


Ordinary array has a fixed size, and no 'push_back' member function, so,
no, such problem does not exist there.

V
Jan 3 '06 #5
Alex Vinokur wrote:
Victor Bazarov wrote:
Alex Vinokur wrote: [snip]
* Does STL require too much space/memory?
You need to define "too much".


I would like to know of experience of people working with STL for
embedded systems.
Are there hard/insoluble problems related to issue?


Presumably you mean, "Are there hard/insoluble problems related to
[this] issue [i.e., too much memory being required]?"

I have used STL on several different processors on embedded systems,
and I have had no problems related to memory usage that wouldn't have
also been incurred by statically allocated or new/malloc allocated
memory. However, problems related to std::vector certainly could be
incurred on an embedded (or other non-embedded!) system, especially
because of its exponential memory allocation strategy. Some embedded
systems have tighter memory requirements than others, so it will
entirely depend on your requirements. However, see below on how to
mitigate potential memory issues.

[snip]
* What is the cost to provide continuity of vectors in memory?


I don't understand the question. Vectors are contiguous. Each vector
object requires some memory for itself and then some overhead exists for
dynamic array allocation. That's essentially the cost.


Let v be of vector<in> type.
For instance, at the moment v.size() = 10000.
Now we do the following thing:
for (int i; i < 20000; i++) v.push_back(i);

Must 'v' be reallocated/copied to provide its continuity for v.size()
== 30000.


std::vector allocates memory exponentially to "amortize" the growing
cost over time. Consequently, when you do your first push_back, vector
will double its size (i.e., allocate a second buffer of double the
size, copy the existing data into it, and then delete the first
buffer), and when you exceed the new size, it will double it again to
40000. So this will (1) waste memory if the vector doesn't grow any
more beyond that, (2) take up some extra memory while you're doing the
copy, and (3) possibly fragment memory beyond repair.

If you know how big it will grow (or even approximately how big), you
can use std::vector<>:: reserve() to set the initial capacity, which
will help minimize copying. If you do not know how big the size will
grow and std::vector's memory allocation scheme doesn't work well for
your system, you should probably not use them.
P.S. By the way, does similar continuity problem exist for ordinary
array?


Yes, and it must be done by hand, which is more error prone. However,
you do then control the memory allocation strategy and could make it
something other than exponential if your system demands it (e.g., add
some predefined amount to the current size, allocate that mem, and then
copy the old into the new; or use some sort of "chunk allocator", where
non-contiguous chunks are tracked by a linked list; etc.).

Cheers! --M

Jan 3 '06 #6
mlimber wrote:
Alex Vinokur wrote:
Victor Bazarov wrote:
Alex Vinokur wrote:
[snip] Let v be of vector<in> type.
For instance, at the moment v.size() = 10000.
Now we do the following thing:
for (int i; i < 20000; i++) v.push_back(i);

Must 'v' be reallocated/copied to provide its continuity for v.size()
== 30000.


std::vector allocates memory exponentially to "amortize" the growing
cost over time. Consequently, when you do your first push_back, vector
will double its size (i.e., allocate a second buffer of double the
size, copy the existing data into it, and then delete the first
buffer), and when you exceed the new size, it will double it again to
40000.

[snip]

Note: This discussion assumed that v.size() == v.capacity().

Cheers! --M

Jan 3 '06 #7
>> Are there any restrictions/problems for use of C++ STL in development
in embedded systems?
You might want to think about exception handling.
* Does STL require too much space/memory?
for a small system, yes. But if you need the functionality, it's probably
better than writing it yourself.
Any other problems?


I ran into trouble some years back with microsoft's VC++ implementation
not being reentrant. <string> reference counts were seeing race
conditions. STLPort seemed to fix the problem. But replacing shared
<string> with (const char*) really helped.


--
mac the naïf
Jan 3 '06 #8
Alex Colvin wrote:
Are there any restrictions/problems for use of C++ STL in development
in embedded systems?


You might want to think about exception handling.

[snip]

Good point. On some of my embedded projects, the compiler didn't
support exceptions at all, and so, neither did the STL. Of course, such
an implementation is not fully conformant to the standard, but it was
still very useful. And some parts of the STL don't throw exceptions
anyway (e.g., std::auto_ptr, many algorithms).

Cheers! --M

Jan 3 '06 #9
>> >> Are there any restrictions/problems for use of C++ STL in development
>> in embedded systems?
You might want to think about exception handling.

[snip]

Good point. On some of my embedded projects, the compiler didn't
support exceptions at all, and so, neither did the STL. Of course, such
an implementation is not fully conformant to the standard, but it was
still very useful. And some parts of the STL don't throw exceptions
anyway (e.g., std::auto_ptr, many algorithms).


I'm with the Embedded C++ folks <http://www.caravan.net/ec2plus/> on this.
I think that the minimum "contract" for a method call is that it return.
But your needs may differ.

As to conformance, I don't know that I've run across a fully conformant
compiler/runtime yet. As implementations like GCC approach conformance,
their libraries have to keep changing.

The result seems to be that C++ has become unstable. You can't link
against a library unless you're using the same compiler version. It's best
if you have all the libraries in source.
--
mac the naïf
Jan 3 '06 #10

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

Similar topics

12
3102
by: Brandon | last post by:
Java seems to have taken off as the platform and language of choice for many embedded devices. Would it be feasible for Python(perhaps running on an embedded version of Linux) to act in such a capacity. Most of my experience with Python has been with Unix-type scripting tasks and using it when it is an applications built in scripting, but I know some people try to use to build larger complex applications. Is the Python interpreter portable...
12
2983
by: Bill Hanna | last post by:
C is inadequate for embedded systems for the following reasons: 1) Direct addressing of memory mapped I/O is not supported simply and easily. You have to find work-arounds that are compiler dependent. You have to create macros , use casting and use indirect addressing (pointers) to read or write to memory mapped I/O. 2) C does not support real time interrupts. The interrupt vectoring is compiler dependent.
10
2521
by: Alex Vinokur | last post by:
The memory allocation issue in embedded systems is usually critical.. How can one manage that? 1. Via 'new' char* p = new (nothrow) char ; if (p == 0) { // He we know that it is impossible to allocate the requested memory // We can do something relevant.
20
22541
by: Jack | last post by:
Is there a Python packaging that is specifically for embedded systems? ie, very small and configurable so the user gets to select what modules to install? For Linux-based embedded systems in particular? I'm thinking of running it on the Linksys's Linux-based open source router WRT54G. It has 4MB flash and 16MB RAM. I think another model has 16MB flash. Any possibilities of running Python on these systems?
0
2271
by: YellowFin Announcements | last post by:
Whitepaper: "Yellowfin Reporting" enables Embedded Business Intelligence -------------------------------------------------------------------------------- Embedded reports are a standard requirement of most applications. But users are increasingly demanding more sophisticated reporting from applications - seeking such features as custom report design, ad hoc report creation and analytics. Developers that want to embed business...
2
1310
by: pdeivanayagam | last post by:
hi i hav cmpltd my engg.i am thnkng of doing an embedded systems. course...can i knw abt prospect of embedded systems?wut will be the oppurtunities available in embedded systems?i want some infm reg this.can anyone guide me reg this? regards deivanayagam
14
1897
by: Martin Wells | last post by:
When I want to store a number, I use "unsigned". I go with unsigned because it's the natural type for the system, and so should be the fastest. However, there are 8-Bit microcontrollers out there that do 8-Bit arithmetic faster than 16-Bit arithmetic, and so on these systems char is faster than int. Standarising C in such a way that int is at least 16-Bit, has this made C both slow and memory-hungry for embedded systems programming?
20
2054
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
There are a few guarantees I exploit in the C Standard. For instance, I might write (unsigned)-1 to get the maximum value for an unsigned integer. Also, I might rely on things such as: memset(data,-1,sizeof data)
30
4302
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
9645
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
10329
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...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.