Re: Where does C++ fit in?

OTOH, I think this goes toward the myth that OO is somehow

>inherently more wasteful of resources than other techniques.

It's not OO I was talking about, but C++ specifically.

As a totally non-scientific example, write a hello world program in C. Compare the binary sizes when compiling with C and C++, even with exceptions turned off. C++ compilers are still catching up in the efficiency department.

--

"Jokes mentioning ducks were considered particularly funny." - cnn.com
Reply to
Chesney Christ
Loading thread data ...

"Hello world" is probably not the best test of a compiler, since were mostly using libraries, but I'll bite.

I compile the following program natively using GCC 3.2.2 under Redhat Linux.

main.c:

#include

int main(int argc,char* argv[]){ printf("Hello world.\n"); }

% gcc -fno-exceptions -fno-rtti -o main -x c main.c % strip main

-rwxrwxrwx 1 mike mike 2788 Apr 30 17:28 main

% gcc -fno-exceptions -fno-rtti -o main -x c main.c % strip main

-rwxrwxrwx 1 mike mike 2792 Apr 30 17:27 main

The C++ program is 4 bytes larger.

BTW, I got the same results with exceptions and rtti enabled.

While we're at it, here is the result of a few other GCC cross compilers.

xscale-elf-gcc -fno-exceptions -fno-rtti -c -x c main.c xscale-elf-strip main.o

-rw-rw-rw- 1 mike mike 500 Apr 30 17:49 main.o

xscale-elf-gcc -fno-exceptions -fno-rtti -c -x c++ main.c xscale-elf-strip main.o

-rw-rw-rw- 1 mike mike 508 Apr 30 17:50 main.o

powerpc-eabi-gcc -fno-exceptions -fno-rtti -c -x c main.c powerpc-eabi-strip main.o

-rw-rw-rw- 1 mike mike 532 Apr 30 17:52 main.o

powerpc-eabi-gcc -fno-exceptions -fno-rtti -c -x c++ main.c powerpc-eabi-strip main.o

-rw-rw-rw- 1 mike mike 536 Apr 30 17:51 main.o

h8300-elf-gcc -fno-exceptions -fno-rtti -c -x c main.c h8300-elf-strip main.o

-rw-rw-rw- 1 mike mike 428 Apr 30 17:54 main.o

h8300-elf-gcc -fno-exceptions -fno-rtti -c -x c++ main.c h8300-elf-strip main.o

-rw-rw-rw- 1 mike mike 428 Apr 30 17:54 main.o

Again, this is a bad and quite way to compare languages, but ...

To paraphrase someone whose name escapes me, "First make it right, then optimize."

--
Michael N. Moran           (h) 770 516 7918
5009 Old Field Ct.         (c) 678 521 5460
Kennesaw, GA, USA 30144    http://mnmoran.org

"... abstractions save us time working, but they don't
  save us time learning."
Joel Spolsky, The Law of Leaky Abstractions

The Beatles were wrong: 1 & 1 & 1 is 1
Reply to
Michael N. Moran

OK (frantic shuffling of goalposts) now compare with the --static switch :)

--

"Jokes mentioning ducks were considered particularly funny." - cnn.com
Reply to
Chesney Christ

Only 100 more bytes in C++, but virtually all of that is from the startup code it is linked to. When compiling to an assembly I can see that the C++ version has merely one extra move instruction so that main() returns a valid value.

--
Darin Johnson
    Where am I?  In the village...  What do you want?  Information...
Reply to
Darin Johnson

Jon Bentley. author of _Programming Pearls_

formatting link

- a book no embedded software developer should be without.

-- Guy Macon, Electronics Engineer & Project Manager for hire. Remember Doc Brown from the _Back to the Future_ movies? Do you have an "impossible" engineering project that only someone like Doc Brown can solve? My resume is at

formatting link

Reply to
Guy Macon

~ ; cat hello.c #include int main(void) { return printf("Hello world\n"); } ~ ; ln hello.c hello.cc ~ ; gcc hello.c ~ ; ls -l a.out

-rwxrwxr-x 1 pete pete 11251 May 1 00:16 a.out ~ ; g++ hello.cc ~ ; ls -l a.out

-rwxrwxr-x 1 pete pete 11356 May 1 00:16 a.out ~ ;

Hmmm. 1% difference? ;)

(A hello world using streams in C++ comes to 12.9k. You're right. This is highly non-scientific ;))

pete

--
pete@fenelon.com "there's no room for enigmas in built-up areas"
Reply to
Pete Fenelon

Oh dear. ;)

~ ; gcc --static hello.c ~ ; ls -l a.out

-rwxrwxr-x 1 pete pete 1692714 May 1 00:21 a.out ~ ; strip a.out ~ ; ls -l a.out

-rwxrwxr-x 1 pete pete 385632 May 1 00:21 a.out

~ ; g++ --static hello.cc ~ ; ls -l a.out

-rwxrwxr-x 1 pete pete 1692715 May 1 00:21 a.out ~ ; strip a.out ~ ; ls -l a.out

-rwxrwxr-x 1 pete pete 385632 May 1 00:22 a.out

;)

pete

--
pete@fenelon.com "there's no room for enigmas in built-up areas"
Reply to
Pete Fenelon

s/embedded//

Along with Kernighan & Pike's "The Practice of Programming", Brooks' "The Mythical Man Month", and Bignell and Fortune's "Understanding Systems Failures".

pete

--
pete@fenelon.com "there's no room for enigmas in built-up areas"
Reply to
Pete Fenelon

gcc -static -fno-exceptions -fno-rtti -x c++ -o main main.c strip main ls -l main

-rwxrwxrwx 1 mike mike 377888 May 1 07:53 main

gcc -static -fno-exceptions -fno-rtti -x c -o main main.c strip main ls -l main

-rwxrwxrwx 1 mike mike 377888 May 1 07:54 main

--
Michael N. Moran           (h) 770 516 7918
5009 Old Field Ct.         (c) 678 521 5460
Kennesaw, GA, USA 30144    http://mnmoran.org

"... abstractions save us time working, but they don't
  save us time learning."
Joel Spolsky, The Law of Leaky Abstractions

The Beatles were wrong: 1 & 1 & 1 is 1
Reply to
Michael N. Moran

You forgot the -fno-exceptions -fno-rtti switches. My results have identical sizes with those.

gcc -static -fno-exceptions -fno-rtti -x c -o main main.c strip main ls -l main

-rwxrwxrwx 1 mike mike 377888 May 1 07:54 main

gcc --static -fno-exceptions -fno-rtti -x c++ -o main main.c strip main ls -l main

-rwxrwxrwx 1 mike mike 377888 May 1 07:56 main

--
Michael N. Moran           (h) 770 516 7918
5009 Old Field Ct.         (c) 678 521 5460
Kennesaw, GA, USA 30144    http://mnmoran.org

"... abstractions save us time working, but they don't
  save us time learning."
Joel Spolsky, The Law of Leaky Abstractions

The Beatles were wrong: 1 & 1 & 1 is 1
Reply to
Michael N. Moran

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.