In C++, when I am using std::cout
like that:
std::cout << "myString" << std::endl;
Is there anything that will be allocated on the heap by std::cout? Or will std::cout do everything on the stack (meaning that std::cout and its underlying functions won't do any new/malloc/etc...
)?
I want to know if heavily using std::cout could cause some heap fragmentation
C++ std:cout: A namespace is a declarative region inside which something is defined. So, in that case, cout is defined in the std namespace. Thus, std::cout states that is cout defined in the std namespace otherwise to use the definition of cout which is defined in std namespace.
In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL.
cout and std::cout both are same, but the only difference is that if we use cout, namespace std must be used in the program or if you are not using std namespace then you should use std::cout.
As for why it is so "time consuming", (in other words, slow,) that's because the primary purpose of std::cout (and ultimately the operating system's standard output stream) is versatility, not performance.
In this specific example your code isn't causing any direct allocations on the heap. However it's possible for the implementation of any method to use the heap for part of it's work. This is perfectly fine so long as the method implementation properly cleans up after itself.
This logic applies to methods such as operator<<(std::ostream&, T)
.
This completely depends on a certain implementation of the basic C++ libraries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With