简体   繁体   English

C编程中的(int)是什么意思

[英]what does (int) mean in C programming

void problem3(void) {
    int overflowme[16];
    int x = (int) problem3; // x is the address of the first instr for problem3
    printf("hello world\n");
    overflowme[17] = x; 

I'm wondering what does the (int) do in C programming. 我想知道(int)在C编程中做了什么。

It's a typecast, and tells the compiler "Ignore the type that problem3 really has, and deal with it as if it were typed as an int". 这是一个类型转换,告诉编译器“忽略问题3真正具有的类型,并将其作为一个int类型处理”。

In this example, problem3 has a function pointer type, so normally the compiler would reject the program (Using a function pointer when an integer is expected is normally a programmer error). 在此示例中,problem3具有函数指针类型,因此通常编译器将拒绝该程序(当期望整数时使用函数指针通常是程序员错误)。 The typecast forces a different interpretation - the programmer is stepping in and saying "I know what I'm doing". 类型转换强迫一种不同的解释 - 程序员踩着并说“我知道我在做什么”。

It's an explicit cast. 这是一个明确的演员。 You are casting the value of problem3 to an integer and then assigning that integer value to x. 您将problem3的值转换为整数,然后将该整数值分配给x。

Note that this does not actually change the value of problem3. 请注意,这实际上并不会更改problem3的值。

It's a type cast - it's a form of converting the type of the operand ( problem3 in your example) to another type. 它是一个类型转换 - 它是将操作数的类型(在您的示例中为problem3 )转换为另一种类型的形式。

In C (and in C++ when a 'C-style cast is used), the cast can perform one of several things: 在C(和C ++中使用'C风格的强制转换)时,强制转换可以执行以下几种操作之一:

  • do nothing but change the type of something without changing the form of the data. 什么也不做,只改变某些东西的类型,而不改变数据的形式。 For example, when you cast a pointer to an int. 例如,当您将指针强制转换为int时。
  • perform a conversion as part of the cast operation. 执行转换作为强制转换操作的一部分。 For example, when casting a float to an int, the data is actually transformed from the form used to represent floating point values (usually an exponent/mantissa form) to a plain old integer (with any fractional part lost) 例如,当将float转换为int时,数据实际上是从用于表示浮点值(通常是指数/尾数形式)的表单转换为普通的旧整数(丢失任何小数部分)

Because the different forms of casting can be confusing or unclear as to what's happening (or intended to happen), C++ added several specific casting operators: 因为不同形式的转换可能会令人困惑或不清楚发生了什么(或打算发生什么),C ++添加了几个特定的​​转换运算符:

  • reinterpret_cast<>() which corresponds to the first form described above reinterpret_cast<>() ,它对应于上述第一种形式
  • static_cast<>() which corresponds to the second form (even if the conversion doesn't result in a change of the internal data format) static_cast<>()对应于第二种形式(即使转换不会导致内部数据格式发生变化)
  • const_cast<>() which is a special case of casting that is able to remove the const or volatile qualifiers that might be applied to an object const_cast<>()这是一个特殊的转换案例,它能够删除可能应用于对象的constvolatile限定符
  • dynamic_cast<>() which is entirely new to C++ and has no similar functionality in C. This operator is used to safely 'downcast' a base object type to one of its derived types. dynamic_cast<>() ,它对C ++来说是全新的,在C中没有类似的功能。此运算符用于将基础对象类型安全地'向下'转换为其派生类型之一。

Because they're inherently dangerous, casts are generally considered bad form. 因为它们天生就是危险的,所以演员通常被认为是不好的形式。 When you perform a cast operation, you're subverting the compiler's ability to perform type checking. 执行强制转换操作时,您将破坏编译器执行类型检查的能力。 However, there are times when it might be necessary or very useful, and you'll see it used often in C code. 但是,有时候它可能是必要的或非常有用的,你会看到它经常在C代码中使用。

In your example, problem3 is a pointer to a function, and the cast is 'converting' the address for that function to an int. 在您的示例中, problem3是指向函数的指针,并且problem3转换将该函数的地址“转换”为int。 It's then storing that address-as-int into the array, but actually one array element past the end of the array (which is a no-no). 然后它将address-as-int存储到数组中,但实际上是一个数组元素超过了数组的末尾(这是一个禁忌)。 On many platforms that invalid array element is where the return address for the the function is stored, so what will happen is when the problem3() function returns, it'll return to itself and run again (ad-infinitum - sort of). 在许多平台上,无效的数组元素是存储函数的返回地址的位置,所以会发生什么是当problem3()函数返回时,它将返回自身并再次运行(ad-infinitum - sort of)。

It'll eventually underflow the stack because the new, 'hacked' run of problem3() won't have a function call that put a return address on the stack - it'll just trash whatever else was on the stack before it and return to itself again, repeating the process until the stack underflows which will likely cause a processor exception. 它最终会使堆栈下溢,因为问题problem3()的新的'黑客'运行将没有一个函数调用,它将一个返回地址放在堆栈上 - 它只会problem3()堆栈之前的任何其他内容并return再一次,重复该过程,直到堆栈下溢,这可能会导致处理器异常。

这意味着在分配给int x之前, problem3被转换为int类型

It's a typecast ie. 这是一个类型,即。 it converts the variable/constant following it into the specified type. 它将后面的变量/常量转换为指定的类型。 Here, a void (*) (void) type is converted into an int (thing in the braces) 这里,void(*)(void)类型被转换为int(大括号中的东西)

As others have noted this is just explicit cast. 正如其他人所指出的那样,这只是明确的演员 It just changes type of variable into int type. 它只是将变量类型更改为int类型。

But from code you posted it looks like this function is preparing for some kind of buffer overflow or something. 但是从你发布的代码看起来这个函数正在为某种缓冲区溢出做准备。 What is the rest of this function ? 这个功能的其余部分是什么?

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

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