2

I have this class:

#include <cstdio> #include <cstring> class Number { int data; public: int get() { return data; } char* to_str(int& size) { static char str[10]; snprintf(str, 10, "%d", data); size = strlen(str) + 1; return str; } }; 

I know that returning static arrays is dangerous (i. e. not thread-safe etc.) and that, since I am using C++ I should use std::string.

What I am interested in here is how this works. Since each method is only compiled once, and it's code is then used by all objects (through the invisible first argument, accessible as the this pointer), this leaves me with a dillema: is that static array unique for each object of that class or is it shared for all objects? Again, I am interested in the mechanic (for learning purposes) and not for good coding practices (the code above is definitely not good code).

1
  • 3
    "is it shared for all objects?" - yes. As with all static objects , there is only one instance of that variable. Commented Sep 15, 2019 at 20:13

1 Answer 1

2

Since each method is only compiled once

It is compiled several times, given it is an inline definition (assuming your class is in a header and it is included by several translation units (TUs)).

If you were providing the definitions in a TU (a .cpp file), then you are right.

and its code is then used by all objects (through the invisible first argument, accessible as the this pointer)

Note that this pointer is not related to whether static data members and static local variables are shared or not, regardless of whether the code is or not shared across the program.

is that static array unique for each object of that class or is it shared for all objects?

Static local variables in methods (member functions) are unique, i.e. shared between all instances (the same as with any other function).

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! The this pointer is passed to each method as an invisible first parameter and the function uses it to know which object it is acting on. I provided the definitions of the methods inline for simplicity. In real code you should always split declarations-definitions in .h-.cpp pairs of files
@DarkAtom You're welcome! "The this pointer is passed to each method as an invisible first parameter and the function uses it to know which object it is acting on" Yeah, but that has nothing to do with whether static variables are shared or not (i.e. the language could have chosen other semantics).
@DarkAtom "In real code you should always split declarations-definitions in .h-.cpp pairs of files" That is debatable. I never do that unless I am facing big compilation times and want to parallelize. In fact, if you are writing header-only libraries, you cannot even do it.
There are very strong reasons as to why you should never put definitions in header files. For example, if you define a global variable in a header (let's say int a = 3), and then include that header in multiple .cpp files (TUs), you will either get an error or worse, some problem which will show up in some unrelated place in the code. But let's say you don't get an error:
If you increment a in a function from TU1, a will NOT be changed in TU2 because TU2 has a completely different variable named a! The correct approach is (well, the correct approach is to not use global variables, but still) to define a in a .cpp file and then write int a; in the header. That will only count as a declaration and not a definition, so all TUs which have included the header will know a is a variable located in another TU.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.