Teaching new tricks to an old dog (C++ -->Ada)

  • Thread starter Turamnvia Suouriviaskimatta
  • Start date
P

Peter Koch Larsen

Alex R. Mosteo said:
Nope. And you can control in portable Ada how big the stack for a given
task must be.

But you do not know the size of the object so how does that help you setting
the stack-size?

/Peter
 
D

Dmitry A. Kazakov

Regarding Storage_Error exception, C++'s one is bad_alloc, which is
guaranteed to *never fail* in case of memory starvation.

Even if exception object is 100GB large?
About stack. Strictly speaking, C++ standard does not define any stack
apart from a stack container , but "automatic storage" and "dynamic
storage".

It is common though the first to be called "stack" and the second "heap"
or "free store".

It is an implementation's/platform's job to provide stack and heap.

Usually the stack is limited in comparison to the heap, and it sounds
strange to me that in Ada there is a large stack. My guess is that you
are using the heap for data storage implicitly(?) and you think you are
using the stack.

Stack here means LIFO. It is not necessarily the machine stack, though in
most cases it will be. I think there is no need to explain why LIFO
allocation / deallocation strategy is in order of magnitude more efficient
than heap? Neither should I explain why local to task (thread) memory is
better than the global heap in any concurrent program and especially in
ones running on multi-processor machines...
Are you familiar with "Resource Acquisition is Initialisation" (RAII)
technique? This is supported by C++ and used by all standard library
containers.

= smart pointers. Yes. It is a widely used technique to regain the safety
lost while switching from stack to heap. It adds additional performance /
space penalty to what heap already has.
 
A

Alex R. Mosteo

Peter said:
But you do not know the size of the object so how does that help you setting
the stack-size?

I'm not sure what you want to imply here. The thing is that while in
stack terrain you're free of all dynamic memory additional
hazards/overheads, which in certain realtime environments is seen as
desirable. As parent said, stacks tend to be smaller than heaps, and in
Ada you can control that without going non-portable.

If we're talking of unknown sized objects, either a stack or a heap can
be too small. ?
 
R

Robert A Duff

Hans Malherbe said:
I would like to believe that people like you would point out the
nonsense preventing people like me from consuming it.

A fair comment.

I *did* post a few notes in this thread trying to set some things
straight. And I might post a few more, if I have time. But how do you
know whether to trust what *I* say over what anyone else says? ;-)

If you really want to know about these languages that are being
discussed (Ada and C++) nothing beats reading a textbook or two.

- Bob
 
I

Ioannis Vranos

Dmitry said:
Even if exception object is 100GB large?


I am talking about the exception class bad_alloc whatever the sizes of
other objects (even exceptions) are. It is guaranteed that there is
always free memory for bad_alloc.

Stack here means LIFO. It is not necessarily the machine stack, though in
most cases it will be. I think there is no need to explain why LIFO
allocation / deallocation strategy is in order of magnitude more efficient
than heap? Neither should I explain why local to task (thread) memory is
better than the global heap in any concurrent program and especially in
ones running on multi-processor machines...


Although I know the general difference between stack and heap (the last
storing things in a messy way), I have no knowledge about them in terms
of program's space (that is how the heap can be rearranged in the
background), so it is better someone else to answer this.

However in general what I know is that the additional time-cost of
dynamic storage in comparison to the automatic storage can be described
as the additional cost of new and delete calls.

= smart pointers. Yes. It is a widely used technique to regain the safety
lost while switching from stack to heap. It adds additional performance /
space penalty to what heap already has.


Smart pointers, as also containers, but even for resources other than
memory.
 
D

Dmitry A. Kazakov

I believed the object might have to be returned? Read the post once again.
If this is so, you can not allocate it on the stack without having to copy
when you return the object. Surely the copy must be more expensive than the
allocation?

The compiler can use multiple stacks so that arguments and the result might
be allocated on different stacks.
Ordinary heap-based speed is not so slow and if you need to get the last
cpu-cycle away, you can simply use an arena for your allocation.

Yes, some Ada compilers would probably use discontiguous object
representation and arenas for variable parts. The difference is that it is
still not the global heap. And it is controlled by the compiler which
exactly knows the scope of objects.
Also you will of course use a smart pointer in the situation above. A C++
programmer with respect for himself only rarely uses new without putting the
result in some kind of smart pointer. Personally, I do not remember when i
did that last.

