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;
Related
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.
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).
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?
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.
This question already has answers here:
Why does std::cout output disappear completely after NULL is sent to it
(3 answers)
Does "cout<<(char*)NULL" doing "close(1)" here? [duplicate]
(3 answers)
Closed 8 years ago.
If I write this code:
const char * b = NULL;
std::cout << "Hello!" << b;
I get this output:
Hello!
However, if I change the order to:
const char * b = NULL;
std::cout << b << "Hello!";
I get no output whatsoever, so I'm curious about it.
I can guess that probably the << operator for cout reads until a NULL, so everything after it is ignored, but wonder if someone could give me some more insight about it.
The operator has a precondition:
Requires: s shall not be a null pointer.
By breaking this precondition, you cause undefined behaviour. Anything could happen.
(As mentioned in the comments, one popular implementation sets failbit and badbit in this case, which would explain the behaviour you see. But that's not something you can rely on.)