Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why ! C++

Reply
Thread Tools

Why ! C++

 
 
Kaz Kylheku
Guest
Posts: n/a
 
      03-06-2007
On Mar 5, 12:03 pm, SasQ <(E-Mail Removed)> wrote:
> So how it's been done? I'm very curious about that,
> because the plain old C doesn't need any runtime
> support AFAIK.


Of course C needs run-time support. What run-time support is needed
depends on the compiler. Not every expression, declaration or
statement is necesarily turned into pure inline machine code.

On some RISC-like processors, for instance, some math operations, even
simple integer ones like divisions, may necessitate the use of a
library routine. This explains the various __mul* and __div* type
functions in the GNU compilers's ``libgcc'' library. If you're using
GCC to write a kernel, then you still have to supply that library.

How about structure initialization and assignment?

Code like

some_struct x = { 0 };
/* ... */
y = x;

may actually translate into machine code which calls memset and
memcpy. If you don't supply these, the thing won't link due to
unresolved references.

C++ construction and destruction has its API also. One possibility is
for the comiler to generate a special hidden construction and
destruction hook functions. Each object file with C++ objects in it
has such functions.

The embedded kernel's startup code would have to have a routine which
would iterates over these functions and calls them.

Now how do you get a list of these functions? One possible way is to
make use of a linker feature known as named sections (also known as
segments). If you've done assembly language programming, you might
have run into conventional sections like .text or .data.

Anyway, for each constructor hook function, a variable holding a
pointer to that function (by way of a symbolic reference that will be
resolved at link time) is also emitted, and that variable is placed
into a section. When the objects are linked, the linker will merge all
of the objects that belong to the same section name into one
contiguous segment of memory in the target object. The routine then
simply has to iterate from some symbolic address marking the section
start to another symbol which resolves to the section end. It knows
that the section contains nothing but pointers to the constructor
functions which are all equally wide, and so essentially it is working
with an array of pointers set up by the linker. And that is how you
can complete the run-time support for C++ global construction and
destruction. The compiler does one part, a linker script defines the
sections to do the second part, and some loop does the rest.

Exceptions? Similar concept. There will be some exception handling ABI
expected by the generated code, which the system has to complete. That
ABI will have some function for unwinding the stack frame by frame,
querying each frame for useful information such as the entry point for
clean-up cod for that frame, etc. The ABI has to map to the
concurrency model also. Do you allow exceptions within interrupt
service routines? If so, the unwinder has to know that it's in an
interrupt stack, and how to recognize that it's hit the top of it,
etc. Moreover, the interrupt may have gone off while the interrupted
task context was processing an exception already, so the two can't get
mixed up, etc.


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-06-2007
SasQ wrote:

> Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisał(a):
>
>> There are some systems written in C++. I am working on
>> an embeddded one that is comprised of a couple of
>> thousand lines of C++ plus a hundred lines of assembler,
>> plus 0 (zero) lines of C.

>
> How did you make it?


I don't know, but I did it too. The smallest controller I used with C++ is
an 8 bit one with 1k of program memory and 64 bytes of data memory. Of
course there are some sacrifices you have to make on such a small system.
Most of the C++ standard library for instance wouldn't make much sense on
such a system, because it heavily depends on dynamic memory and the code
would be too big.

> How can C++ work in an embedded system?


Just like C can.

> I thaught C++ needs some additional implementation code,
> which make base for C++ language facilities before it
> call main() function.


So?

> I mean the things like reserving static memory, calling constructors for
> static objects, exception handling mechanisms etc. And I thaught that
> new and delete operators have to be implemented using some system
> API/kernel calls to some system memory manager, and cannot work in "blank"
> environment without some system/kernel, which may do the memory management
> for new/delete implementation code.


What makes you think new/delete must use a kernel to manage memory? It can
just as well have its own memory manager.

> So how it's been done? I'm very curious about that, because the plain old
> C doesn't need any runtime support AFAIK.


Well, it also has to initialize static variables. And if you have malloc and
free, implementing new and delete is trivial. Exception handling is
currently not supported by the implementation I use, but that's mostly
because nobody bothered to implement it yet. I guess on the small
controller I mentioned above, the overhead would be way too big, but there
are controllers of the same architecture with up to 256k of program memory
and (through an external memory interface) up to 64k of data space, and
this would definitely be enough for exception handling to be reasonable.

 
Reply With Quote
 
 
 
 
lakshmi
Guest
Posts: n/a
 
      03-07-2007
> > Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisał(a):
>
> >> There are some systems written in C++. I am working on
> >> an embeddded one that is comprised of a couple of
> >> thousand lines of C++ plus a hundred lines of assembler,
> >> plus 0 (zero) lines of C.


Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2007
lakshmi wrote:
>>>Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisał(a):

>>
>>>>There are some systems written in C++. I am working on
>>>>an embeddded one that is comprised of a couple of
>>>>thousand lines of C++ plus a hundred lines of assembler,
>>>>plus 0 (zero) lines of C.

>
>
> Embedded systems written in c++ ....Did you ever felt that,
> you should have used C instead of C++ ?....
>

No, why should I? C++ is a very good fit in the embedded space.

--
Ian Collins.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-07-2007
lakshmi wrote:

>> > Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisał(a):

>>
>> >> There are some systems written in C++. I am working on
>> >> an embeddded one that is comprised of a couple of
>> >> thousand lines of C++ plus a hundred lines of assembler,
>> >> plus 0 (zero) lines of C.

>
> Embedded systems written in c++ ....Did you ever felt that,
> you should have used C instead of C++ ?....


I started with C, but I felt I should have used C++ instead of C

 
Reply With Quote
 
Zara
Guest
Posts: n/a
 
      03-07-2007
On 6 Mar 2007 23:18:13 -0800, "lakshmi" <(E-Mail Removed)>
wrote:

>> > Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisa?(a):

>>
>> >> There are some systems written in C++. I am working on
>> >> an embeddded one that is comprised of a couple of
>> >> thousand lines of C++ plus a hundred lines of assembler,
>> >> plus 0 (zero) lines of C.

>
>Embedded systems written in c++ ....Did you ever felt that,
>you should have used C instead of C++ ?....



I started in C, felt that I shoudl have started in C++, trashed all
previous work, restarted in C++. Now my progress goes faster.

Zara
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 2:18 am, "lakshmi" <(E-Mail Removed)> wrote:
> Embedded systems written in c++ ....Did you ever felt that,
> you should have used C instead of C++ ?....


We also use C++ on our embedded systems running TI DSPs. (BTW, the TI
compiler is quite compliant with the Standard, but it doesn't ship
with the full standard library, particularly the STL. One can easily
add STLport, though.)

Cheers! --M

 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      03-08-2007
On 6 Mar 2007 23:18:13 -0800, "lakshmi" <(E-Mail Removed)> wrote:

>> > Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisa?(a):

>>
>> >> There are some systems written in C++. I am working on
>> >> an embeddded one that is comprised of a couple of
>> >> thousand lines of C++ plus a hundred lines of assembler,
>> >> plus 0 (zero) lines of C.

>
>Embedded systems written in c++ ....Did you ever felt that,
>you should have used C instead of C++ ?....


I've been writing safety-critical embedded code in C++ for years. Wouldn't
trade it for C, ever.

-dr
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments