如何确定数据存储在内存中的方式? [英] How to determine the way data is stored in memory?

查看:62
本文介绍了如何确定数据存储在内存中的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑我已经定义了这样的整数。

int a = 5;

现在假设一个整数在内存中分配2个字节并且

内存地址从左向右增加,哪种方式

是5位存储的16位表示..

即15表示0000000000001111二进制吧?如果

内存地址从左到右方向增加每个内存

地址能够容纳8位(即1个字节)..你能不能告诉

存储的结构......就像这样???

00 00 00 00 00 11 11

^较小的地址^更大的地址。

或者是不同的格式?

请详细说明。

非常感谢您的耐心等待。

再见

解决方案

Abhishek写道:

让我们考虑我已经定义了一个整数这个。
int a = 5;
现在假设一个整数在内存中分配2个字节并且内存地址从左向右增加
方向是5位存储的16位表示..
即15是0000000000001111表示二进制对吗?如果内存地址从左向右增加,每个内存地址能够容纳8位(即1个字节)..你能告诉它存储的结构吗?它是这样的??? 00 00 00 00 00 00 11 11
^较小的地址^更大的地址。
或者是不同的格式?
请详细说明。
非常感谢您的耐心等待。<再见




没有什么可以详细说明的 - 这取决于您使用的

硬件。这些通常都是低价和高价的高端风味(低价与高价位相同,分别为
),但没有什么可以禁止的东西
weirder。


干杯


弗拉基米尔


>让我们考虑我已经定义了这样的整数。

int a = 5;
现在考虑一个整数分配的事实2字节


这是一种只在某些机器上才有的假设。


还要注意C标准否认并踩踏并吐出

假设"字节"相当于八位字节,虽然

有些实现可以做到这一点。
内存中的
和内存地址从左到右的方向增加,哪种方式


内存地址没有左字样或正确的或方向。

假设一个整数表示为big-endian

仅适用于某些机器。

是存储的5位16位表示..
即15是0000000000001111表示二进制对吗?如果
存储器地址从左向右增加,每个存储器地址能够保持8位(即1个字节)..可以使用u
u是UNIX中的结构V6内核。我不知道这里有什么相关的



告诉它存储的结构......就像这样? ?00 00 00 00 00 00 11 11
^较小的地址^更大的地址。


如果你声称每个字节存储两位,并且一个16位整数占用8个八位字节,那就不是真的了任何C

的实现。

或者是不同的格式?
请详细说明。
非常感谢你的耐心。



您描述的整数的二进制核心转储可能是:

$ b $ 00 00000000 00001111


ASSUMING:CHAR_BIT = 8,16位整数和bit-endian。

Gordon L. Burditt


Vladimir S. Oka写道:

Abhishek写道:

让我们考虑我已经定义了这样的整数。
int a = 5;
现在假设一个整数在内存中分配2个字节,并且内存地址从左到右方向增加,这种方式是5位存储的16位表示..
即15表示0000000000001111在二进制对吗?如果内存地址从左向右增加,每个内存地址能够容纳8位(即1个字节)..你能告诉它存储的结构吗?是这样的??? 00 00 00 00 00 00 11 11
^较小的地址^更大的地址。
或者是不同的格式?
请详细说明。
非常感谢您的耐心等待。<再见



没有什么可以详细说明的 - 这取决于你使用的硬件。这些通常都是低端和高端的口味(首先是低阶和高阶位),但是没有什么可以禁止的东西。
<欢呼

弗拉基米尔



对不起,发送太快了......


确定的方法你的机器如何存储数据:

将一个int变量初始化为具有不同高位和

低位的东西。使用char *指向它并在那里读出

值。如果在你的系统上char比int更窄,那么你很幸运,并且能够在读取的值上告诉基于字节序的

。如果sizeof int == sizeof char,你可以尝试

来使用long,而不是int,或者任何比char更宽的
的整数类型。


无论如何,练习应该很有趣...... ;-)


干杯


弗拉基米尔


