"static" as class member and linkage

Dear all,

In C (or C' textbook), "static" is used as internal linkage, which declare the static variable only existing as different instances in the defferent source file. On the other hand, static member of class seems to be the same instance (Singleton in C++ uses that).

How is "static" interpreted in terms of memory arrangement?(I heard static variable is assigned into text segment in each process)
Last edited on
How is "static" interpreted in terms of memory arrangement
The compiler is free to put static variables wherever it wants. You can't portably rely on the placement. That said, I suspect that statics are usually stored along side the globals. It seems to me that that would be easiest for the compiler writer.

I heard static variable is assigned into text segment in each process
The text segment is where the code goes and is often (usually?) read-only, so you can't put any non-const variables there.
>The text segment is where the code goes and is often (usually?) read-only, so you can't put any non-const variables there.

This point is probably my misunderstanding, sorry.

What you said is understandable. But, how should I understand internal linkage, the same names refer to different instances?
Don't be confused by the different usages of the static keyword. A static variable inside a class is treated just like any other global variable.
Last edited on
Dear Peter87

I appreciate your comment.

I understand you are right. But, why did C++ language specification decide to use static keyword in class, instead of extern? This is my subtle question. It leads to misunderstanding.
Last edited on
I don't think I can give a good answer because I don't know enough about the ancient history of C and C++, but I'm guessing that static made sense because it makes the variable have static storage duration.
Topic archived. No new replies allowed.