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编程中做什么。
6 个解决方案
#1
16
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".
它是一个类型ast,并告诉编译器“忽略问题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). The typecast forces a different interpretation - the programmer is stepping in and saying "I know what I'm doing".
在这个例子中,problem3有一个函数指针类型,所以通常编译器会拒绝这个程序(当一个整数被期望的时候,使用一个函数指针通常是程序员的错误)。类型转换强制执行一种不同的解释——程序员介入并说“我知道我在做什么”。
#2
2
It's an explicit cast. You are casting the value of problem3 to an integer and then assigning that integer value to x.
这是一个显式的。您将问题3的值转换为一个整数,然后将该整数值分配给x。
Note that this does not actually change the value of problem3.
注意,这实际上不会改变问题3的值。
#3
1
It's a type cast - it's a form of converting the type of the operand (problem3
in your example) to another type.
它是一种类型转换——它是将操作数类型(在您的示例中是问题3)转换为另一种类型的一种形式。
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)
- 作为转换操作的一部分执行转换。例如,当将浮点数转换为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 - reinterpretation t_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 theconst
orvolatile
qualifiers that might be applied to an object - const_cast<>()是可以删除可能应用于对象的const或volatile限定符的强制类型转换的特殊情况
-
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. 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). 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).
在您的示例中,问题3是一个指向函数的指针,而cast是将函数的地址“转换”为int,然后将该地址作为int类型存储到数组中,但实际上是数组末尾的一个数组元素(这是不允许的)。在许多平台上,无效的数组元素是存储函数的返回地址,所以当问题3()函数返回时,它将返回自己并再次运行(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()不会有一个函数调用,返回地址栈上的——它会垃圾无论之前是在堆栈上,再次回到自身,重复这个过程,直到栈底流可能会导致一个处理器例外。
#4
0
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 ?
但是从你发布的代码来看,这个函数看起来像是在为某种缓冲区溢出做准备。这个函数的其余部分是什么?
#5
0
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(括号中的东西)
#6
0
It means that problem3
is converted to type int before assigning to the int x
这意味着问题3在赋值给int x之前被转换为int类型
#1
16
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".
它是一个类型ast,并告诉编译器“忽略问题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). The typecast forces a different interpretation - the programmer is stepping in and saying "I know what I'm doing".
在这个例子中,problem3有一个函数指针类型,所以通常编译器会拒绝这个程序(当一个整数被期望的时候,使用一个函数指针通常是程序员的错误)。类型转换强制执行一种不同的解释——程序员介入并说“我知道我在做什么”。
#2
2
It's an explicit cast. You are casting the value of problem3 to an integer and then assigning that integer value to x.
这是一个显式的。您将问题3的值转换为一个整数,然后将该整数值分配给x。
Note that this does not actually change the value of problem3.
注意,这实际上不会改变问题3的值。
#3
1
It's a type cast - it's a form of converting the type of the operand (problem3
in your example) to another type.
它是一种类型转换——它是将操作数类型(在您的示例中是问题3)转换为另一种类型的一种形式。
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)
- 作为转换操作的一部分执行转换。例如,当将浮点数转换为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 - reinterpretation t_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 theconst
orvolatile
qualifiers that might be applied to an object - const_cast<>()是可以删除可能应用于对象的const或volatile限定符的强制类型转换的特殊情况
-
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. 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). 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).
在您的示例中,问题3是一个指向函数的指针,而cast是将函数的地址“转换”为int,然后将该地址作为int类型存储到数组中,但实际上是数组末尾的一个数组元素(这是不允许的)。在许多平台上,无效的数组元素是存储函数的返回地址,所以当问题3()函数返回时,它将返回自己并再次运行(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()不会有一个函数调用,返回地址栈上的——它会垃圾无论之前是在堆栈上,再次回到自身,重复这个过程,直到栈底流可能会导致一个处理器例外。
#4
0
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 ?
但是从你发布的代码来看,这个函数看起来像是在为某种缓冲区溢出做准备。这个函数的其余部分是什么?
#5
0
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(括号中的东西)
#6
0
It means that problem3
is converted to type int before assigning to the int x
这意味着问题3在赋值给int x之前被转换为int类型