不使用C转换指针会导致问题吗? [英] Not casting pointers in C can cause problems?

查看:64
本文介绍了不使用C转换指针会导致问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我在上课,在某个时候,讲师正在谈论C代码.他说:

Yesterday I was in class, and at some point the instructor was talking about C code. He said:

在C语言中强制转换指针的目的是什么?唯一的目的 是使编译器正确解释指针操作 (例如,添加一个int指针将导致不同的偏移量 而不是添加char指针).除此之外,没有区别:所有指针在内存中都以相同的方式表示,无论指针指向的是int值,char值,short值还是其他值.因此,强制转换指针不会修改内存中的任何内容,只会帮助程序员进行与其所处理的指针类型更相关的操作.

What is the purpose of making a pointer cast in C? The only purpose is to make the compiler interpret correctly the pointer operations (for example, adding an int pointer will result in a different offset than adding a char pointer). Apart from that, there is no difference: all pointers are represented the same way in memory, regardless if the pointer is pointing to an int value, a char value, a short value, or whatever. So, casting a pointer will not modify anything in the memory, it will just help the programmer with operations more related with the pointer-type he is dealing with.

但是,我在堆栈溢出中特别读到了这不是100%正确的信息.我读过一些奇怪的机器中,不同类型的指针可以以不同的方式存储在内存中.在这种情况下,如果将代码编译到这种机器上,则不将指针更改为正确的类型可能会导致问题.

However, I have read, specially here in Stack Overflow, that this is not 100% true. I have read that in some weird machines, pointers for different types can be stored in different ways in memory. In this case, not changing the pointer to the correct type could cause problems if the code is compiled to this kind of machine.

基本上,这是我正在谈论的那种代码.考虑下面的代码:

Basically, this is the kind of code I'm talking about. Consider the code below:

int* int_pointer;
char* char_pointer;
int_pointer = malloc(sizeof(int));
*int_pointer = 4;

现在有两个选择:

1.

char_pointer = (char *)int_pointer;

2.

char_pointer = int_pointer;

案例2的代码可能会出现问题?进行强制转换(第1种情况)最终会更改内存中的指针格式(如果是,您能举个机器的例子吗?)?

The code on case 2 could became a problem? Making the cast (case 1) would eventually change the pointer format in memory (if yes, could you give an example of machine?)?

谢谢

推荐答案

您的讲师关于共享相同表示形式的所有指针类型的说法通常适用于C语言的现实实现.

What your instructor said about all pointer types sharing the same representation is generally true for real-life implementations of C language.

但是,从抽象C语言本身的角度来看,这是不正确的. C语言保证

However, it is not true from the point of view of abstract C language itself. C language guarantees that

  1. char *指针的表示方式与void *指针相同.
  2. 指向类型为T的合格版本(constvolatilerestrict)的指针的表示方式与指向不合格T的指针的表示方式相同.
  3. 所有结构类型的指针都用相同的方式表示.
  4. 所有联合类型的指针都用相同的方式表示.
  1. char * pointers are represented in the same way as void * pointers.
  2. Pointers to a qualified version of type T (const, volatile, restrict) are represented in the same way as pointers to unqualified T.
  3. Pointers to all struct types are represented identically.
  4. Pointers to all union types are represented identically.

仅此而已.不存在其他担保.这意味着就语言本身而言,int *指针与double *指针具有不同的表示形式.指向struct S的指针与指向union U的指针的表示方式有所不同.

That is all. No other guarantees exist. Which means that as far as the language itself is concerned, int * pointer has different representation from double * pointer. And pointer to struct S is represented differently from pointer to union U.

但是,如前所述,您使用char_pointerint_pointer的示例并不能完全说明问题. char_pointer = int_pointer;分配完全无效(如不编译"中所述).语言不支持在不兼容的指针类型之间进行隐式转换.此类转换始终需要一个显式的强制转换运算符.

However, your example with char_pointer and int_pointer, as presented, is not exactly illustrative. The char_pointer = int_pointer; assignment is simply invalid (as in "does not compile"). Language does not support implicit conversions between incompatible pointer types. Such conversions always require an explicit cast operator.

这篇关于不使用C转换指针会导致问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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