简体   繁体   English

这行在C99中是什么意思?

[英]What does this line mean in C99?

static int* p= (int*)(&foo);

I just know p points to a memory in the code segment. 我只知道p指向代码段中的内存。 But I don't know what exactly happens in this line. 但是我不知道这条线到底发生了什么。
I thought maybe it's a pointer to a function but the format to point a function is: 我以为这可能是指向函数的指针,但是指向函数的格式为:

returnType (*pointerName) (params,...);
pointerName = &someFunc; // or pointerName=someFunc;

You take the address of foo and cast it to pointer to int . 您将foo的地址转换为指向int指针。

If foo and p are of different types, the compiler might issue a warning about type mismatch. 如果foop是不同类型,则编译器可能会发出有关类型不匹配的警告。 The cast is to supress that warning. 演员是为了抑制那个警告。

For example, consider the following code, which causes a warning from the compiler ( initialization from incompatible pointer type ): 例如,考虑以下代码,这会引起编译器警告( initialization from incompatible pointer type ):

float foo = 42;
int *p = &foo;

Here foo is a float , while p points to an int . 这里foo是一个float ,而p指向一个int Clearly - different types. 显然-不同的类型。

A typecasting makes the compiler treat one variable as if it was of different type. 类型转换使编译器将一个变量视为不同类型。 You typecast by putting new type name in parenthesis. 通过在括号中放置新的类型名称来进行类型转换。 Here we will make pointer to float to be treated like a pointer to int and the warning will be no more: 在这里,我们将使float的指针被视为指向int的指针,并且警告将不再存在:

float foo = 5;
int *p = (int*)(&foo);

You could've omitted one pair of parenthesis as well and it'd mean the same: 您也可以省略一对括号,这意味着相同:

float foo = 5;
int *p = (int*)&foo;

The issue is the same if foo is a function. 如果foo是一个函数,则问题是相同的。 We have a pointer to a function on right side of assignment and a pointer to int on left side. 我们在赋值右侧有一个指向函数的指针,在左侧有一个指向int的指针。 A cast would be added to make a pointer to function to be treated as an address of int . 将添加强制类型转换以使指向函数的指针被视为int的地址。

A pointer of a type which points to an object (ie not void* and not a pointer to a function) cannot be stored to a pointer to any other kind of object without a cast, except in a few cases where the types are identical except for qualifiers. 指向对象的类型的指针(即非void *而不是函数的指针)在没有强制转换的情况下无法存储为指向任何其他类型的对象的指针,除非在少数情况下类型相同对于预选赛。 Conforming compilers are required to issue a diagnostic if that rule is broken. 如果违反该规则,则要求合格的编译器发出诊断。

Beyond that, the Standard allows compilers to interpret code that casts pointers in nonsensical fashion unless code aides by some restrictions which, as written make such casts essentially useless, for the nominal purpose of promoting optimization. 除此之外,该标准还允许编译器以无意义的方式解释以强制方式转换指针的代码,除非代码辅助了某些限制,这些限制在书面上使此类强制转换实质上无用,以促进优化的名义目的。 When the rules were written, most compilers would probably do about half of the optimizations that would be allowed under the rules, but would still process pointer casts sensibly since doing so would cost maybe 5% of the theoretically-possible optimizations. 编写规则时,大多数编译器可能会执行规则所允许的大约一半的优化,但仍会明智地处理指针转换,因为这样做可能会花费理论上可能的优化的5%。 Today, however, it is more fashionable for compiler writers to seek out all cases where an optimization would be allowed by the Standard without regard for whether they make sense. 但是,如今,对于编译器作者来说,寻找所有允许标准进行优化的情况变得无关紧要,而不论它们是否有意义。

Compilers like gcc have an option -fno-strict-aliasing that blocks this kind of optimization, both in cases where it would offer big benefits and little risk, as well as in the cases where it would almost certainly break code and be unlikely to offer any real benefit. 诸如gcc之类的编译器具有-fno-strict-aliasing选项,该选项会阻止这种优化,无论是在提供大收益而几乎没有风险的情况下,还是在几乎肯定会破坏代码且不太可能提供的情况下任何真正的好处。 It would be helpful if they had an option to block only the latter, but I'm unaware of one. 如果他们可以选择仅阻止后者,那将是有帮助的,但是我不知道一个。 Thus, my recommendation is that unless one wants to program in a very limited subset of Dennis Ritchie's language, I'd suggest targeting the -fno-strict-aliasing dialect. 因此,我的建议是,除非有人想用Dennis Ritchie的语言中非常有限的一部分进行编程,否则我建议使用-fno-strict-alias方言。

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

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