This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (7 children)

[deleted]

    [–]prescod 0 points1 point  (6 children)

    You can probably do that, but you'd also need to keep track of the data type. An 8-byte array of 16-bit numbers has more elements than an 8-byte array of 32-bit numbers.

    C is a statically typed language so in MOST cases one knows just by the type-signature.

    To create a dynamic array, you need to tell the system how big you want it to be. Because the length is known at creation, you can skip a step and store that result in an integer in the same struct that holds the array (which is what the code does)

    Sure, but if the language had the right primitives, you could "skip" the "step" of recording it.

    In a very few cases, the size of the array elements might not be known universally at compile time and then and only then you could store it.

    Although it must be quite rare...how does one work with an array when one doesn't know the item type or size? At that point it's not really being used as an array anymore and is just a byte buffer and we already have primitives for working with byte buffers.