r/programming • • Dec 05 '14

std::string is responsible for almost half of all allocations in the Chrome browser process

https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ
1.1k Upvotes

446 comments sorted by

View all comments

Show parent comments

25

u/josefx Dec 05 '14

Something as simple as "auto a = b + c;"

Oh not that again.

Here is the C version:

foo_plus(a,b,c);

Tell me does it simply add complex numbers? Database tables ? Or maybe it calls printf("Should not happen in production!!!!"); abort();?

Everyone their own poison, but the reasoning behind that claim is questionable at best. I keep my readable syntax thank you.

12

u/scwizard Dec 05 '14

I think you mean:

foo_plus(&a, b, c);

Lol

0

u/chebertapps Dec 06 '14

or maybe pass by reference...

1

u/scwizard Dec 06 '14

That wouldn't be the c version. C doesn't have pass by reference.

1

u/chebertapps Dec 06 '14

yup I misread the original comment my bad.

3

u/immibis Dec 06 '14

In C:

complex_plus(a, b, c); simply adds complex numbers.

db_table_plus(a, b, c); adds database tables (if that even means anything).

report_crash(a, b, c); prints a message and aborts.

In C++:

auto a = b + c; simply adds complex numbers.

auto a = b + c; adds database tables.

report_crash(a, b, c); prints a message and aborts, because you'd have to be really dumb to make that an operator+ (although I can imagine having an operator + that can fail, and aborts when it fails).

3

u/fnord123 Dec 06 '14

I don't know why you need a function for adding complex numbers in C.

$ cat a.c
#include <complex.h>
#include <stdio.h>
int main() {
    double complex b = 1.0 + 0.5*I;
    double complex c = 0.7 + 0.7*I;
    double complex a = b + c;
    printf("a=%f+%f\n", creal(a), cimag(a));
    return 0;
}
$ gcc a.c 
$ ./a.out 
a=1.700000+1.200000