简体   繁体   English

全局变量在C中不连续

[英]Global Variables not contiguous in C

Currently we are trying to keep track of the variables stored in memory, however we have faced with the following issues, maybe you would help us out 当前,我们正在尝试跟踪存储在内存中的变量,但是我们面临以下问题,也许您会帮助我们

Currently we defined some global variables in our code, as follows 当前,我们在代码中定义了一些全局变量,如下所示

int x;
char y;

And we added the following lines of code 并且我们添加了以下代码行

int main ( int argc, char *argv[ ] ){ 
printf("Memory of x %p\n",&x); 
printf("Memory of y %p\n",&y);
system( "pause");
return 0;
}

The program returned the following address 程序返回了以下地址

Memory of x 0x028EE80
Memory of y 0x028EE87

If I make a sizeof x and a sizeof y I get 4 and 1 (the size of types integer and char) What is then in between 0x028EE84 and 0x028EE86? 如果我使sizeof x和sizeof y得到4和1(整数和char类型的大小),那么0x028EE84和0x028EE86之间是什么? why did it took 7 positions in order to insert the char variable in memory instead of inserting it on the 0x028EE81 memory position? 为什么要花7个位置才能将char变量插入内存中,而不是将其插入0x028EE81内存位置?

In general, the compiler will try to do something called alignment. 通常,编译器将尝试执行称为对齐的操作。 This means that the compiler will try to have variables ending on multiples of 2, 4, 8, 16, ..., depending on the machine architecture. 这意味着编译器将尝试使变量以2、4、8、16 ...的倍数结尾,具体取决于机器的体系结构。 By doing this, memory accesses and writes are faster. 这样,内存访问和写入速度更快。

There are a number of very good answers here already however I do not feel any of them reach the very core of this issue. 这里已经有很多非常好的答案,但是我认为其中没有一个是这个问题的核心。 Where a compiler decides to place global variables in memory is not defined by C or C++. 编译器决定将全局变量放置在内存中的位置不是由C或C ++定义的。 Though it may appear convenient to the programmer to store variables contiguously, the compiler has an enormous amount of information regarding your specific system and can thus provide a wide array of optimisations, perhaps causing it to use memory in ways which are not at first obvious. 尽管对于程序员来说,连续存储变量似乎很方便,但是编译器具有与您的特定系统有关的大量信息,因此可以提供广泛的优化选择,也许使编译器以最初并不明显的方式使用内存。

Perhaps the compiler decided to place the int in an area of memory with other types of the same alignment and stuck the char among some strings which do not need to be aligned. 也许编译器决定将int与其他类型的相同对齐方式放在内存区域中,并将char粘在不需要对齐的某些字符串中。

Still, the essence of this is that the compiler makes no obligations or promises of where it will store most types of variables in memory and short of reading the full sources of the compiler there is no easy way to understand why it did so. 尽管如此,其本质还是在于,编译器对将大多数类型的变量存储在内存中的位置不承担任何义务或承诺,而且如果不阅读编译器的全部源代码,就很难理解为什么这样做。 If you care about this so badly you should not be using separate variables, consider putting them into a struct which then has well defined memory placement rules (note padding is still allowed). 如果您非常在意此问题,则不应使用单独的变量,请考虑将其放入结构中,该结构具有明确定义的内存放置规则(仍然允许填充)。

Because the compiler is free to insert padding in order to get better alignment . 因为编译器可以自由插入填充以获取更好的对齐方式

If you absolutely must have them right next to each other in memory, put them in a struct and use #pragma pack to force the packing alignment to 1 (no padding). 如果绝对必须在内存中将它们紧挨着,请将它们放在struct然后使用#pragma pack强制将包装对齐方式设置为1(无填充)。

#pragma pack(push, 1)

struct MyStruct
{
    int x;
    char y;
};

#pragma pack(pop)

This is technically compiler-dependent behavior (not enforced by the C++ standard) but I've found it to be fairly consistent among the major compilers. 从技术上讲,这是与编译器有关的行为(不是由C ++标准强制执行),但是我发现在主要的编译器中,它是相当一致的。

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

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