lets consider that I have defined an integer like this.
int a=5;
now taking the fact that an integer is allocated 2 bytes in memory and
the memory address increases in the left to right direction, which way
is the 16bit representationof 5 stored..
i.e 15 is 0000000000001111 representation in binary right? if the
memory address increases in left to right direction with each memory
address being capable of holding 8bits (i.e 1 byte)..can u tellme the
structure in which it is stored..is it like this???
00 00 00 00 00 00 11 11
^lesser address ^greater address.
or is it in different format?
Please elaborate.
Thank you very much for ur patience.
Bye

解决方案

Abhishek wrote:

lets consider that I have defined an integer like this.
int a=5;
now taking the fact that an integer is allocated 2 bytes in
memory and the memory address increases in the left to right
direction, which way is the 16bit representationof 5 stored..
i.e 15 is 0000000000001111 representation in binary right? if
the memory address increases in left to right direction with
each memory address being capable of holding 8bits (i.e 1
byte)..can u tellme the structure in which it is stored..is it
like this??? 00 00 00 00 00 00 11 11
^lesser address ^greater address.
or is it in different format?
Please elaborate.
Thank you very much for ur patience.
Bye



There''s nothing really to elaborate -- it depends on the
hardware you''re using. These generally come in low- and
high-endian flavours (lower order vs higher order bits first,
respectivelly), but there''s nothing proscribing something
weirder.

Cheers

Vladimir


>lets consider that I have defined an integer like this.

int a=5;
now taking the fact that an integer is allocated 2 bytes
That is an assumption that is true only on some machines.

Also be aware that the C standard denies and stomps on and spits
on the assumption that "byte" is equivalent to "octet", although
some implementations do that.
in memory and
the memory address increases in the left to right direction, which way
Memory addresses do not have "left" or "right" direction.
The assumption that an integer is represented as big-endian
is only true on some machines.
is the 16bit representationof 5 stored..
i.e 15 is 0000000000001111 representation in binary right? if the
memory address increases in left to right direction with each memory
address being capable of holding 8bits (i.e 1 byte)..can
u u is a structure in a UNIX V6 kernel. I don''t see how it''s
relevant here.
tellme the
structure in which it is stored..is it like this???
00 00 00 00 00 00 11 11
^lesser address ^greater address.
If you are claiming that two bits are stored per byte, and that a
16-bit integer takes up 8 octets, that''s not true of any C
implementation.
or is it in different format?
Please elaborate.
Thank you very much for ur patience.



A core dump in binary of the integer you describe might be:

00000000 00001111

ASSUMING: CHAR_BIT = 8, 16-bit integer, and bit-endian.

Gordon L. Burditt


Vladimir S. Oka wrote:

Abhishek wrote:

lets consider that I have defined an integer like this.
int a=5;
now taking the fact that an integer is allocated 2 bytes in
memory and the memory address increases in the left to right
direction, which way is the 16bit representationof 5 stored..
i.e 15 is 0000000000001111 representation in binary right? if
the memory address increases in left to right direction with
each memory address being capable of holding 8bits (i.e 1
byte)..can u tellme the structure in which it is stored..is
it like this??? 00 00 00 00 00 00 11 11
^lesser address ^greater address.
or is it in different format?
Please elaborate.
Thank you very much for ur patience.
Bye



There''s nothing really to elaborate -- it depends on the
hardware you''re using. These generally come in low- and
high-endian flavours (lower order vs higher order bits first,
respectivelly), but there''s nothing proscribing something
weirder.

Cheers

Vladimir


Sorry, hit send too quickly...

The way to determine how your machine stores data:

Initialise an int variable to something with different high- and
low-order bits. Point to it using a char* and read out the
value there. If on your system char is narrower than an int,
you''re in luck, and will be able to tell the endianness based
on the value read. if sizeof int == sizeof char, you could try
to use long, instead of int, or any integer type that''s wider
than char.

In any case the exercise should be fun... ;-)

Cheers

Vladimir


这篇关于如何确定数据存储在内存中的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