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.)
Related
This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 4 years ago.
#include <iostream>
int main() {
int* pt;
std::cout << pt << std::endl;
if(pt)
std::cout << "true" << std::endl;
else
std::cout << "false" << std::endl;
return 0;
}
This code fragment evaulate to false. What is more it prints 0 in
std::cout << pt << std::endl;
I thought that uninitialized pointers points to some random address, which may contain garbage value. In this case it points to 0 value like i would assign it to nullptr. Could someone clarify this for me?
This code invokes Undefined Behavior (UB), since the pointer is used uninitialized.
The compiler should emit a warning, when a warning flag is used, like -Wall for example:
warning: 'pt' is used uninitialized in this function [-Wuninitialized]
std::cout << pt << std::endl;
^~
It just happens, that at this run, on your system, it had the behavior of a null pointer. That means that the garbage value the pointer takes, happens to be 0. However, notice that, kernel zeroes appear relatively often.
This question already has answers here:
No out of bounds error
(7 answers)
Closed 1 year ago.
I'm working on dynamic arrays for my c++ course, but I'm confused about the behavior of my dynamic arrays. For example, if I run this code:
int* myDynamicArr = new int[3];
for (int i = 0; i < 10; i++)
{
myDynamicArr[i] = i + 1;
cout << myDynamicArr[i] << endl;
}
I would expect it to not work since I only declared it as size 3. But when I run it, it prints out 0-9. Same thing if I do this:
char* myCharArr = new char[2];
strcpy(myCharArr, "ThisIsALongString");
cout << myCharArr;
It prints the full string even though it seems like it should fail. Can anyone explain what I'm doing wrong here? Thanks!
C++ does not perform bounds checking on arrays. So when you read or write past the bounds of an array you trigger undefined behavior.
With undefined behavior, your program may crash, it may output strange results, or it may (as in your case) appear to work properly.
Just because it could crash doesn't mean it will.
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:
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)
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;