简体   繁体   English

隐式内部链接与显式内部链接(“静态”)不同?

[英]Implicit internal linkage not the same as explicit internal linkage (“static”)?

Today I encountered a pecularity which, although probably not really important, nevertheless puzzles me.今天我遇到了一个特殊性,虽然可能不是很重要,但让我感到困惑。 Maybe I'm just not understanding C++ correctly, too.也许我也只是不正确理解 C++ 。

Some arrays inside a source file point to string literals, like so:源文件中的一些 arrays 指向字符串文字,如下所示:

const char* a[] = { "a", "b", "c" };
const char* b[] = { "d", "e"};
const char* c[] = { "f", "g"};

None of these pointer arrays is ever used in any way other than being passed to GetProcAddress to retrieve a function pointer from a library (this is a non-blocking dynamic OpenAL/EFX/capture function loader and context creator/manager). None of these pointer arrays is ever used in any way other than being passed to GetProcAddress to retrieve a function pointer from a library (this is a non-blocking dynamic OpenAL/EFX/capture function loader and context creator/manager).

It eventually occurred to me that I should probably declare those variables as static const since they're not needed anywhere outside that very.cpp file, so making internal linkage explicit seemed appropriate.我最终想到,我可能应该将这些变量声明为static const ,因为在very.cpp 文件之外的任何地方都不需要它们,因此明确内部链接似乎是合适的。 They should have internal linkage anyway (ISO14882 3.5(3)), so we're only being good citizens by making explicit what the compiler already assumes.它们无论如何都应该有内部链接(ISO14882 3.5(3)),所以我们只是通过明确编译器已经假设的内容来成为好公民。

Doing that innocent change resulted in a 512 byte increase of executable size.进行这种无害的更改导致可执行文件大小增加了 512 字节。 Not like an additional 512b really matter, but it just didn't seem to make sense that the exact same thing would result in different code.不像额外的 512b 真的很重要,但完全相同的事情会导致不同的代码似乎没有意义。 Since static const is deprecated (ISO14882 7.3.1.1(2)), I tried an anonymous namespace also, with the same result.由于不推荐使用static const (ISO14882 7.3.1.1(2)),我也尝试了匿名命名空间,结果相同。

Looking at the assembler source shows that explicit internal linkage ( static or namespace{} ) will move the string literals into .rdata rather than .data , and the string literals are interleaved with pointer-to-string-literal arrays, instead of having all strings and all pointers in one block, respectively.查看汇编源代码表明,显式内部链接( staticnamespace{} )会将字符串文字移动到.rdata而不是.data中,并且字符串文字与指向字符串文字的指针 arrays 交错,而不是全部字符串和一个块中的所有指针,分别。 Herein probably lies the reason for the different size too -- very likely shuffling data from one section to another has hit a section size constraint.这也可能是大小不同的原因——很可能将数据从一个部分改组到另一个部分已经达到了部分大小限制。 Interestingly, all 3 flavours mangle the names differently too.有趣的是,所有 3 种口味的名称也不同。

Now I wonder: Am I making a fallacy, should those pointers not have internal linkage?现在我想知道:我是否犯了一个谬误,这些指针是否应该没有内部链接?

Also, in my understanding const is already read-only, inhowfar is static const "more read-only" (one goes into .rdata and the other does not)?另外,据我所知, const已经是只读的, static const “更多只读”(一个进入.rdata ,另一个没有)?

Your arrays are not declared const , hence they aren't implicitly internal linkage either.您的 arrays声明const ,因此它们也不是隐式内部链接。 What you have is non-const arrays of pointers-to-const.您所拥有的是指向常量的非 const arrays。

That said, I don't know why this affects whether the strings end up in .rdata or .data .也就是说,我不知道为什么这会影响字符串是否以.rdata.data结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM