Skip to content

Use of static array to take a value from user #1

@kingadarsh

Description

@kingadarsh

A static array gets memory allocation in the stack, that is they get what they want during compilation.

But dynamic arrays get memory allocation in the heap, that is they get what they want during runtime.

So this is the first thing that you should keep in mind, that since an array is meant to have a fixed size*, so it’s always better to allocate them statically.

Now let me clear the statement that I just said, as we know the heap memory is fixed for a program, and an array memory elements are contiguously allocated in the memory.
There might be a situation that can arise where the heap might have sufficient memory for x elements but not in a contiguous manner, at that time the array won’t be allocated(since the array is a collection of contiguous memory blocks ) and we have to use other approaches like Linked lists. So this is why instead of getting into this kind of trouble, why not assign it statically and get the desired memory at the compile time.

Note - The above situation that I discussed is not likely to happen(I also never seen such kind of stuff happening) but that doesn’t mean that we should avoid a good practice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions