LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-09-2004, 06:31 PM   #16
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45

megamanx, that link i posted mentions the same issue as said by stroustrup.
neither have i actually needed an OO approach to what i did in C++/Java. but then, im still in school, and im guessing the reall problems will appear when i start work.
even then, im told that a lot of OO code written does not really need such an approach.
 
Old 01-09-2004, 07:31 PM   #17
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
I will check the link h/w, thanks mate. I've just got my C++ grades, it was not good at all.. since I really don't understand the hole concept of h/w. Arghhh, looks like I'll have to remake that course :S
 
Old 01-10-2004, 02:22 PM   #18
Tesl
Member
 
Registered: Jun 2003
Location: Durham, UK
Distribution: Slackware 9, Mandrake 9.1
Posts: 163

Rep: Reputation: 30
at first i never saw the need for OO, i understood how it worked but never ever saw the need for it.

For a lot of projects i think thats the same, however in all the larger apps iv built OO has been vital for me, and has made things a lot lot easier. Its all about the design, and iv found a lot of problems easily solved using OO.

That and being able to write my own classes for little bits and pieces really can be extremely helpful. There are times when you write a little program and think "hang on, that might be useful in other places" so you can just save it into a class, write its accessor methods (or whatever it needs) then forget about it
 
Old 01-10-2004, 02:56 PM   #19
wgself
LQ Newbie
 
Registered: Jan 2004
Location: Charlottesville, VA
Distribution: RedHat 8.0
Posts: 9

Rep: Reputation: 0
I think the reason most large projects are in c is that they are at established companies with large libraries of code that handle most of the functionality of c++. It is far quicker to write a function in c and add it to a library than to write the same function in c++ and cover all ot the OO overhead and documentation. I've been using c for about twenty years anc c++ for ten. I use both as needed by the project at hand. Both are great compared to Java or VB. (My opinion - don't want to start a war either ;-)
 
Old 01-12-2004, 03:15 PM   #20
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
jtshaw:

Quote:
Object Orientation and Abstraction, while logically very efficient, doesn't lend itself to code that is as fast or as memory friendly as function code with only simple data types.
I've often seen this view expressed, but have yet to see any reasoning or evidence that supports it. I can understand why compile might be generally slower than for a similar C program, but why would a C++ prog use more memory or perform worse than a C program?
 
Old 01-12-2004, 06:28 PM   #21
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
and i found the "real" reason coming from the developers at the lkml. apparently, the assembly code generated from the compilers is not as clean/efficient when in C++.
 
Old 01-12-2004, 08:12 PM   #22
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
h/w, I believe that is exactly what I said earlier... gcc is better at compiling C code, thanks for verifying

Mad Cactus:
The more layers of abstraction the more variables you tend to have, the more function calls you have (which tend to translate to jumps in assembly), the more total lines of code, ect. ect. ect. More variables and more code tends to mean more memory usage. The more jumps you have the more processor time you use (since it takes a step longer to get to the next productive line of code). Unless you have ridiculously good optimization at compile time this will cause your code to be slightly slower. Now of course this doesn't make a bit of different on something like a 1Ghz. processor. But when you run it on a i386 or some slow embedded processor it sure does.
 
Old 01-12-2004, 08:30 PM   #23
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
by jtshaw
The more jumps you have the more processor time you use (since it takes a step longer to get to the next productive line of code).

its a bit worse than you make out because jumps also require the pipeline to be re-filled and possibly a cache refill if the destination isnt in the cache, this is the reason why compilers offer the option to unroll loops

by Mad Cactus
but why would a C++ prog use more memory or perform worse than a C program?

C++ has to implement rtti, you also need to store tables for virtual function addresses, and there are loads of temporary objects created and destroyed which results in a lot of constructor/destructor calls
 
Old 01-12-2004, 08:34 PM   #24
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
The type of programming I did in school also didn't need OO, but they taught it because in the world you programs aren't going to be that simple. The abstraction and layering is very useful for nearly everthing I've done since.

Currently I'm programming a lot of 3D stuff, and I don't know how I'd cope without my Vector, Matrix, and Camera classes. I wrote those about 4 years ago, they've been in continual use ever since.

As for applications written in C++ download KDE source, have a poke around. The best examples I ever saw of well written OO code.
 
Old 01-13-2004, 08:16 AM   #25
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
jtshaw:

Quote:
The more layers of abstraction the more variables you tend to have, the more function calls you have ...
I don't agree with that at all - the only difference between an object and functionally identical modular code is the notation used, the same variables and functions are used. So each object is a "variable" - so what? The memory needed is still only the sum of its members.
 