I'd be happy if all C++ programmers would be ready and willing to
compensate deficiencies of the language through more careful and thoughtful
design. Actually, we are doing a lot of developing in C++, and due to my
job responsibilities time to time I have to review C++ code...
 
T

T Beck

Adrien said:
Ada is also used in particular rockets called missiles, and its job
inside is just to make them fall... but fall gracefully.

Would that happen to include the now-infamous Patriot Missles (the ones
that couldn't hit a barn after they'd been on for a while due to a
bug?)

--T Beck
 
J

Jerry Coffin

Pascal said:
Just plain wrong, there is data (a PHD) see
http://www.adaic.com/whyada/ada-vs-c/cada_art.html

Perhaps you should reread this, paying paritcular attention to the
dates involved. According to the paper, they started switching from C
to Ada around 1986. C wasn't standardized until 1989, and (as you'd
expect) it was some time after that before most compilers implemented
the standard language.

By 1990 or so when compilers conforming reasonably closely with the C
standard became available, it appears likely that essentially all new
development was being done in Ada. Under the circumstances, it would be
rather surprising if the C code was ever rewritten into standard C.

In short, this is not a comparison to the C language as it exists
today, or has existed in well over a decade.

He also mentions C++ in passing, but (again, based on the timing) he
was clearly looking at C++ when it was still in an embryonic stage.
Worse, the understanding and use of the language were, if anything,
well behind the development of the language itself. The language itself
was clearly inferior to todays, but it's use was inferior by an even
larger margin.

If anything, based on my own experience with standard C vs.
pre-standard C, I'd say his study shows rather the opposite of what you
think it does. Standard C was enough of an improvement over
pre-standard C that it would be rather surprising if standard C didn't
beat Ada in most areas studied (the primary exception being code
reuse).

It's true that Ada has also been updated in the meantime, but the
changes in Ada have been _drastically_ smaller than those in C or C++.
Ada 83 already had generics, already had type-checking on the same
general order as that introduced into C with function prototypes, etc.
With Ada 95 you can probably expect a fairly substantialy improvement
in reuse, but only rather minor improvements elsewhere.

By contrast, comparing modern C++ to the pre-standard C shows _large_
improvements in nearly all areas. This is due in part to the changes in
the language itself, but perhaps even more so to improved understanding
of how to use the language.
 
J

jayessay

Ioannis Vranos said:
Two interesting links:

http://home.earthlink.net/~joshwalker1/writing/TemplateMetaprogramming.html

http://www.codeproject.com/cpp/crc_meta.asp


Again, it is a whole field of its own with many books about it. It is
about turning *any* run-time operation to compile time (and the output
can be the compiler messages).

Sigh. If you really want to know how potent this sort of angle can
really be, you should spend some time looking at the real thing. Here
are a couple vastly more interesting links:

http://www.gigamonkeys.com/book/macros-standard-control-constructs.html

http://www.paulgraham.com/onlisp.html


/Jon
 
J

Jerry Coffin

Adrien said:
but still ARPA-net was created by the United States Defense Advanced
Research Project Agency, part of... the DoD !

That was exactly my point: it started out as a DoD-funded project. The
question is about what happened then -- how willing the DoD was to open
the project to the public at large. As it was, the project was done
with DoD funding, but was really done at and by universities. While it
is openly acknowledged to have fulfilled its original intent extremely
well, nearly every choice in its design and implementation was about as
UN-military as possible. Despite this, it took 20+ years before it was
really open to the general public.

Had even ONE element in the design or implementation fit even slightly
more closely with the military mind-set, I doubt it would have been
opened up to the public yet, or possibly ever.
I don't remember running UNIX (implemented in C) was cheap at the
time ARPA-net was born ! also, one of the major Ada compiler is
available for free, so why would it be so expensive ?

Prices for computing in general were much higher at the time, but UNIX
was cheaper than most.

It seems difficult to believe that anybody would believe or even imply
that the cost of the compiler determines the cost of the final product.

Even ignoring that for the moment, you're using cirular logic -- you're
assuming the 'net would be built with GNAT, ignoring the fact that the
free availability of GNAT is largely a _result_ of the 'net's
existence.
 
J

Jerry Coffin

Robert A Duff wrote:

[ ... ]
If you really want to know about these languages that are being
discussed (Ada and C++) nothing beats reading a textbook or two.

Much as I hate to further my (undoubtedy bad) reputation for being
disagreeable, I still have to disagree.

