 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frank Astier Guest
|
Posted: Tue Nov 23, 2004 6:52 pm Post subject: Real-time C++? |
|
|
I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
that you can in C pretty much whatever you can do in C++. Which got me
thinking. There are no templates in C to start with, and I believe
templates are an important engineering tool in C++.
Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
Thanks,
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Nov 24, 2004 7:29 pm Post subject: Re: Real-time C++? |
|
|
Frank Astier wrote:
| Quote: | Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
|
When you see this line
}
and are using C, you know that this will never trigger any code to be
called[1]. Under C++, this might call the dtor of objects to be invoked,
which in turn might release resources to a global heap which first has
to
be locked. If that happens[2], it might take an infinite amount of time
for
the lock to become available, even worse things might happen in an
interrupt handler or a signal handler.
However, this is not something that makes C++ unsuitable, it just adds
one
more pitfall to the unweary, it is thus just something to be aware of.
The
reason why many prefer C for some tasks is that you have complete
control
over what is happening. C++ removes some of this control, but as a
compensation you also can delegate some responsibility to implicitly
called
code.
A totally different reason to choose C over C++ is that C compilers
have
been ported more widely. Many small systems do have a C compiler, while
only few have a C++ compiler. You need to see if that applies to your
case.
Uli
[1]: I'll leave aside the fact that after a block a function might
return or
that the stackpointer might be adjusted for the vars that just went out
of
scope.
[2]: Note that the same problem applies when e.g. free() is called at
that
position, it is just that free() is visible/explicit while a dtor call
is
invisible/implicit.
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sergei Organov Guest
|
Posted: Wed Nov 24, 2004 8:26 pm Post subject: Re: Real-time C++? |
|
|
[email]fastier (AT) yahoo (DOT) com[/email] (Frank Astier) writes:
| Quote: | I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements...
|
Well, as an evidence of their ignorance, you may wish to point them to
the eCos, <http://sources.redhat.com/ecos/>, an open source RTOS that is
both implemented in C++ and supports C++ applications nicely.
--
Sergei.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Branimir Maksimovic Guest
|
Posted: Thu Nov 25, 2004 12:00 am Post subject: Re: Real-time C++? |
|
|
[email]fastier (AT) yahoo (DOT) com[/email] (Frank Astier) wrote in message news:<40da7b0d.0411230910.20b8fa3d (AT) posting (DOT) google.com>...
| Quote: | I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
that you can in C pretty much whatever you can do in C++. Which got me
thinking. There are no templates in C to start with, and I believe
templates are an important engineering tool in C++.
Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
|
Any compiled language (even interpreted if interpreter respects
requirements) program can be suited to meet "real time" requirements.
Are you trolling?
Greetings, Bane.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Malte Clasen Guest
|
Posted: Thu Nov 25, 2004 12:05 am Post subject: Re: Real-time C++? |
|
|
Frank Astier wrote:
| Quote: | Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
|
That depends a bit on your kind of real-time requirements and your usage
of C++ features. I've written a software synthesizer in C++ and there
was no problem at all, neither with general execution speed nor
responsiveness. But this application had no "fatal" limits: It would be
annoying if the sound skipped a bit now and then, but you can detect
this, the musician can increase the buffer size and everything's fine.
If you're working on something important, you should pay close attention
to the standard library features you use: The complexity guarantees are
often given in ammortized time. A push_back() on a vector should take
constant time, but it may take linear time once in a while to extend the
capacity. Since this happens more seldom the larger the vector gets
(size increases by a factor of about 1.5), the overall cost of this
outlier is negligible - for usual applications. Real-time apps can choke
on this very badly, and it's hard to spot during tests.
So you should have a close look on all features you're going to use to
find these problems and circumvent them. In case of the vector just try
to reserve the required size during initialization and you're done.
Malte
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Nov 25, 2004 12:08 am Post subject: Re: Real-time C++? |
|
|
[email]fastier (AT) yahoo (DOT) com[/email] (Frank Astier) wrote in message
news:<40da7b0d.0411230910.20b8fa3d (AT) posting (DOT) google.com>...
| Quote: | I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
that you can in C pretty much whatever you can do in C++.
|
That's trivially true, since both languages are Turing complete. It's
not a question of what you can do, but how difficult and how expensive
it is to do it.
I'd used C extensively before learning C++. When I wrote programs in C,
I typically defined a struct, and a mess of functions to manipulate that
struct. And crossed my fingers that no one would access the internals
of the struct except through my functions.
So the real situation is: C++ offers improved verification of some of
the basic concepts of software engineering. (It offers a lot more, but
that will do for starters.) And you can do everything you can do in C
in C++. So formally, there is never a motivation for not using C++
(although there will be contexts where you might want to avoid certain
features). Practically, of course -- if there is no C++ compiler
available for the targetted platform, that is a good motivation not to
use C++. Other languages, such as Ada, also offer good support for
basic software engineering, and might be reasonable alternatives. But
not C.
| Quote: | Which got me thinking. There are no templates in C to start with, and
I believe templates are an important engineering tool in C++.
|
A lot depends on what you are doing. For very, very small applications,
you might find no use for templates. In many applications, you will
find no use for virtual functions, and in some, external constraints
will mean no exceptions, or even no new/delete (and no malloc/free, if
you are using C).
But hey, if the application doesn't call for some particular feature,
you're not required to use it. And it's hard to imagine an application
where none of C++'s features (compared to C) would be useful.
| Quote: | Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
|
A lot depends on what you mean by "real-time". I know of a lot of
real-time applications written in C++. But for very limited processors,
the real question might end up being the availability of a compiler.
And if there is no C++ compiler available for the platform, then THAT is
a very valid reason for not using C++. Compared to C, I'd say it is
about the only valid reason I can think of.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Thu Nov 25, 2004 2:04 am Post subject: Re: Real-time C++? |
|
|
"Frank Astier" <fastier (AT) yahoo (DOT) com> wrote
| Quote: | I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
that you can in C pretty much whatever you can do in C++.
|
Sure, and you could assembly has the same property too.
But ability to do something alone it's not a reason to prefer a thing over
the other, there must be some benefit.
The fact that C++ front end compilers are capable to create C code from any
C++ source does not mean humans could do the same thing as well and as
correctly. :-)
Practice shows in C it is almost impossible to write a correct program
without having an excessive review system. Having no automation will leave
you with all kinds of resource leaks and buffer overrun-like extremely
stupid bugs -- just look around, the world is still fighting those after
billions of $$ spent on eliminaiton and more suffered as damage. (Mike
Howard pointed some place where just switching to use a some string class
was enough to mitigate buffer overruns...)
As to real-time stuff, I see absolutely no difference between C and C++, and
no benefits of C whatsoever. Comes to mind -- the top argument is
simmetric: you can also do anything in C++ that you can do in C. Just C++
adds a plenty of cool tools to your service.
| Quote: | Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
|
If you learn any such gotcha do us a favour and tell what it is. If you
want you can picjk up a subset of C++ that still includes most of its power,
and be able to create identical binary to a C one -- but with musc better
formed source, and one that withstand changes ways better. (Thinking hard
exceptions looks like the only stuff that can introduce an unpredictable
factor. It's not at all hard to not use them, also many compilers offer to
remove exception support using a switch.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Robert Kindred Guest
|
Posted: Thu Nov 25, 2004 2:08 am Post subject: Re: Real-time C++? |
|
|
"Frank Astier" <fastier (AT) yahoo (DOT) com> wrote
| Quote: | I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
that you can in C pretty much whatever you can do in C++. Which got me
thinking. There are no templates in C to start with, and I believe
templates are an important engineering tool in C++.
Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
|
We are doing real-time programming on QNX with gcc 3.1.1, and I am glad we
did not stick with C.
For instance, I have a paradigm I stole from ACE where I encapsulate a
thread into a C++ object. This makes things quite clean and clear-cut for
me.
Robert Kindred
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Astier Guest
|
Posted: Thu Nov 25, 2004 8:51 am Post subject: Re: Real-time C++? |
|
|
Thanks Ulrich, that makes a lot of sense, and it is pretty useful
material for me. But it sounds to me that besides the compiler
availability issue, the developper skill level is going to be the main
factor in this discussion: being aware of enough pitfalls, there is no
reason you couldn't get good performance in C++.
Thanks,
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Thu Nov 25, 2004 8:56 am Post subject: Re: Real-time C++? |
|
|
"Ulrich Eckhardt" <doomster (AT) knuut (DOT) de> wrote
| Quote: | Anyhow, I was wondering what the experience out there was for C++ and
real-time requirements: is it just a matter of writing reasonable code
and getting the right compiler, or is there some gotcha that's going
to render C++ useless?
When you see this line
}
and are using C, you know that this will never trigger any code to be
called[1].
|
Yeah.
| Quote: | Under C++, this might call the dtor of objects to be invoked,
which in turn might release resources to a global heap which first has
to be locked.
|
Err, this is not the truth -- dtors are not called on a whim or by rolling a
d6, their invocation is absolutely deretministic, as what they do.
So at } there will be a call to dtor of an object you created just a few
lines above, and its dtor is there to do exactly what you need.
| Quote: | If that happens[2], it might take an infinite amount of time for
the lock to become available, even worse things might happen in an
interrupt handler or a signal handler.
|
And how that is different from a C alternative? There you'll have az
explicit free() at the pont, and all the happening is the same. If release
is inappropriate at the moment, then not }, not the dtor call is the one to
blame, but using the kind of resource itself -- I'd wager its *creation*
already introduced the problem.
| Quote: | [2]: Note that the same problem applies when e.g. free() is called at
that
position, it is just that free() is visible/explicit while a dtor call is
invisible/implicit.
|
If that visibility rocks it's possible to write a proper comment at places,
of even going the C way in the parts of program it is appropriate. While
natural use is possible in the rest of the project.
| Quote: | However, this is not something that makes C++ unsuitable, it just adds
one more pitfall to the unweary, it is thus just something to be aware
of. |
In practice I saw a zillion cases where lack of those automatic dtor calls
were the pitfall -- meybe not at the first release of the code, but injected
later by adding an extre branc here, and an early return there -- and some
time later the whole thing consumed the resources due to leaks.
Or left a lock unpaired for that matter, a counter not decremented...
| Quote: | The
reason why many prefer C for some tasks is that you have complete
control over what is happening.
|
Is that a difference? If you don't want a dtor called don't use an
automatic local object with one, but create with placement-new instead.
You can avoid that dtor call if you absolutely want to. But as I observed
dtors is the main reason people want C++ over other languages, and the most
handy and missed function.
| Quote: | C++ removes some of this control, but as a
compensation you also can delegate some responsibility to implicitly
called code.
A totally different reason to choose C over C++ is that C compilers
have
been ported more widely.
|
Yep, that was a reason indeed a decade ago, nowadays it must be a really
abandoned platform if you can't compile a C++ compiler there having a C
compiler, or use a proper front-end.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Wesp Guest
|
Posted: Fri Nov 26, 2004 1:05 pm Post subject: Re: Real-time C++? |
|
|
Frank Astier <fastier (AT) yahoo (DOT) com> wrote:
| Quote: | I'm coming back to the C/C++ world after a lull of a few years doing
Java, and I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
|
They're wrong. C++ may not be ideal, but C is even less so.
The only point where I'm a bit unsure is dynamic allocation within the
RT loop.
I suspect that a for a typical implementation, the realtime guarantee no
longer holds as soon as new/delete is called. Note that this is an
assumption, please correct me if I'm wrong!
Consider the following generic Euler solver:
template< typename M > struct euler_solver {
euler_solver( M const& m , double const& dt )
: m( m ) , dt( dt ) , x( m.default_state() ) {}
// Advance model state by one time step.
void step()
{ x = x + dt * m.derivative( x ) ; }
private:
M& m ; // The model. m.derivative( x ) must return the
// state derivative at x.
double const dt ; // Time increment.
M::state_type x ; // Model state.
} ;
Clear and readable generic code, isn't it?
Do you see the gotcha? Everything's fine as long as M::state_type is
e.g. just a plain double or std::complex<>. But it might also be a
vector whose dimension is known only at runtime. In this case,
M::state_type's will---directly or indirectly---allocate memory.
It's the temporaries in the expression
x + dt * m.derivative( x )
which trigger this behaviour.
Typically, three temporaries will be generated: The return value of
derivatives(), the result of the * operator and the result of the +
operator.
First possible solution: Expression templates. Some std::valarray<>
implementations even do use them, I understand, but who wants to
implement ET's for all their state structures?
Second possible solution: Formulate step() differently.
void step() {
dx = m.derivative( x ) ;
dx *= dt ;
x += dx ;
}
with a
M::state_type dx ;
suitably initialized (dimensioned) in the constructor.
To me, this variant is definitely less readable than the first one, plus
it gets even more so once you're using higher-order solvers...
Third possible solution: Fixed-size vectors. In practice, this will
almost always be acceptable, but somehow I just don't like fixed-size
vectors... :-)
Fourth possible solution: Dynamic allocation on the stack
(variable-sized arrays and containers based on them). Unfortunately,
the language doesn't support this (why? C99 does!), so we're out of luck
here.
Fifth possible solution: Customized allocators. I don't have any
experience whith these. Anybody using them in a similar context?
At the moment I'm using solution #2, accepting the reduction in
readability. This is also the approach Simulink/Realtime workshop take,
but they do it in C.
How do others cope with these issues? Am I chasing a non-problem?
Should I perhaps just not worry at all about dynamic allocation in a
real-time loop?
Cheers
-Gerhard
--
Gerhard Wesp o o Tel.: +41 (0) 43 5347636
Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/
CH-8045 Zuerich _/ See homepage for email address!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Wesp Guest
|
Posted: Fri Nov 26, 2004 1:08 pm Post subject: Re: Real-time C++? |
|
|
Balog Pal <pasa (AT) lib (DOT) hu> wrote:
| Quote: | Under C++, this might call the dtor of objects to be invoked,
which in turn might release resources to a global heap which first has
to be locked.
Err, this is not the truth -- dtors are not called on a whim or by rolling a
d6, their invocation is absolutely deretministic, as what they do.
|
That's what Ulrich wrote. He wrote that the destructor is invoked at
the line where you see a '}'.
The problem is that in almost all modern C++ implementations, new/delete
is *necessarily* implemented in a thread-safe manner which implies a
potential nondeterministic time delay. Correct me if I'm wrong.
| Quote: | In practice I saw a zillion cases where lack of those automatic dtor calls
were the pitfall -- meybe not at the first release of the code, but injected
|
Fully ACK.
| Quote: | Yep, that was a reason indeed a decade ago, nowadays it must be a really
abandoned platform if you can't compile a C++ compiler there having a C
compiler, or use a proper front-end.
|
I talked to QNX about 2 years ago (that's less than a decade and at
the time there was *no* C++ compiler available for QNX. (For those not
familiar with the realtime community, QNX is definitely *not* an
abandoned platform.) Well, there was g++ 2.95.something, but this was
already *way* outdated even then, and as we all know, it's far from
compliant. Glad to read in this thread that apparently they learned by
now.
Cheers
-Gerhard
--
Gerhard Wesp o o Tel.: +41 (0) 43 5347636
Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/
CH-8045 Zuerich _/ See homepage for email address!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Nov 26, 2004 4:26 pm Post subject: Re: Real-time C++? |
|
|
In article <d6652001.0411240252.41ddb118 (AT) posting (DOT) google.com>,
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes
| Quote: | A lot depends on what you mean by "real-time". I know of a lot of
real-time applications written in C++. But for very limited processors,
the real question might end up being the availability of a compiler.
And if there is no C++ compiler available for the platform, then THAT is
a very valid reason for not using C++. Compared to C, I'd say it is
about the only valid reason I can think of.
|
And because writing a high quality C compiler is orders of magnitude
simpler (and cheaper) than writing one for C++, those providing tool
chains for specialist limited processors may find that the market does
not justify the development and maintenance of a C++ compiler.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Fri Nov 26, 2004 4:27 pm Post subject: Re: Real-time C++? |
|
|
Ulrich Eckhardt <doomster (AT) knuut (DOT) de> wrote
[...]
| Quote: | When you see this line
}
and are using C, you know that this will never trigger any code to be
called[1].
|
What you don't know is whether any operation should have been
performed just before the brace but was forgotten. In well written C++
this is less likely to happen.
| Quote: | Under C++, this might call the dtor of objects to be invoked,
which in turn might release resources to a global heap which first has
to be locked. If that happens[2], it might take an infinite amount of time
for the lock to become available, even worse things might happen in an
interrupt handler or a signal handler.
However, this is not something that makes C++ unsuitable, it just adds
one more pitfall to the unweary, it is thus just something to be aware of.
|
It is debatable whether it is better to have to remember all the
things you need to do by hand or to have some of them done for you
(not invisibly, just from another place in the code).
| Quote: | The reason why many prefer C for some tasks is that you have complete
control over what is happening. C++ removes some of this control, but as a
compensation you also can delegate some responsibility to implicitly
called code.
|
I wouldn't say that C++ removes control, it just moves it elsewhere.
All in all I find this argument more a question of superstition,
rather than of direct experience.
| Quote: | A totally different reason to choose C over C++ is that C compilers
have been ported more widely. Many small systems do have a C compiler,
while only few have a C++ compiler. You need to see if that applies to your
case.
|
I would also expect it to be easier to find a well optimizing compiler
for C than for C++.
[...]
| Quote: | [2]: Note that the same problem applies when e.g. free() is called at
that position, it is just that free() is visible/explicit while a dtor call
is invisible/implicit.
|
In the Python community they say that explicit is better than
implicit, to which I'd add ...unless you forget it.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marc-Antoine Ruel Guest
|
Posted: Fri Nov 26, 2004 11:53 pm Post subject: Re: Real-time C++? |
|
|
"Gerhard Wesp" <gwesp (AT) cosy (DOT) sbg.ac.at> a écrit dans le message de news:
co4bng$ia9$1 (AT) esel (DOT) cosy.sbg.ac.at...
| Quote: | Frank Astier <fastier (AT) yahoo (DOT) com> wrote:
I've just met people who contend that C++ is not well suited
to real-time requirements... They prefer C, and they further maintain
They're wrong. C++ may not be ideal, but C is even less so.
|
I agree with you. I am a NT kernel programmer, so I don't do real-time
coding but I still have design decisions to make according to the time each
task takes. Having converted kernel C projects to C++ (or C+ like I sometime
call it), when you do code in C, you can do much more stupids things than in
C++.
But C++ have much more side effects than C. You have to know the pit falls
and not touch certain features that can induce non-deterministic
functionality or hidden code like RTTI, temporaries, virtual functions and
bases and exceptions.
Having your own run-time library helps in not having functionality that
could hurt run-time behavior or performance. But it's not the language
fault.
| Quote: | The only point where I'm a bit unsure is dynamic allocation within the
RT loop.
(snip)
void step() { x = x + dt * m.derivative( x ) ; }
(snip)
Third possible solution: Fixed-size vectors. In practice, this will
almost always be acceptable, but somehow I just don't like fixed-size
vectors...
|
Personnally, I would have written:
void step() { x += dt * m.derivative( x ) ; }
which removes the need of a temporary.
If you do Euler solver with dynamic arrays on a real-time system with
temporaries, that's your problem (or feature . This is called a design. A
language can't help you doing wrong. You *have to* take in account the
various side effects of your code, what ever the language you use.
When I code in C++, I think about the side effects and do "defensive
programming". It's normal since it is NOT normal programming. I don't
program kernel mode programs like user mode programs or in C#! :)
| Quote: | Fourth possible solution: Dynamic allocation on the stack
(variable-sized arrays and containers based on them). Unfortunately,
the language doesn't support this (why? C99 does!), so we're out of luck
here.
Fifth possible solution: Customized allocators. I don't have any
experience whith these. Anybody using them in a similar context?
(snip)
Cheers
-Gerhard
|
I can talk about that since I did implement my version of custom allocators.
I prefer using new & delete rather than malloc & free (or the like) since it
1) permits to specify the way each kind of objects are allocated (and not
only function arguments)
2) gives me constructors and destructors
3) gives me placement new that help me "re-use" existing objects
4) can take arguments to pass it to a lower-level allocator.
For instance, my operator new takes an option to determine the type of
memory that will be used by the system for the allocation of the object, in
my case, paged memory or non paged memory. I also can specify a tag for the
memory allocation. I could specify mono-threaded too, if it existed. My
allocations looks like:
MyClass * p = new(NonPagedPool, ' GAT') MyClass( ctr params );
so I don't loose any functionality and can modify the behavior of each
object type.
But the most usefull side-effects are the use of destructors in debug that
help me assert the validity of certain behavior.
Marc-Antoine Ruel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|