Hmmm so if I wrote int 7 int 255 int 1 int 65535 and then a 1000 character long string (can I even write mixed types to a "grow" buffer?) would this cause an issue?
You can write mixed data to grow buffers with no issues. You are only limited to a specific data type when using the
fast buffer type. Source (and potentially helpful for further reading):
Guide To Using Buffers
Basically, as long as you can ensure that whatever receives the buffer data will be able to read it properly, you're good to go, since you can (or have to) specify how many bytes to read/write when performing operations on buffers. In the above example, you'll know that the buffer's format is:
1 byte (7)
1 byte (255)
1 byte (1)
2 bytes (65535)
1000 bytes (string) + 1 byte (null terminator of the string)
You could even have the string be variable length, as long as it contains no null bytes and the receiver will interpret the null byte as the end of the string. This also means you can have multiple strings of variable length.
The thing with mixing data like this is that it's a lot more error-prone than working with fixed alignments. Reading a byte too much? Oops, the value you just read as well as the entire rest of your data will now be read as garbage.
That kind of stuff won't happen if you use a fixed alignment for writing and reading. It may "waste" more space than necessary, but whether that matters depends on what you're using this for.
I generally recommend to start out by making things
work first - then, if optimization will have substantial benefits, you can think about how to optimize it and will probably instantly notice if you messed up somehow. Starting out by packing everything as tightly as possible can make it difficult to pinpoint at which point things are going wrong, though, especially when operating on the "bit" level where you can't tell one zero apart from another without context.
Another thing to keep in mind is that while it's certainly
possible to mix data of different sizes, it may be overall easier and more comfortable to split the data into two separate buffers.
Would this make the buffer excessively large, because the ints are padding to accommodate the string size?
That depends on the buffer's alignment. If you set it to an alignment of 1, no automatic padding will be applied, and you essentially have the endless and gapless stream of data I described in my previous post.
Note that using mixed-size data with an alignment of 1, and therefore having no padding/gaps, means that you have to ensure that the receiver of a buffer knows its format. There's no way to tell whether "11111111 11111111 11111111" is supposed to be (255, 255, 255), (255, 65535), (65535, 255) or (16777215) just by looking at the buffer's contents.
This would be a different story if the buffer had an alignment of 2 and had the contents "11111111 11111111 00000000 11111111". This is unmistakably (65535, 255).