If you follow-up the reading with some real use, it beats reading alone
by a wide margin, at least IME.
 
D

Dr. Adrian Wrigley

Ada features I would love to have in C++:
* "new types" and sub-ranges

Agree strongly. (with compiler-generated checking!)
* runtime template instantiation (I think being able to instantiate
generics dynamically is the coolest thing!)

This would be nice!
* subprograms inside subprograms. I think it would be great for use
with the STL (i.e., using a local function with std::foreach)

Agree strongly.
* packages. namespaces are nice, but packages make modularity of very
large system a lot easier. Especially now with "use type"

Vital! C++ and C suffer a lot from not enforcing encapsulation,
allowing headers to break stuff etc. Better control of scope
and visibility. Better separation of interface and implementation
* representation specifications!!!!
Agree.

C++ features I would to have in Ada:
* Implicit instantiation. Yeah, I know, it can be unsafe, but that is
one thing I really like about C++: the ability to automate repetitive
things.

Sometimes useful but is it more than "syntactic sugar"?
* actually having a class object encapulating the data and methods.

Yes. It might help if packages were first class objects instead
* reference (in out) types in functions (without resorting to
access types)

procedures usually suffice. The "Rosen Trick" works in most
cases. I'd like to be able to identify functions as "pure",
like in VHDL, and allow the compiler to discard or memoize
calls to functions marked as pure.
* meta-templates. very neat stuff. ugly, but neat.

I think I prefer Ada generics. I have yet to see how Meta-templates
really contribute to a program design.
* The STL!!! (though I hear something similar is coming?)

Yes. Agree strongly.
-------------------------------------------------------------
My list of Ada features I would love to have in C++ adds:

Concurrency. Tasks. Protected objects.
Distributed programs. Persistent variables.

Eliminate large swathes of the C++ standard which leaves
the semantics up to the compiler!

Named parameter association. Proper parameter modes
"out" and "in out" modes

clarity over arrays. C++ gives you choice of pointers
C arrays, vectors. This is one example of where the
'C' facilities were too limited, and C++ added
alternatives, rather fhan fixing what existed in C.
(and char *, char arrays, string etc)

Strong(er) typing, better enumerations, fixed point types,
modular types *and* non-modular types. Basic type attributes.

More robust, more readable, less terse syntax, particularly
for complex templates (really!)

Portability without resorting to preprocessor directives
and conditional compilation
--------------------------------------------------------------
My list of C++ features I would love to have in Ada adds:

