LWN.net Logo

Russell: GCC and C vs C++ Speed, Measured

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 23, 2013 9:13 UTC (Sat) by khim (subscriber, #9252)
In reply to: Russell: GCC and C vs C++ Speed, Measured by mveinot
Parent article: Russell: GCC and C vs C++ Speed, Measured

There are small, subtle, differences. Perhaps the most well-known is the fact that “sizeof 'a'” has size 4 in C, but 1 in C++. This is not big deal since automatic type conversion gives you the same result in the end, but it can easily affect heuristics which will mean that code produced will be equivalent yet different.


(Log in to post comments)

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 24, 2013 9:29 UTC (Sun) by aleXXX (subscriber, #2742) [Link]

What does --enable-build-with-cxx actually do ?

I mean, even if I do "g++ main.c", main.c is still considered a C file and sizeof('a') should still be 1.
Or does that forcce cxx as a language on all C files ?

Alex

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 24, 2013 12:46 UTC (Sun) by jwakely (subscriber, #60262) [Link]

> What does --enable-build-with-cxx actually do ?

Approximately, it does CC=g++

> I mean, even if I do "g++ main.c", main.c is still considered a C file and sizeof('a') should still be 1.

I assume you mean sizeof('a') should be 4 if it was considered a C file, but it isn't, using g++ implies the input file is C++

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 26, 2013 10:48 UTC (Tue) by khim (subscriber, #9252) [Link]

I mean, even if I do "g++ main.c", main.c is still considered a C file and sizeof('a') should still be 1.

Rilly? It's easy to test, you know.

$ cat main.c
#include <stdio.h>

int main() {
  printf("%zd\n", sizeof 'a');
  return 0;
}
$ gcc main.c -o main ; ./main
4
$ g++ main.c -o main ; ./main
1

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 26, 2013 0:49 UTC (Tue) by nix (subscriber, #2304) [Link]

Perhaps the most well-known is the fact that “sizeof 'a'” has size 4 in C, but 1 in C++.
What? sizeof(char) is 1 by definition in both languages.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 26, 2013 1:18 UTC (Tue) by andrel (subscriber, #5166) [Link]

But 'a' is not a char in C.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 27, 2013 15:32 UTC (Wed) by nix (subscriber, #2304) [Link]

Ooh. You're right, I never noticed, it's an int. Bizarrely counterintuitive, though I can see why they did it, I think.

How is it that even a language as simple as C still has corners to learn after decades using it?

C, simple?

Posted Mar 28, 2013 9:27 UTC (Thu) by ncm (subscriber, #165) [Link]

C isn't really simple at all, it's just uncommonly good at presenting the illusion of simplicity. If it really were simple, you wouldn't like it.

C, simple?

Posted Mar 28, 2013 10:01 UTC (Thu) by dlang (✭ supporter ✭, #313) [Link]

As languages go, C actually is pretty simple.

you can do very complex things with very simple building blocks, remember that even the most complex CPU is a combination of AND, OR, NOT gates combined with memory. These are very simple components, but the results that are built with these simple components are very complex.

C, simple?

Posted Mar 28, 2013 17:54 UTC (Thu) by apoelstra (subscriber, #75205) [Link]

> C isn't really simple at all, it's just uncommonly good at presenting the illusion of simplicity. If it really were simple, you wouldn't like it.

As dlang said, the machine model is very simple compared to that of other imperative languages. Lisp and Haskell are probably simpler, but Java or Python are much more complex (and PHP is so wild that I would argue it -has- no machine model!).

By "if it really were simple, you wouldn't like it", ITYM "if it really were simple, it couldn't be used to program computers", because C's complexity seems to be an unavoidable consequence of physical machines (e.g. having finitely sized types).

C, simple?

Posted Mar 28, 2013 19:50 UTC (Thu) by dlang (✭ supporter ✭, #313) [Link]

assembly is simpler, and if you only ever had to program one machine it may even be reasonable, but since assembly is different for every system, and it doesn't have any standard way to define higher level structures, C was born

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds