地址数据的大小(The size of data of an address)

编程入门 行业动态 更新时间:2024-10-26 22:27:25
地址数据的大小(The size of data of an address)

c语言中指针指向的地址包含多少数据(字节,2字节),或者它取决于指向的数据类型?

The address that pointed by a pointer in c language contains how much data (byte , 2 byte ) , or is it dependent on the data type that point to ?

最满意答案

我不完全确定你的意思是“指针指向的地址”。 我假设你指的是它指向的对象,而不是指针本身的大小。

您的问题的答案可能取决于您的“地址”的含义。

AC指针值不仅仅是一个原始内存地址(尽管通常以这种方式实现)。 指针值指的是特定类型的对象,该类型指定指向对象的大小。 并且C标准相当一致地使用“地址”一词来指代(非空)C指针值。

另一方面,“地址”一词通常用于表示原始存储器地址, 可以将其视为指向单个字节。 但另一方面,即使在机器代码级别,地址引用的数据大小也取决于您使用它做什么。 (我甚至研究过机器地址只能指向64位字的系统;字节操作完全由软件完成。)

int*类型的指针指向int对象。 根据定义,该对象的大小是sizeof (int)字节(通常为4个字节,但如果一个字节至少为16位,则可以是8,或2,甚至1)。 类似地, struct foo*类型的指针指向struct foo对象,该对象可以是任意大小。

int*指针不仅指向int对象的第一个字节,而是指向整个int对象。 (但是如果将int*指针转换为char* ,结果将指向“first”字节。)

作为一种特殊情况, void*类型的指针指向内存中的某个位置,但不指定它指向的对象的大小。 在将其转换为其他指针类型之前,不能取消引用它。

推荐阅读: comp.lang.c FAQ的第4部分。

I'm not entirely sure what you mean by "The address that pointed by a pointer". I'll assume you're referring to the object it points to, not to the size of the pointer itself.

The answer to your question can depend on just what you mean by "address".

A C pointer value is not just a raw memory address (though it's usually implemented that way). A pointer value refers to an object of a specific type, and that type specifies among other things, the size of the pointed-to object. And the C standard fairly consistently uses the word "address" to refer to a (non-null) C pointer value.

On the other hand, the word "address" is commonly used to refer to a raw memory address, which can be thought of as pointing to a single byte. But on the other hand, even on the machine code level, what size of data an address refers to can depend on what you do with it. (I've even worked on systems where a machine address can only point to a 64-bit word; byte operations were done entirely in software.)

A pointer of type int* points to an int object. The size of that object is, by definition, sizeof (int) bytes (commonly 4 bytes, but it could be 8, or 2, or even 1 if a byte is at least 16 bits). Similarly, a pointer of type struct foo* points to a struct foo object, which could be of just about any arbitrary size.

An int* pointer doesn't just point to the first byte of an int object, it points to the entire int object. (But if you convert an int* pointer to char*, the result will point to the "first" byte.)

And as a special case, a pointer of type void* points to some location in memory, but does not specify the size of the object it points to. You can't dereference it until you convert it to some other pointer type.

Recommended reading: Section 4 of the comp.lang.c FAQ.

更多推荐

本文发布于:2023-04-27 21:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   地址   数据   address   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!