Hacker News new | past | comments | ask | show | jobs | submit login

Why not just:

char waste[however-many-bytes-are-needed];

?




That's allocated in a different segment.


It is now. Was it then? AFAICT from https://en.wikipedia.org/wiki/PDP-11 none of the PDP-X machines had a segmented architecture.


Even so, https://sourceware.org/binutils/docs/as/PDP_002d11_002dPseud....

  9.34.2 Assembler Directives
  The PDP-11 version of as has a few machine dependent
  assembler directives.
  
  .bss
  Switch to the bss section.

  ...


Can't say for sure if it's the reason in this particular situation, but that would require dynamic memory allocation, and while it may seem backwards to reserve more memory than potentially needed on old memory-constrained systems, it is a pattern you see very frequently because on those systems, you actually know exactly how much memory you have. As a result, you know exactly how much memory you can "waste".

Especially on single-tasking systems, it doesn't matter how much memory you waste, because no other program is affected by it.

Or in other words: If you know you are guaranteed to have x KB of memory available and no other program can steal it, and you know that you don't need more than y KB, then why allocate up to y KB dynamically when you can just straight-up allocate y KB statically, which is less work?


IMO the way C programmers avoid dynamic memory allocation because it's so painful is probably the best feature of the language.


External arrays are static, they do not involve any dynamic allocation.


It's not clear from the question whether GP just wonders about the strange syntax used to reserve the space or about allocating a fixed amount of memory. The "however-many-bytes-are-needed" part sounded to me like they thought that a dynamic amount of memory should be allocated.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: