There are a few questions on here about how to use memset() in C or what a buffer is. But I have yet to find a good explanation for why it is necessary to use and what it actually does for your program.
I saw in this program that was trying to print out a rotating 3D cube that they used. I am struggling to see what this actually does for the program.
memset(buffer, backgroundASCIICode, width * height);
Would love any help trying to explain what a buffer is, and how memset() is used to create one.
memsetis a C-standard function fromstring.hwith the prototype:memset(void *s, int c, size_t n);It takes a pointer to a memory location, or buffer, in the form of
void *s, a 1-byte value in the form ofint c, and a number of bytes to be set in the form ofsize_t n.The function writes the value in
cinton-bytes starting at the memory addresssFor instance the code snippet:
...allocates a buffer large enough for 10
ints, and then writes zeroes in every byte in that buffer.In the example you give,
backgroundASCIICodeis being copied intowidth * heightbytes beginning at the pointerbuffer.Generally speaking a buffer is any memory region that you have access to via a pointer to the first byte, which is why
memsettakes a pointer to the buffer via avoid*, since it doesn't care what kind of data is stored there, since it's just going to overwrite each byte.