The audacity to do such a thing…

  • RyeMan@lemmy.ml
    link
    fedilink
    arrow-up
    13
    ·
    1 year ago

    In lower level languages like C/C++ the reason becomes much more apparent when you learn about memory allocation and management (as a bonus it also really helps to understand how OS’s handle memory). Dynamically declaring variables in a loop would mean you need to allocate a chunk of memory for each variable that’s generated on the fly, most of, if not all of the dynamically declared variables would not even use most of their allocated memory resulting in a ton of extra overhead and wasted space within memory. An array is usually the answer when someone asks how to dynamically define variables. With an array you allocate the space needed in memory and can iterate across it block by block resulting in more control and efficiency within your reserved memory block. Linked lists are also a fun thing to look into when you aren’t sure how big your array needs to be. It’s a hard question to answer in a 100 level class because the answer actually goes pretty deep into low level programming, operating system and hardware principles.