Skip to content

Commit a3888eb

Browse files
committed
info on memory allocation for buffers
1 parent 2602b7d commit a3888eb

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

buffers-and-streams/buffers.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ using `Buffer.from()`.
4242

4343
Buffers can also be instantiated by size using `Buffer.alloc()`, `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()`
4444

45+
// The next line should be an 'aside', how to do this in markdown and the rendered site?
46+
4547
> _Unsafe_ as the memory containing the buffer is not initialised - i.e. not zeroed-out, so the potential exists for sensitive data to be leaked.
4648
4749
```
@@ -57,4 +59,28 @@ console.log(myBuffer2);
5759
const myBuffer3 = Buffer.alloc(3, 'a');
5860
console.log(myBuffer3);
5961
// <Buffer 61 61 61>
60-
```
62+
```
63+
64+
### Caveat: Buffer size
65+
66+
Once instantiated, using either `from()` or one of the `alloc()` methods a Buffer cannot be resized.
67+
68+
A Buffer's size is measured in Octets which is a more accurate way of saying 'an 8-bit byte'.
69+
70+
```
71+
const myBuffer4 = Buffer.alloc(4);
72+
console.log(myBuffer4);
73+
// <Buffer 00 00 00 00>
74+
myBuffer4.write('card');
75+
console.log(myBuffer4);
76+
// <Buffer 63 61 72 64>
77+
myBuffer4.write('cards');
78+
console.log(myBuffer4);
79+
// <Buffer 63 61 72 64> - last character is lost
80+
```
81+
82+
## Terminology
83+
84+
**Octet**
85+
86+
An Octet is a more accurate way to describe a byte consisting of 8-bits. In some systems a byte can have more or less bits.

0 commit comments

Comments
 (0)