simple and direct interfacing to C/C++ header files - a *big*
drawback of Ada :(

template specialization

extra parameters with "new"
--------------------------------------------------------------
But what of features not present in either?

Introspection

Dynamic compilation/interpretation

parallel execution constructs (see Cilk, Occam's "par")
coroutines

configurations (from VHDL)

associative arrays (from Perl)

What else?
 
P

Peter Koch Larsen

Dmitry A. Kazakov said:
The compiler can use multiple stacks so that arguments and the result
might
be allocated on different stacks.

Okay. If the Ada concept of a "stack" is the same as my hardware-oriented
one, I doubt i would have much trust to the performance of a such a type of
programming.
Yes, some Ada compilers would probably use discontiguous object
representation and arenas for variable parts. The difference is that it is
still not the global heap. And it is controlled by the compiler which
exactly knows the scope of objects.


I'd be happy if all C++ programmers would be ready and willing to
compensate deficiencies of the language through more careful and
thoughtful
design. Actually, we are doing a lot of developing in C++, and due to my
job responsibilities time to time I have to review C++ code...

If I were to review code with a regular use of new and delete, I would send
the code right back - and the responsible programmer on a course - hoping
for a substantial improvement in style.

/Peter
 
P

Pascal Obry

Ioannis Vranos said:
The bottom line is that as a language itself it has less abilities than C++,
the framework being another case.

Right. I agree. Don't get my message wrong. I never said that Java was
better. I just stated the original goal as expressed by Sun.

Pascal.

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
 
P

Peter Koch Larsen

"Dr. Adrian Wrigley" <[email protected]> skrev i en
meddelelse
Agree strongly. (with compiler-generated checking!)

This can be implemented mostly with templates.
This would be nice!

How would you do that without "compiling on the fly"?
Agree strongly.

This will be part of the next standard, I hope. Still i do not believe it to
be esential for use.
Vital! C++ and C suffer a lot from not enforcing encapsulation,
allowing headers to break stuff etc. Better control of scope
and visibility. Better separation of interface and implementation


Agree.

Why? What is the purpose - if not to restrict portability?

Sometimes useful but is it more than "syntactic sugar"?


Yes. It might help if packages were first class objects instead


procedures usually suffice. The "Rosen Trick" works in most
cases. I'd like to be able to identify functions as "pure",
like in VHDL, and allow the compiler to discard or memoize
calls to functions marked as pure.


I think I prefer Ada generics. I have yet to see how Meta-templates
really contribute to a program design.


Yes. Agree strongly.
-------------------------------------------------------------
My list of Ada features I would love to have in C++ adds:

Concurrency. Tasks. Protected objects.
Distributed programs. Persistent variables.

I doubt that this should be part of the standard. What is needed is
specification of low-level features in e.g. multiprocessor environments.
Eliminate large swathes of the C++ standard which leaves
the semantics up to the compiler!

Most of these "implementation defined" parts are there for portability. To
many definitions will make it more difficult to port C++ to other platforms.
Named parameter association. Proper parameter modes
"out" and "in out" modes

I do not see the purpose of the out-parameter. Why not simply return the
value(s)?
As for named parameter associations, these can again be implemented in a
library.
clarity over arrays. C++ gives you choice of pointers
C arrays, vectors. This is one example of where the
'C' facilities were too limited, and C++ added
alternatives, rather fhan fixing what existed in C.
(and char *, char arrays, string etc)

For C++ portability from C was (and is) an important issue. Thus there is no
question about remocing e.g. pointer-arithmetic and decaying of arrays to
pointers. This simply can not happen. Also some of these features are needed
for an efficient implementation of low-level classes (e.g. std::vector).
What you can do is not use these features. And it is just a matter of
Strong(er) typing, better enumerations, fixed point types,
modular types *and* non-modular types. Basic type attributes.

There are some bad decisions in the type system - specifically i hate the
automatic conversions from double to integral value. But again - this is
something you have to live with.
More robust, more readable, less terse syntax, particularly
for complex templates (really!)

Right. The template stuff could possibly have been more elegant.
Portability without resorting to preprocessor directives
and conditional compilation

I am not sure i follow you here. In what way do you want portability? Are
you thinking of e.g. portability between an X-windows system and Windows? Or
portability of lower-level constructs such as multithreading or networking?
I wonder how Ada does this stuff.
--------------------------------------------------------------
My list of C++ features I would love to have in Ada adds:

simple and direct interfacing to C/C++ header files - a *big*
drawback of Ada :(

template specialization

extra parameters with "new"
--------------------------------------------------------------
But what of features not present in either?

Introspection

Dynamic compilation/interpretation

parallel execution constructs (see Cilk, Occam's "par")
coroutines

configurations (from VHDL)

associative arrays (from Perl)

I thought Ada supported generic programming. Isn't it then just a question
of creating a library?
What else?
/Peter
 
?

=?ISO-8859-1?Q?Falk_Tannh=E4user?=

Dr. Adrian Wrigley said:
But what of features not present in either? [...]
associative arrays (from Perl)

Wouldn't that be std::map in C++?

Falk
 
R

REH

Dr. Adrian Wrigley said:
Sometimes useful but is it more than "syntactic sugar"?
No, probably not but unlike a lot of my colleagues, I like syntactic sugar.
That's why I love being able to overload operators in both languages. I can
add to the language, and make my new constructs look like they are part of
the language.
I think I prefer Ada generics. I have yet to see how Meta-templates
really contribute to a program design.
The reason I prefer template (their power, not looks) is their ability to
"store" information using the type system. For example, recently on a
(personal) project, I had a set of object I wanted to be able to act upon
(like std::foreach does) and filter for various criteria. Using templates I
created a function to traverse the objects. This function takes two
template parameters, the operation to be perfomed, and the filter. I have
template classes for filters and operators for the boolean operators to
combine and build more advanced filters on-the-fly without creating functors
each time. Something like:

traverse_objects(objects, operation, filter1(stuff) && filter2(other-stuff)
|| !filter3());

This has to be done with templates and class types because the boolean
operations can't be done now, but "deferred" until called by operation. As
an example, this is a simplicist version of the "and" filter:

template<class A, class B>
struct and_filter {
and_filter(const A& a, const B& b) : m_a(a), m_b(b) {}
bool operator() (Object& o) {return m_a(o) && m_b(o);}
A m_a;
B m_b;
};

template<class A, class B>
inline and_filter<A, B> operator&& (const A& a, const B& b)
{
return and_filter<A, B>(a, b);
}

I know its less safe, but I like that I can use "anything" for the template
parameters that "fit" the syntax (i.e., using functions or class objects
that have the () operator defined).
Named parameter association. Proper parameter modes
"out" and "in out" modes
I would love have named parameters too. That way I would not have to
"agonize" over the "priority" of my default parameters (i.e., if there are
10 and I have to change the 10th from its default, I have to define the
first 9).
clarity over arrays. C++ gives you choice of pointers
C arrays, vectors. This is one example of where the
'C' facilities were too limited, and C++ added
alternatives, rather fhan fixing what existed in C.
(and char *, char arrays, string etc)

Yes, arrays are first-class objects!
Strong(er) typing, better enumerations, fixed point types,
modular types *and* non-modular types. Basic type attributes.
I love that Ada enumeration don't pollute the global namespace and allow
fully qualified names. I wish I could do enum_name::element in C++ without
wrapping the enum declaration in a namespace.
More robust, more readable, less terse syntax, particularly
for complex templates (really!)
Completely agree!!
associative arrays (from Perl)
I assume you want more than that offered by std::map?
 
R

REH

Peter Koch Larsen said:
How would you do that without "compiling on the fly"?
Ada generics can do this without compiling on the fly. I think its a nice
feature.
Why? What is the purpose - if not to restrict portability?
Actually rep. specs. enhance portability by explicitly defining the actual
sizes of types, the internal layout of records, etc. The only aspect (I
believe) that is implementation specific, it bit ordering (though, didn't
they add pragmas for this?)
 
I

Ioannis Vranos

I do not know what you mean exactly by that, however one can define his
own types rather than having everything as built-in.


C++ provides general purpose facilities with which one can build his own
special purpose libraries, rather than providing special purpose
facilities as built in.


Consider std::complex as a complex type, rather than a built in complex
type.

This would be nice!


From my (perhaps limited) experience of .NET where run-time generics
are also available to C++ (with the upcoming C++/CLI, .NET 2 and VC++
2005 - currently Beta), run-time generics are more limited than
compile-time templates. Also since they are run-time they are less
efficient.
Vital! C++ and C suffer a lot from not enforcing encapsulation,
allowing headers to break stuff etc. Better control of scope
and visibility. Better separation of interface and implementation


May I assume that packages are a form of precompiled dynamic-link
libraries, like dlls in Windows?

My list of Ada features I would love to have in C++ adds:

Concurrency.


Will be available in C++0x. Today is platform-specific, but there is
also a standard about C++, OpenMP (see below).


Tasks. Protected objects.
Distributed programs. Persistent variables.

Eliminate large swathes of the C++ standard which leaves
the semantics up to the compiler!

Named parameter association. Proper parameter modes
"out" and "in out" modes

clarity over arrays. C++ gives you choice of pointers
C arrays, vectors. This is one example of where the
'C' facilities were too limited, and C++ added
alternatives, rather fhan fixing what existed in C.
(and char *, char arrays, string etc)

Strong(er) typing, better enumerations, fixed point types,
modular types *and* non-modular types. Basic type attributes.

More robust, more readable, less terse syntax, particularly
for complex templates (really!)


I assume you are right, concerning newcomers, but for me Ada's syntax is
the bizarre one (apart from the Pascal "subset"). :)

Portability without resorting to preprocessor directives
and conditional compilation


These are used essentially for system-oriented code.

--------------------------------------------------------------
My list of C++ features I would love to have in Ada adds:

simple and direct interfacing to C/C++ header files - a *big*
drawback of Ada :(

template specialization

extra parameters with "new"
--------------------------------------------------------------
But what of features not present in either?

Introspection

Dynamic compilation/interpretation


This is against the systems programming bias of C++. Also there are C++
interpreters out there. Here is a nice, small one:

http://home.mweb.co.za/sd/sdonovan/underc.html

parallel execution constructs (see Cilk, Occam's "par")
coroutines


Today there is OpenMP:

http://www.openmp.org/drupal

configurations (from VHDL)

associative arrays (from Perl)


I am not sure if you mean this, however standard C++ library provides
map and multimap.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,058
Members
47,668
Latest member
SamiraShac

Latest Threads

Top