I'm a newbie programmer, can you help me imagine what a stream is, is it a fixed array of bytes that transfer data from i.e: a file to Y? And what is Y here, a buffer or something else? In what way is the buffer related to stream?
-
https://stackoverflow.com/a/5144867/2850543 for streams – Millie Smith May 29 '19 at 04:30
-
Also, I did not vote to close this as too broad because I don't believe this is a broad question. It has a succinct, accurate answer. – Millie Smith May 29 '19 at 04:31
-
1The term "stream" is used for a variety of things, some of which are listed in [this wikipedia article](https://en.wikipedia.org/wiki/Stream_(computing)). – user3386109 May 29 '19 at 04:35
-
1@user3386109: It is rather easy to imagine what OP meant by "stream", especially in relation to "buffer", and under [tag:c] tag. – Amadan May 29 '19 at 04:36
-
1@Amadan Yes, did you read the article? It lists several examples of *"what a stream is"* which is what the OP asked. – user3386109 May 29 '19 at 04:39
-
@user3386109 it's stack overflow, why would i bother to ask about a stream of water or something else here? – Van Teo Le May 29 '19 at 04:42
-
@VanTeoLe You didn't read the article either, did you? That article doesn't say a single thing about water. – user3386109 May 29 '19 at 04:43
-
@user3386109: I did read it, but I believe OP would be confused by the variety of related meanings. It is obvious the meanings are related once one knows them, but at this moment I strongly believe OP should focus on one of them - the one relevant to his query - and only when they need to, to investigate how it relates to others. VanTeoLe - noone mentioned a stream of water. – Amadan May 29 '19 at 04:44
-
@user3386109 my fault sorry – Van Teo Le May 29 '19 at 04:47
-
@user3386109 If that was in response to me, then there's a one sentence definition of a stream in the page you linked (the first sentence). If not, then carry on. – Millie Smith May 29 '19 at 05:07
-
@MillieSmith Ok, I'll carry on. For the record, that comment was directed to the OP (specifically in response to *"can you help me imagine what a stream is"*), and was unrelated to your comments. – user3386109 May 29 '19 at 05:10
-
@user3386109 Sounds good. I definitely misinterpreted, so I'm sorry if you got a notification for my first message that I deleted (or if 28k rep lets you see deleted comments) – Millie Smith May 29 '19 at 05:16
-
@MillieSmith I think only the diamond mods can see deleted comments (I certainly can't). So no worries. – user3386109 May 29 '19 at 05:24
2 Answers
A stream is either a source (input stream) or sink (output stream) of data, that is available (or provided) in time (as opposed to all at once).
A buffer is an array (a piece of memory) that is used to store data temporarily. An input buffer is typically filled from an input stream by the OS; an output buffer (once filled by the programmer) is consumed by the OS.
Imagine you want to fill a tub with water. You start with a water source like a water tank or public waterworks that can be transfered through a water tap. You put a bucket under the water tap and turn it on. When the bucket is full, you dump it into the tub, and put it back under the tap. You repeat that until your tub is full.
Loading a file, for example, works almost the same way. You have a data source (the file on disk); you open an input stream (a programmatic construct that will give you data generally as fast as the disk can read them). You allocate a buffer (a small memory area) and tell the system to fill it from the stream. When it is full, you append it to the big chunk of allocated memory that you reserved for file contents, then let the buffer be filled again. When the whole file is read, you close the stream.

- 191,408
- 23
- 240
- 301
-
-
Pretty much, though it is limited to the output buffers. It is an imperfect analogy, but still quite close to reality. Also note that there are a variety of buffers - some you create yourself, some are under the hood and used by the OS, some are in hardware and you have no programmatic access to them. But the purpose is always the same - accumulate the dripping data till a process or a device is ready for it. – Amadan May 29 '19 at 04:47
Difference between a buffer and a stream is
A Stream is a sequence of bytes of data that transfers information from or to a specified source.
A sequence of bytes flowing into a program is called input stream. A sequence of bytes flowing out of the program is called output stream Use of Stream makes I/O machine independent.
A Buffer is a sequence of bytes that are stored in memory.
In C, I/O operations are asynchronous: you don’t know when you have data nor how much of it. So a buffer is usually used to collect data from the stream (file, socket, device). When the buffer is full, consumers of that stream are notified and can consume data from the buffer until is depleted. Then wait for the buffer to be filled again before using that data. It is a place to store something temporarily, in order to mitigate differences between the input speed and output speed. While the producer is being faster than the consumer, the producer can continue to store the output in the buffer. When the consumer speeds up, it can read from the buffer. The buffer is there in the middle to bridge the gap.
Y in your question can be a file, socket or a device(I/O).
Hope this solves your Query :)

- 109
- 1
- 6