473,439 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,439 software developers and data experts.

Why We Use C Than C++...

Hi, Please help me in this regard...
All the kernel level programs are written in C... (i.e: Open Source
LINUX)... Why are they not using C++... I personally feel that C++ is
more easy to code than C... When i searched in our encyclopedia (i.e
Google), i came up with several answers, telling "C is much faster than
C++ and most kernal prgms uses only C" ... But why is it so...? Why
they use C for these OS based programs.. Please Help me with detailed
explanation...

Cheers,
Balaji Jayaraman...

Jan 12 '06
109 4474

"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:43***********************@news.wanadoo.fr...
2) dwarf2 based tables, where the compiler describes in detail each
stack frame so that the runtime is able to figure out where it should
jump. This method is fast when not throwing exceptions since there
is no overhead, but it costs a LOT in RAM space since those tables
must be generated for each function, and that is surely not cheap in
space requirements. The tables need to be there even for functions that
do not use exceptions at all.


On a typical virtual memory system, executables are not loaded into memory,
they are memory-mapped into the virtual address space. Only when a specific
address is accessed does that particular page get swapped into real memory
(called demand paged virtual memory). Thus, although the exception handling
tables can consume significant address space, they don't consume RAM unless
exceptions are actually happening.

To enhance this effect, the exception handling tables are typically grouped
together and separate from ordinary static data.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Jan 17 '06 #101

"Ian Collins" <ia******@hotmail.com> wrote

There are many cases where C++ is a good choice for procedural code, the
ability to manage the lifetime of dynamic memory and locks is the one I
have found most useful in kernel space.

It might be the least bad choice, if you can impose discipline on your
developers, and you need some feature not provided by C.
Jan 17 '06 #102
Logan Shaw <ls**********@austin.rr.com> wrote:
For example, C++ allows you to do this:

int *i = new int[100];

There is no reason the performance of this should be any worse than
the C equivalent:

int *i = (int *) malloc (100 * sizeof (int));

But, which one is easier to write?
Wouldn't most experienced C developers write this instead?

int *i = malloc(100 * sizeof *i);

Casting the return value of malloc() in C (not C++!) is bad form.

Using "sizeof type" instead of "sizeof *var" is also bad form.
Also, will the compiler catch
the error for you if you accidentally change the second one to this?

double *d = (double *) malloc (100 * sizeof (int));


This potential problem evaporates if you use the proper form.
Jan 17 '06 #103
Malcolm wrote:
"Ian Collins" <ia******@hotmail.com> wrote
There are many cases where C++ is a good choice for procedural code, the
ability to manage the lifetime of dynamic memory and locks is the one I
have found most useful in kernel space.


It might be the least bad choice, if you can impose discipline on your
developers, and you need some feature not provided by C.

The compiler can impose discipline though options. The environment will
back this up by simply not loading the application if the rules are not
followed (lack of C++ run time in kernel space).

--
Ian Collins.
Jan 17 '06 #104
After discussing a lot, i gathered some important points given by our
friends... Below are some points, why we use C rather than C++...

When doing kernels, you'll likely be interfacing with lots of low-level
functions to handle hardware interrupts and C is the most portable
language (across different compilers) for doing this.
From http://ibis.nott.ac.uk/guidelines/ch4/chap4-4.1.html
High-level authoring systems allow developers to implement
sophisticated software products quickly. Such systems have many
advantages, therefore, in projects where there is a premium on
productivity. However, these advantages are won at some cost: high
level programs operate less directly on the hardware they control than
low level programs.
The best high level authoring tools can, of course, be extended at low
level to serve particular needs (eg with the aid of compiled languages
like C). It is important, therefore, when setting up a project team, to
consider the merits of adding low level programming skills to the team
even though much of the development will be at a higher level. The
presence of in house skills of this kind can be extremely
cost-effective.

Low-level languages are closer to the hardware than are high-level
programming languages, which are closer to human languages.

From http://www.cplusplus.com/doc/informa...scription.html
When choosing a programming language to make a project, many different
considerations can be taken. First, one must decide what is known as
the level of the programming language. The level determines how near to
the hardware the programming language is. In the lower level languages,
instructions are written thinking directly on interfacing with
hardware, while in "high level" ones a more abstract or conceptual code
is written.

Generally, high level code is more portable, that means it can work in
more different machines with a smaller number of modifications, whereas
a low level language is limited by the peculiarides of the hardware
which it was written for. Nevertheless, the advantage of low level code
is that it is usually faster due to the fact that it is indeed written
taking advantage of the possibilities of a specific machine.

A higher or lower level of programming is to be chosen for a specific
project depending on the type of program that is being developed. For
example, when a hardware driver is developed for an operating system
obviously a very low level is used for programming. While when big
applications are developed usually a higher level is chosen, or a
combination of critic parts written in low level languages and others
in higher ones.

(So from the above statement its clear that Object Oriented Programming
is generally used for application type of projects)

