(Please excuse me if i got the title incorrect, I believe it is binding but if anything let me know and I will edit the title/question)
Learning about different ways to bind data in programming (No specific language) we went over 5 types. (I gave the type, with the definition given in class, under it will be my questions)
1) Static: subscript ranges are statically bound and storage allocation is static (before run-time)
- To start, I never understood what it is said by, “storage allocation is static” meaning it happens before run time. Now my thought is that, memory is allocated during a program so how does storage get allocated before even running a program? this is one question that has been bugging me the most. When reading about how memory is allocated before run time makes no sense. I can compile a program, and never run it for 2 weeks, so how does it allocate memory before that? As you can see I am missing something.
2) Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time
I want to be sure my example is correct, in c++ you can’t do (tested on VS 2015) int size = 100; int data[size];
but this works
const int size = 100; int data[size];
my guess would be that it is “fixed” due to the fact the const is seen during compile time so the program knows the size before hitting that statement, is this correct?
3) Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time)
- this is the one example my prof never gave us, any anyone provide me with one?
4) Fixed Heap-dynamic: similar to fixed stack-dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack)
if i am correct this is an example of this
int * x = new int [size];
due to the fact that, the allocated memory is on the heap, it is dynamic allocated memory, and the fixed part comes into fact that the size cannot grow, correct?
I want to think you for your time, sorry if these questions may be elementary but after asking people, and googling I get mixed answers and not sure what to believe
5) Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times
- my prof used an ArrayList in c# as an example since it can dynamically grow,but this only confused be because I thought an arraylist is just a class that is working with a primitive array. if array gets larger, it just garbage collects the memory and allocates a larger one and move elements into it which would make it fixed heap-dynamic?
Advertisement
Answer
“Before run-time” means that the memory is allocated when an instance of the program is created (when you choose to start a program, the operating system does this), but before the execution of the program starts.
Yes, you are correct that the value of a const int
is known at compile time, therefore the compiler can use it to know the size of the array at compile time.
If you use a variable instead, you would have to allocate the array dynamically at run-time.
“Stack-dynamic” means that the variables are created at a function call, such as the parameters of the function. They are run-time and temporary and exist on the stack. Their size is not known at compile-time.
Your example of “fixed heap dynamic” is probably not correct, becuase I assume you’re implying a variable, not a constant, to state the size of the array. “Fixed” is known at compile time, so you need to use a constant/literal. “Fixed heap dynamic” is an array of fixed size, but allocated dynamically on the heap.
“Heap-dynamic” doesn’t mean that the size can change, but that the size is only known at run-time. Arrays cannot naturally change size, since you cannot guarantee that there is any more contiguous memory next to it that it can grow into. Making an array larger normally means you have to make a new, larger array, and copy the old contents into it, and delete the old, smaller array.