Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::cout fully operating on the stack?

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

like image 984
Mesop Avatar asked Apr 23 '12 17:04

Mesop


People also ask

What is the difference between std :: cout and cout?

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.

Why does std cout not work?

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.

Do I have to use std :: cout?

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.

Why is cout so slow?

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.


2 Answers

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).

like image 158
JaredPar Avatar answered Sep 17 '22 13:09

JaredPar


This completely depends on a certain implementation of the basic C++ libraries

like image 22
mikithskegg Avatar answered Sep 18 '22 13:09

mikithskegg