Binary file space allocation

Aug 26, 2014 at 6:29am
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?
Aug 26, 2014 at 6:50am
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 Aug 26, 2014 at 6:51am
Aug 26, 2014 at 7:14am
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 Aug 26, 2014 at 7:15am
Aug 26, 2014 at 10:40am
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
Aug 26, 2014 at 6:31pm
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.
Aug 26, 2014 at 6:39pm
There are variable-length records in COBOL.
Aug 26, 2014 at 7:22pm
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.
Aug 27, 2014 at 1:40am
thanks guys
Topic archived. No new replies allowed.