What c++ does for you, behind the scenes

Started by
21 comments, last by visitor 14 years, 5 months ago
Quote:Original post by ToohrVyk
In fact, if you make a blanket statement about C++ without specifying otherwise, people will assume that you are discussing well-behaved programs. Saying "references can be null" is just going to lead to misunderstandings and pointless arguments, even though it's technically true that in misbehaving C++ programs you can manage to create a reference r such that &r is a null pointer.


Some code to do just that:
#include <cstdio>int main (int argc, char *argv[]) {    int *x = NULL;    int& y = *x;    printf("%d", x);    printf("%d", y);    return 0;}


GCC/MinGW compiles that fine. When run, however, the program outputs "0" and then crashes.
Advertisement
Quote:Original post by Zahlman

There's nothing about the implementation of std::string that requires a std::auto_ptr, BTW, although it is "managed" memory in a sense. There's also nothing in the spec that I know of that would mandate a re-allocation every time - if the string being copied over was longer, there's no reason the old storage couldn't be reused. You're also forgetting about short-string optimizations. And that __FILE__ and __LINE__ stuff is only going to happen in debug mode.


I gave string only as an example.

In C, b is assigned to a. No allocations will ever happen, no magic, no nothing. Either literal value or pointer will be assigned. A memcpy might be involved.

And while it's true that in some cases, some compiler, some STL implementation might under certain circumstances choose to perform certain optimizations, avoid some allocations, might avoid making copies - this is exactly what "behind the scenes" means.

There are obviously optimizations that can happen, but if I give you a line of code, can you say what it does?
Foo bar() {  ...  return x;};
What does this do in C, and what all can happen in C++? How much memory will be allocated, how long will it take to run, how much code will be generated? What about C# or Java?

There are many acronymed optimizations that might apply to this case, but they are again left at specific compiler's discretion. In this respect, C++ is highly non-deterministic by design.

Quote:Which you can't even create in C.

typedef struct FOO {  char * a;  char * b;  char * c;} foo;foo * make_safe_deep_copy(foo* x);
It can be done quite trivially, but the code in the end will be quite clear, and very explicit about what happens.

And I'm not trying to advocate C as superior, just pointing out why C still tends to be more popular in some domains.
Actually that signature would work for implementing a copy constructor. I suppose assignment with deep copy might look something like this:

typedef struct FOO {  char * a;  char * b;  char * c;} foo;int safe_deep_assign(foo* x, const foo* y){    char *a = 0, *b = 0, *c = 0;    a = malloc(strlen(y->a) + 1);    b = malloc(strlen(y->b) + 1);    c = malloc(strlen(y->c) + 1);    if (a && b && c) {        strcpy(a, y->a);        strcpy(b, y->b);        strcpy(c, y->c);        free(x->a);        free(x->b);        free(x->c);        x->a = a;        x->b = b;        x->c = c;        return 1;    }    else {        free(a);        free(b);        free(c);        return 0;    }}


There's also quite a lot going on under the hood, and it gets more complicated than that if you want to reuse the char buffer if the target's string is long enough. (BTW, what do you mean by exception-safe default assignment - do you mean exception-safe as in not leaking or exception-safe as in leaving target unchanged in case of failure, as the default assignment operator should not be concerned with the latter?)

In this particular case you seem to be talking more against operator overloading, assignment has nothing more going on under the hood for primitive types than it has in C.

Perhaps one place where significantly more might be going on is the C++ coders custom to store things by value in containers. Overhead of copying under the hood is more or less avoidable with the use of pointers, but you have a choice of trading some performance for a higher and simpler abstraction.

This topic is closed to new replies.

Advertisement