When i searched for why the systems like Unix, Linux, Windows have been
written in C, i came up with an answer that its just because C was
considered as a 'portable assembler', allowing to drive the hardware on
one hand, and to provide a sufficient level of abstraction to build
portable interfaces (API).
Some minor reasons for using C from
http://www.linuxdevices.com/eljonlin...07/4870s1.html

*

C++'s virtues are expensive. Advanced OOP features, such as
templates and the practice of using classes in the place of primitives,
to name two examples, cause unacceptable code bloat.

*

A C++ compiler may generate many routines for one function
(templates) or create routines where no function explicitly appeared
(constructors, casts, etc.). There is generally a one-to-one
relationship between a function in C code and the resulting
machine-code routine. It's easier to optimize what you can see than
what you must infer.

*

Virtual methods and polymorphism complicate runtime linking and
require many relocations. This slows C++ application launch time
considerably. C applications are both simple to link and amenable to
lazy linking, so they load quickly. (For details, see Waldo Bastian's
paper ``Making C++ Ready for the Desktop",
http://www.suse.de/~bastian/Export/linking.txt.)

*

Each class with virtual methods has an associated vtable array,
which adds memory overhead.

*

C++'s tighter type-checking makes it difficult to write the
space-conscious code reuse common to C applications (think: void *).

*

The small, simple code demanded of embedded projects provides
maintainability. There is no reason to assume OOP will further simplify
such systems.

*

GUIs may not have a simple solution in a rigorous OOP model.

*

It's easy to get carried away and start doing OOP for OOP's sake.
The One True Object Model may describe a problem perfectly, but it
comes at the cost of excessive code.

Carefully written C code can be much faster than C++ code,
especially on embedded hardware. GTK+'s hand-crafted object system
offers much better spatial locality than C++'s more numerous and
distributed constructors. Our device has a tiny cache, so locality is
an especially important performance consideration.

From the above said points, I feel that 'C' Code is more close to

kernel and harware programs where memory management, speed are
considered and where as 'C++' Code is for easy use for programmers in
developing Application level programming...

Jan 18 '06 #105
so********@gmail.com wrote:
After discussing a lot, i gathered some important points given by our
friends... Below are some points, why we use C rather than C++...

When doing kernels, you'll likely be interfacing with lots of low-level
functions to handle hardware interrupts and C is the most portable
language (across different compilers) for doing this.
As is the C subset of C++.
Some minor reasons for using C from
http://www.linuxdevices.com/eljonlin...07/4870s1.html

*

C++'s virtues are expensive. Advanced OOP features, such as
templates and the practice of using classes in the place of primitives,
to name two examples, cause unacceptable code bloat.
To use a colloquialism popular in these parts, bollocks. Sorry about
using such a term, but the above is pure FUD. *

A C++ compiler may generate many routines for one function
(templates) or create routines where no function explicitly appeared
(constructors, casts, etc.). There is generally a one-to-one
relationship between a function in C code and the resulting
machine-code routine. It's easier to optimize what you can see than
what you must infer.
Again, FUD. OK, maybe not so with inexperienced C++ programmers, but
when you know what you are doing, this isn't an issue.
*

Virtual methods and polymorphism complicate runtime linking and
require many relocations. This slows C++ application launch time
considerably. C applications are both simple to link and amenable to
lazy linking, so they load quickly. (For details, see Waldo Bastian's
paper ``Making C++ Ready for the Desktop",
http://www.suse.de/~bastian/Export/linking.txt.)
Not an issue in kernel space, where everything tends to use static linking.
*

Each class with virtual methods has an associated vtable array,
which adds memory overhead.

* True, cost/benefit trade off.
C++'s tighter type-checking makes it difficult to write the
space-conscious code reuse common to C applications (think: void *).

* That's why its there! (think: bugs).
The small, simple code demanded of embedded projects provides
maintainability. There is no reason to assume OOP will further simplify
such systems.

* C++ doesn't mandate OOP.
GUIs may not have a simple solution in a rigorous OOP model.

* Kernels don't have GIUs.
It's easy to get carried away and start doing OOP for OOP's sake.
The One True Object Model may describe a problem perfectly, but it
comes at the cost of excessive code.
That's a bold assumption, any facts to back it up?
Carefully written C code can be much faster than C++ code,
especially on embedded hardware.
Nope, FUD.

GTK+'s hand-crafted object system offers much better spatial locality than C++'s more numerous and
distributed constructors. Our device has a tiny cache, so locality is
an especially important performance consideration.
Again, Kernels don't have GIUs.
From the above said points, I feel that 'C' Code is more close to

kernel and harware programs where memory management, speed are
considered and where as 'C++' Code is for easy use for programmers in
developing Application level programming...

I'd have to disagree there, with this first assumption anyway. Both
languages are fine for kernel and hardware programs, I know I've used both.

The bigger issue is your developer skills base. An experienced team of
C developers will produce a much better result than an inexperienced
team of C++ developers. C++ kernel/embedded developers are a rare
commodity.

--
Ian Collins.
Jan 18 '06 #106

Ian Collins wrote:
sonugeetha wrote:
From the above said points, I feel that 'C' Code is more close to kernel and harware programs where memory management, speed are
considered and where as 'C++' Code is for easy use for programmers in
developing Application level programming...

I'd have to disagree there, with this first assumption anyway. Both
languages are fine for kernel and hardware programs, I know I've used both.


yes collins, i accept with you as you say you have used both... but
what i told above is what i felt.. Maybe because that i'm familiar more
in C than C++...

The bigger issue is your developer skills base. An experienced team of
C developers will produce a much better result than an inexperienced
team of C++ developers. C++ kernel/embedded developers are a rare
commodity.

yes collins... i accept with you... Everything depends on the way we
implement and how well we know... :)

---
Balaji Jayaraman

Jan 18 '06 #107
Ed Jensen wrote:

Wouldn't most experienced C developers write this instead?

int *i = malloc(100 * sizeof *i);

I would say no to that. Certainly most of the experienced C developers
that read this newsgroup would, but I've never seen it used by an
outsider.

Brian
Jan 18 '06 #108
so********@gmail.com wrote:
From http://www.cplusplus.com/doc/informa...scription.html
When choosing a programming language to make a project, many different
considerations can be taken. First, one must decide what is known as
the level of the programming language. The level determines how near to
the hardware the programming language is. In the lower level languages,
instructions are written thinking directly on interfacing with
hardware, while in "high level" ones a more abstract or conceptual code
is written.

Generally, high level code is more portable, that means it can work in
more different machines with a smaller number of modifications, whereas
a low level language is limited by the peculiarides of the hardware
which it was written for. Nevertheless, the advantage of low level code
is that it is usually faster due to the fact that it is indeed written
taking advantage of the possibilities of a specific machine.

A higher or lower level of programming is to be chosen for a specific
project depending on the type of program that is being developed. For
example, when a hardware driver is developed for an operating system
obviously a very low level is used for programming. While when big
applications are developed usually a higher level is chosen, or a
combination of critic parts written in low level languages and others
in higher ones.

(So from the above statement its clear that Object Oriented Programming
is generally used for application type of projects)


No it is not clear, this text only talks about levels of abstraction.

There are reasons for using OOP/OBP. One reason is that the problem when
displayed/presented as interacting objects is easier to understand. Think
about it: how many drivers in fact provide a common interface? This is a
typical example of in-kernel OOP. Other examples are the memory subsystem:
it could be abstracted as a single object that initially owns all
available RAM and then gives it away in an orderly way. Mostly, this is a
case of encapsulation (which is just one facet of OOP) and something which
can also be achieved with good modules written in C. Lastly, there are
dozens of kernel-level object types inside e.g. Linux. Every time you see
a struct and a bunch of associated functions this is in fact an object. If
the struct is only available as declaration, it also has encapsulation. If
it has some function pointers or a pointer to some other struct holding
them it is an implementation of an interface, i.e. derivation and thus
OOP. If it contains a void pointer named 'private' or 'user' or such it is
to extend the data of the struct with data the particular derived
implementation needs.

Please also note that C++ is not synonymous with OOP. C++ supports OOP
(yes, to an extent that disappoints OOP purists) but doesn't mandate it.

Some minor reasons for using C from
http://www.linuxdevices.com/eljonlin...07/4870s1.html [...] C++'s virtues are expensive. Advanced OOP features, such as
templates and the practice of using classes in the place of primitives,
to name two examples, cause unacceptable code bloat.
hmmm, templates are not generally an OOP feature. Wrapping primitives can
be just as efficient as using them directly - you just need to inline
relevant accessors. Other than that, an inappropriate design will alway
suck in this or that way and you have a choice to use those features, a
fact the author of above text blissfully ignores.
Virtual methods and polymorphism complicate runtime linking and
require many relocations. This slows C++ application launch time
considerably. C applications are both simple to link and amenable to
lazy linking, so they load quickly. (For details, see Waldo Bastian's
paper ``Making C++ Ready for the Desktop",
http://www.suse.de/~bastian/Export/linking.txt.)


Not only that this doesn't apply to in-kernel programming (as already
pointed out), it has also ceased to be true! It was just a matter of
patching the linker to cope with whole arrays at once, as they happen with
vtables of polymorphic C++ classes, IIRC it was called the 'combreloc'
patch.

cheers

Uli
(who is surprised by the low amount of flames in this thread)

Jan 18 '06 #109
> Very interesting. However, your analysis is how C would have to be
written to do all the things C++ does to implement OOP. Not a good
comparision. If you are primarily using C you really will not care a
whit about tying to do things the C++ way.


The intent of the articles is to show C equivalent of the processing
performed
by a C++ compiler. This gives you a good feel of the overhead in using
C++.
This information would be useful to someone evaluating the performance
characteristics of C++.

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

Jan 22 '06 #110

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.