Passing parameters by reference from C++ to x86 MASM [duplicate] - c++

This question already has answers here:
C++ inline assembly: how to deal with references?
(2 answers)
How does assembly do parameter passing: by value, reference, pointer for different types/arrays?
(3 answers)
Passing the reference arguments to an assembly function
(1 answer)
C++ assembler output - how are references implemented
(3 answers)
Closed 11 months ago.
I'm trying to pass parameters by reference from C++ to a function written in assembly, but I can't seem to actually change the value within the assembly function. What am I doing wrong here?
void __declspec(naked) asmChange(int&){
__asm{
mov [esp+4], 5
ret
}
}
int main (){
int a = 12;
cout << "Before: " << a << endl;
asmChange(a);
cout << "After: " << a << endl;
return 0;
}
In the program above I'm simply trying to change the value of a variable. I don't really understand what I'm doing wrong here and can't seem to find any answers to my issue anywhere else online.

Related

How does the compiler determine what value to output during runtime when a function name is sent to cout without parenthesis? C++ [duplicate]

This question already has answers here:
g++ "calling" a function without parenthesis (not f() but f; ). Why does it always return 1?
(2 answers)
Closed 2 years ago.
#include <iostream>
int returnFive()
{
return 5;
}
int main()
{
std::cout << returnFive << '\n';
return 0;
}
Since this compiles without error, how does the system determine what value is actually sent and printed to console?
Imagine if the code written is something like
if(returnFive)
returnFive();
Here the expectation is that the compiler checks if the function pointer for returnFive is nullptr or not.
The compiler here is evaluating the function pointer as a boolean expression of whether it is NULL or not and printing the output.
https://godbolt.org/z/Psdc69. You can check that the cout is being passed a (bool).

Value of const changed in the memory but not on the output [duplicate]

This question already has answers here:
Modifying a const through a non-const pointer
(6 answers)
Closed 2 years ago.
I'm trying to write a program that changes the value of a const variable. I know this shouldn't be done this in the first place and I'm doing this just to know how this happen.
I've already read other questions about this but I'm not trying to know how to do it but why it isn't happening in my program. This is the code I wrote:
int main()
{
const int a = 5;
int *pointer_a = const_cast<int*>(&a);
*pointer_a = 6;
// Address of a
std::cout << &a << std::endl;
// Prints 5 while memory says it is 6
std::cout << a << std::endl;
// Address that pointer points too
std::cout << pointer_a << std::endl;
// Prints 6
std::cout << *pointer_a << std::endl;
}
What I noticed while debugging this program is that in fact the value at the memory address of a gets updated. It does change the value of a from 5 to 6.
The IDE (Visual Studio) also shows the value of a to be 6 but when printing it to the console, it prints 5. Why does this happen when the current value on the memory is 6?.
It is undefined behaviour to modify a value which is initially declared as const.
https://en.cppreference.com/w/cpp/language/const_cast:
const_cast makes it possible to form a reference or pointer to
non-const type that is actually referring to a const object. Modifying
a const object through a non-const access path results in undefined
behavior.
try turning off compiler optimization flags (Project Properties, C/C++, Optimization)

Passed array inconsistent bahevior; C++ [duplicate]

This question already has answers here:
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 5 years ago.
I'm trying to learn some basic C++ and the moment I started to think I got a grasp of all those pointers I stumbled across this problem:
int sizeOf(string texts[]) {
return sizeof(texts);
}
int main() {
string texts[] = {"apple", "banana", "orange", "watermelon"};
cout << sizeof(texts) << endl;
cout << sizeOf(texts) << endl;
}
and this function returns
128
8
My question is: what is happening when I pass this array as an argument? Why suddenly C++ forgets that this is an array? I have tried to dereference it inside a method (return sizeof(*texts)) but it returned 32 instead which is the size of one string element, not a whole array. Is it possible to do what I want to do? Where am I mistaken?

Why --(-val) is not working? [duplicate]

This question already has an answer here:
Feeling confused with -(--a) vs --(-a) in c
(1 answer)
Closed 7 years ago.
int val = -10;
cout << ++(-val) << endl;
Output should be 11. But it gives me an error.
The error is "lvalue required as increment operand". This means that it's just a value; it does not represent a particular object like "val" anymore so trying to change it makes no sense to the compiler.

Std::cout function as parameter [duplicate]

This question already has answers here:
Why do function pointers all have the same value?
(4 answers)
Closed 8 years ago.
I recently noticed you can do stuff like
void foo(){ }
//...
std::cout<<foo<<std::endl;
This tends to print out "1", regardless of the passed function.
Now I'm curious: What exactly is this doing?
foo is decaying to a function pointer, which is then converted to a boolean with value true (because the function pointer is not null), which is printed as "1", because booleans are printed as numbers by default.
Try this:
std::cout << std::boolalpha << foo << std::endl;