C++ Pointers and the Flush function

In summary, pointers are useful for indirect addressing and avoiding unnecessary copying of large data. As for the flush function, it allows for immediate output of text instead of waiting for a full line to be stored in memory.
  • #1
ineedhelpnow
651
0
I need some help understanding some things.
One, I understand WHAT pointers do but how are they useful and how/when are they necessary?
Two, what is the purpose of the flush function? This is the definition that I've been given: "The << flush forces cout to flush any characters in its buffer to the screen before doing each task, otherwise the characters may be held in the buffer until after a later task completes." I'm not able to really understand it. Can someone clarify?
 
Technology news on Phys.org
  • #2
For your first question, read up on the concept of indirection, that is, addressing an object by some piece of information other than the object itself. For instance, if I want to visit your house, I am not going to contract workers and engineers to build a perfect replica of your house next to mine, check it out, and then ask them to destroy it when I'm done. You just give me your address instead and I can go there myself. Here your home address is a kind of "pointer". Similarly, if you have a function that needs to access a large buffer in memory, you don't make a copy of the buffer and give it to the function, that would be a waste, you just tell the function where to find the buffer in memory (= a pointer to the buffer).

There are other use cases for pointers, which you will eventually encounter if you keep programming. Needless to say, they are important.

For your second question, each time you actually write some text to the console there is some significant amount of input/output overhead. So the text you send with cout or whatever is not sent to the console character by character, instead it is stored into a fast memory buffer until a full line is stored (by default; for other types of files it could instead wait for a certain number of characters) and that line is then sent to the console all in one shot. In this context "flush" simply means to directly send whatever is in the buffer directly to the console without waiting for it to be ready to be sent.
 

FAQ: C++ Pointers and the Flush function

What are pointers in C++?

Pointers in C++ are variables that hold memory addresses instead of values. They are used to indirectly access and manipulate data stored in memory.

How do you declare a pointer in C++?

To declare a pointer in C++, you use the asterisk symbol (*) before the variable name, followed by the data type of the value it will point to. For example, "int *ptr" declares a pointer named "ptr" that will point to an integer value.

What is the purpose of the flush() function in C++?

The flush() function in C++ is used to clear the output buffer and display any pending data on the screen. It is helpful when you want to see the output of a program immediately without waiting for the buffer to fill up.

How do you use pointers to dynamically allocate memory in C++?

To dynamically allocate memory using pointers in C++, you use the "new" keyword followed by the data type and the number of elements to allocate. This will return a pointer to the first element in the allocated memory block. For example, "int *arr = new int[5]" allocates memory for an array of 5 integers and assigns the memory address to the pointer "arr".

What is the difference between NULL and nullptr in C++?

NULL and nullptr are both used to represent a null pointer in C++, but they have different data types. NULL is typically defined as 0 and is an integer type, while nullptr is a keyword that represents a pointer literal and is of the type "std::nullptr_t". In newer versions of C++, nullptr is preferred over NULL for null pointers.

Similar threads

Replies
40
Views
3K
Replies
8
Views
1K
Replies
23
Views
2K
Replies
9
Views
2K
Replies
1
Views
1K
Replies
8
Views
2K
Replies
11
Views
5K
Back
Top