[英]C memory address and values
I have a problem I can't seem to figure it out. 我有一个问题,我似乎无法弄明白。 I hope someone could be able to throughly explain it to me.
我希望有人能够彻底向我解释。 I get that its very elementary..
我觉得这很简单......
Problem is following: 问题如下:
How do the following variables go into consecutive memory addresses and their value? 以下变量如何进入连续的内存地址及其值?
int8_t a = 0x65;
char b = 'k';
uint16_t c = 22222;
for example, 例如,
int8_t esim = 9;
would be stored as 将被存储为
0000:00001001
Anyone? 任何人?
I have a problem I can't seem to figure it out.
我有一个问题,我似乎无法弄明白。 I hope someone could be able to throughly explain it to me.
我希望有人能够彻底向我解释。 I get that its very elementary.
我认为这是非常基础的。
And here you go wrong. 在这里你出错了。 It is very elementary but not quite as you think it to be
这是非常基本的,但并不像你想象的那样
Distinct variables need not be stored in consecutive locations in memory. 不同的变量不需要存储在内存中的连续位置。 (Or as the last bullet says, not be stored in memory at all)
(或者最后一颗子弹说,根本不存储在内存中)
The storage of individual bytes within a multibyte value is implementation-defined - check your compiler manuals. 多字节值中的单个字节的存储是实现定义的 - 检查编译器手册。 Most personal computing processors nowadays use little-endian 2's complement for integers, however.
然而,现在大多数个人计算处理器都使用little-endian 2补码作为整数。
Even if they were organized by the compiler to appear in memory in exactly the same order as they are declared, each datatype can require an implementation-specific alignment and can therefore start only at an address that is multiple of this alignment 即使他们是由编译器组织在内存中出现,正是因为他们的声明相同的顺序,每个数据类型都需要实现特定的排列,因此在它是此对齐的倍数的地址只启动
And finally, there need not be any variables or memory allocations whatsoever, the compiler just needs to generate a program that behaves as if there were such variables. 最后,不需要任何变量或内存分配,编译器只需要生成一个程序, 就好像有这样的变量一样。
We can certainly say something about your program however. 不过,我们当然可以说一下您的计划。 If the excerpt
如果摘录
#include <stdint.h>
int8_t a = 0x65;
char b = 'k';
uint16_t c = 22222;
compiles and the variables are placed in memory, then 编译并将变量放在内存中
a
will be 8 bits with value 0b01100101
a
将为8位,值为0b01100101
c
will be 16 bits and stored in memory as 2 bytes - 0b11001110
and 0b01010110
at increasing memory addresses (little-endian, usual), or the same 2 bytes reversed: 0b01010110
and 0b11001110
(big-endian). c
将为16位并作为2个字节存储在存储器中 - 0b11001110
和0b01010110
在增加的存储器地址(little-endian,通常),或相同的2个字节反转: 0b01010110
和0b11001110
(big-endian)。 b
, if the execution character set is ASCII-compatible, as int8_t
exists, then char
must also be 8 bits wide, then its value will be stored as 0b01101011
(ie 107, the ASCII code of k
). b
, 如果 执行字符集是ASCII兼容的,因为int8_t
存在,那么char
也必须是8位宽,然后它的值将被存储为0b01101011
(即107, k
的ASCII码)。 Additionally, most often the alignment requirement of an uint16_t
object is 2; 另外,大多数情况下
uint16_t
对象的对齐要求是2; if that is the case, it must start at an even address. 如果是这种情况,它必须从偶数地址开始。
This deduction is only possible because the int8_t
and uint16_t
must not have padding bits , hence from having them we can deduce that the width of the smallest addressable unit ( char
) must be 8 bits too. 这种推论是唯一可能的,因为
int8_t
和uint16_t
不能有填充位 ,因此通过它们我们可以推断出最小可寻址单元( char
)的宽度也必须是8位。 And uint16_t
has only 2 bytes, hence it can only have two choices for endianness. 并且
uint16_t
只有2个字节,因此它只能有两个字节序选择。
It is easy to test how GCC organizes global variables . 很容易测试GCC如何组织全局变量 。 Consider the module having the source code
考虑具有源代码的模块
#include <stdint.h>
int8_t a = 0x65;
char b = 'k';
uint16_t c = 22222;
we can compile it to an object file: 我们可以将它编译成目标文件:
% gcc -c layouttest.c -o layouttest.o
and then use nm
to list the symbols and their addresses: 然后使用
nm
列出符号及其地址:
% nm layouttest.o
0000000000000000 D a
0000000000000001 D b
0000000000000002 D c
It seems to be as Jabberwocky's answer expects. 这似乎是Jabberwocky的答案所期望的。 If we now compile with
-O3
, the results can be different: 如果我们现在使用
-O3
编译,结果可能会有所不同:
% gcc -c layouttest.c -o layouttest.o -O3; nm layouttest.o
0000000000000003 D a
0000000000000002 D b
0000000000000000 D c
Ie the variables were reorganized, with c
at the bottom. 即变量被重新组织,底部是
c
。
Complement to Antti Haapala's answer: Antti Haapala答案的补充:
While it is totally up to the compiler how and where to store variables, the memory layout of variables declared consecutively is often in the same order than the order of declaration especially in non optimized code. 虽然完全取决于编译器如何以及在何处存储变量,但连续声明的变量的内存布局通常与声明顺序的顺序相同,尤其是在非优化代码中。
So the variables declared like this: 所以变量声明如下:
int8_t a = 0x65;
char b = 'k';
uint16_t c = 22222;
could be stored like this: 可以像这样存储:
Address Content in binary
--------------------------
0000: 01010101 (0x64)
0001: 01101011 ('k' = 107, ASCII code of k)
0002: 11001110 (low bits of 22222)
0003: 01010110 (high bits of 22222)
where Address
is the relative address with repsect to the memory address of variable a
. 其中
Address
是相对地址,带有对变量a
的内存地址的repsect。
Once again: don't assume that this is the necessarily the case on your platform. 再一次:不要认为这是您平台上必然的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.