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.
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.
It's an explicit cast. You are casting the value of problem3 to an integer and then assigning that integer value to x.
Note that this does not actually change the value of problem3.
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".
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".
It means that problem3
is converted to type int before assigning to the int x
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)
As others have noted this is just explicit cast. It just changes type of variable into int
type.
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 ?
It's a type cast - it's a form of converting the type of the operand (problem3
in your example) to another type.
In C (and in C++ when a 'C-style cast is used), the cast can perform one of several things:
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:
reinterpret_cast<>()
which corresponds to the first form described abovestatic_cast<>()
which corresponds to the second form (even if the conversion doesn't result in a change of the internal data format)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 objectdynamic_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.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.
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).
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.