Old 01-13-2004, 08:56 AM   #26
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
by MadCactus
the only difference between an object and functionally identical modular code is the notation used, the same variables and functions are used. So each object is a "variable" - so what? The memory needed is still only the sum of its members.

that is not true as i said in my above post(23) the memory needed is not the same as you also need to store information about its type, virtual function addresses, etc. you also get a lot of extra function calls for constructors/destructors when creating temporary objects

look at the following c++ code
Code:
#include <iostream>

using namespace std;

class A
{
private:
	int a;
public:
	A() { 
		a=0;
		cerr << "Constructing a default A" << endl;
	}
	~A() {
		cerr << "Destructing an A" << endl;
	}
	A(A& b) {
		cerr << "Copying an A" << endl;
		a=b.a;
	}
	
	void set(int i) {
		a=i;
	}

	friend ostream& operator<<(ostream& os, A a);
};

ostream& operator<<(ostream& os, A b) {
	os << b.a;
	return os;
}

int main()
{
	A array[100];

	for(int i=0;i<100;i++) {
		array[i].set(i);
	}

	for(int i=0;i<100;i++) {
		cout << array[i] << endl;
	}

	return 0;
}
compiling it and running ./a.out 2>&1 1>/dev/null | wc -l gives me an output of 400, if i did operator<< by const reference(which you should always do) rather than value then it would cut me down to 200 but i wanted to show the copy constructor because there are times when const reference isnt possible. now just imagine i was passing a vector of these things around in a recursive function, can you see just how many function calls your gonna get.

now if you do it in C with structs then you dont get any of those function calls but nor do you get the safety of having constructors/destructors.
 
Old 01-23-2004, 02:34 AM   #27
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Your examples might be true for trivial samples, but given a more complex (real life system) I think you might find there is little to no performance benefit using C over C++..

C++ compilers are getting better and faster (generated code) with time.

The number of instructions in a object file is not a direct representation of the execution speed or memory use.

I believe in using my experience more than other's bold statements regarding these types of matters.

I have been programming for many years (15+) and have used c++ for nearly all development for the past 5 years. Not once have I experienced a performance issue due to code execution speed. In practice the design of the software is far more important than the language it is implemented in.

C++ gives speed close to C (close enough) but provides type safty, references (safe pointers), exceptions and all the other OO features we've been enjoying ever since its creation.

Some argure that C++ has RTTI (Run Time Type Information) virtual tables which cause additonal overhead. True but not quite. RTTI and virtual tables to add additional overhead but it is not that expensive, nor required. A C++ program man turn off RTTI and exceptions (exception handling requires RTTI) if it is deemed to expensive. Also virtual tables are only created/used when needed. If you never have virtual function or inheritence then you will not pay the penalty.

I believe that OO aids in decomposing a problem down to to implementable units which encapslate their functionality and infact make it easier (in general) to optimise performance critical code.

Its a matter of taste. C has its place, as does C++. They are both great languages, but both require good programmers to make use of their features rather than abuse.
 
Old 01-23-2004, 08:02 AM   #28
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
i generally agree with you mr_segfault, in a program thats actually doing real work then the extra time and memory taken by C++ is insignificant compared to what the algorithm itsself requires, on the fast machines of today you cant really see a noticeable difference, the aim of my post was to point out to MadCactus that higher level languages do use more memory and do make more function calls than there lower level equivelent even though its not noticable because of the speed/memory capacity of machines today.
 
Old 01-23-2004, 08:32 AM   #29
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
I also tend to agree with you mr_segfault. Today, right now, the C++ compilers like g++ are pretty much as good as the c compilers. However, if you think back to the early days of linux the same statement was not true. There were probably C++ compilers out that performed perfectly well, but the ones packaged with GCC did not. Many many open source projects have roots back to those days when pretty much everything Linux/Unix was in C.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can do me a favor? lifetaster Linux - Security 1 03-11-2005 10:01 AM
Can someone do me a favor? TerminalPhreak Linux - Newbie 3 02-27-2003 04:10 PM
can anyone do me a favor? I need... C++freak Linux - Distributions 4 09-22-2002 12:16 PM
What FS do you favor? NSKL Slackware 14 05-30-2002 02:02 AM
Squid favor... sancho5 Linux - Networking 1 09-24-2001 01:41 AM


All times are GMT -5. The time now is 06:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration