C/C++ C++ Pointers and the Flush function

AI Thread Summary
Pointers are essential in programming as they allow for indirection, enabling access to memory locations without duplicating data. Instead of creating copies of large data structures, pointers provide a way to reference the original data efficiently. This is particularly useful for functions that need to manipulate large buffers, as it conserves memory and improves performance.The flush function in C++ is used to clear the output buffer, sending any stored characters to the console immediately rather than waiting for the buffer to fill up. This is important because it reduces input/output overhead, ensuring that output appears on the screen without delay. By using flush, developers can control when data is displayed, which can be crucial in scenarios where timely feedback is necessary.
ineedhelpnow
Messages
649
Reaction score
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
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

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