Binary file space allocation

If every record of a binary file is to be fixed length then shouldn't some space be wasted if variables are different size in a struct?
Where did you read such nonsense? In a COBOL book?

The smallest unit that can be stored in a file is a byte, 8 bits.
Last edited on
But each record should be fixed? so your answer suggests space wont be be wasted..I am learning new in class but confused
Last edited on
how a struct/record is written binary to a file depends on the compiler and settings.
Read this:

http://en.wikipedia.org/wiki/Data_structure_alignment
Possibly. If the records have strings then it's likely that the fixed length record allows some maximum number of characters in the string and it always allocates that maximum amount, regardless of whether they are all used. This is wasteful of space, but there may be other advantages of using the fixed length record (e.g., then n'th record is located at byte number n*record_size). A fixed length record is useful when the performance benefits outweigh the cost of the wasted space.
There are variable-length records in COBOL.
The term "wasted" is a bit of an imaginative stretch in this regard. I have a feeling that the article coder777 linked to explains this better, but a modern OS allocates pages of memory in blocks of a predefined size at a time. If you are even one byte over the page boundary of what is allocated to you then BOOM! You trigger a page fault and get allocated an entire new block of memory for that one piece of data.

There will always be allocated space that is not being put to productive use, especially in C++. But they are nothing to a modern system so don't sweat over it.
thanks guys
Topic archived. No new replies allowed.