2

Question: Are the tags on structures that are declared as static at file scope private to the file in the same way that the actual struct variable being declared will be? Or are the structure tags common across files?

This question came from a problem I am having with the MSVS2019 debugger showing me the wrong values and struct member names in some files. (You can read about the bug here, if you're interested: Visual Studio 2019 Debugger Issue) The files were all cloned from an original and all have something like this:

static struct MyPrivateData_s
{
   char *szData[64];
} myData;
static int myCount;

The actual members of the struct vary from file-to-file and the values are quite different for each.

I know that variables that are declared static, but above any functions in a source file have "file scope" -- are global to that file only. I'm pretty certain the VS debugging system has a problem, but it made me wonder about how (or even if) the static storage class affects the structure tag.

If the tag is treated like the name of the struct and other variables, then it is private to the file. However, if the tag is treated differently, it may be triggering the VS bug. Remember, I am asking about only the structure tag, not the name of the actual structure variable. It would also be good to know how names from a typedef are affected by 'static' as well.

I have been trying to find the answer in all of the various C references I own and can find on line, but I (a) don't know the right question to ask and/or (b) am not understanding (or recognizing?) the answer when I find it. Hopefully a C guru can help me out.

FWIW, I think that anything following static at file scope should be restricted to the file where it is found. In fact, only below the point it is found in the file. That is how I have always coded and I have never encountered an issue before this debugger thing made me wonder about this. Thanks!

I think this may answer my question: Limit Struct Scope even though it does not explicitly use the term "tag". Instead, it appears to be calling the tag a "type", which, it becomes when prefixed by 'struct'. So static has no effect, but the fact that the tag appears in a C file and NOT in a common header means that the tag is private to the file. Sorry to have been a bother.

Steve Valliere
  • 1,119
  • 7
  • 12
  • None of the sentences in the body or title of your post is a question. To ask a question, you should actually ask a question. – Eric Postpischil Oct 22 '19 at 18:23
  • At the start, you write about the debugger confusing values in different **objects**. Later, you write you are only asking about **tags**. At the end, you write about the scope of things declared with `static`. It is not at all clear what you are asking about. – Eric Postpischil Oct 22 '19 at 18:26
  • @EricPostpischil I had hoped that "wonder about how (or even if) the static storage class affects the structure tag" was close enough to a question, sorry for the confusion. – Steve Valliere Oct 22 '19 at 19:23

2 Answers2

3

static does not affect structure tags or scope.

There is no scope beyond file scope (C 2018 6.2.1 1). Identifiers in different scopes can refer to the same object via linkage (6.2.2 1). If the different scopes are in different translation units, the identifiers can refer to the same object if they have external linkage.

Structure tags do not have linkage (6.2.2 6: “The following identifiers have no linkage: an identifier declared to be anything other than an object or a function;…”) Therefore, structure tags in different translation units refer to different entities.

This means that structure types in different translation units are different types. One would then wonder how the same structure can be used in different translation units. Per 6.2.7 1, structure types in different translation units may be compatible. For structures, compatibility largely requires identical definitions, with some allowances such as possibly omitting a tag.

The presence of static in a declaration (but not inside [ and ]) affects linkage (6.2.2 3) and storage duration (6.2.4 3) for objects and functions.

This answers this question:

Question: Are the tags on structures that are declared as static at file scope private to the file in the same way that the actual struct variable being declared will be? Or are the structure tags common across files?

static affects neither structure tags nor scope. It affects whether object and function identifiers can be related across translation units due to linkage, not scope.

typedef names are aliases.

It would also be good to know how names from a typedef are affected by 'static' as well.

C 2018 6.7.8 3 says “… A typedef declaration does not introduce a new type, only a synonym for the type so specified…” As above, static affects only objects and functions; it does not affect types or type names.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I think the author just wants some documentation on what language errata can happen when you try a declaration and a definition at the same time. In general, I’ve found that where the `block-stmt` is used to force the language to have a really programmatically specific effect that the programmer is able to rest easily with the result. – C. R. Ward Oct 23 '19 at 16:17
  • This answer is what I was hoping to get. Now I have some verifiable facts to use in case Microsoft tries to claim that there is no bug in their debugger and my issue is caused by my code. Thank you! – Steve Valliere Oct 23 '19 at 18:40
0

Try this instead:

struct _myprivdata_s_ {
    char *szData[64];
};
typedef struct _myprivdata_s_ MyPrivateData_s;
static MyPrivateData_s myData;
static int myCount;
C. R. Ward
  • 93
  • 5
  • This answer does not explain. Is it taking advantage of some aspect of C semantics as specified by the standard? Is it working around a compiler/debugger bug? Something else? What is it supposed to accomplish? Which of the issues mentioned in the question does it speak to? – Eric Postpischil Oct 22 '19 at 18:55
  • The problem seems to stem from using `static` and `struct` in the declaration statement. This choice doesn't always have the effect the programmer expects. —CW – C. R. Ward Oct 22 '19 at 18:58
  • What problem? What effect? How does this answer fix it? – Eric Postpischil Oct 22 '19 at 18:59
  • You can try this link @[ https://ideone.com/8p8dBm ] to tell me if this is the problem that the original poster intends to solve as he says... – C. R. Ward Oct 22 '19 at 19:01
  • @C.R.Ward I'm not sure how a C++ example answers a C question. Also, my actual question is about the visibility of the struct tag when multiple files use the same tag for a static structure. I tried to clarify the question at the top of my post. – Steve Valliere Oct 22 '19 at 19:21
  • @Steve.Valliere: Do you have a longer snippet of code than the one you gave? – C. R. Ward Oct 22 '19 at 19:42