answersLogoWhite

0

There is no such thing as dynamic memory, but we often use the term to mean non-static memory. Static memory is where all constant variables, global variables and static variables are allocated in static memory. Static memory is a contiguous block of memory of fixed length divided into two sections: initialised memory and uninitialised memory. Initialised variables and constants are allocated to the initialised section and are stored as a bitmap image in the executable itself. Uninitialised variables are allocated in the uninitialised section which is typically a block of zero-initialised memory. No bitmap image is required for uninitialised memory, only its length need be stored in the executable. The compiler can determine the amount of memory required for each constant and variable.

Non-static memory is essentially all other memory used by a program. Every thread has its own call stack allocated as each thread is instantiated and released as each thread is destroyed. All stacks are fixed-length (determined at design time). The call stack is used to store local variables, automatic variables and temporary variables, as well as to implement exception handling.

The heap (or free store) is essentially all other memory available to the program and is used to instantiate all anonymous variables. The heap is what we generally mean by dynamic memory because anonymous variables are created and destroyed as and when they are required at runtime. Since we cannot refer to an anonymous variable by name (being anonymous means they have no name), we must refer to them solely by their address. This means we must use pointer variables to store those addresses. If we have a large number of anonymous variables of the same type to keep track of, then we typically allocate them in contiguous memory and maintain a single pointer to the start of the allocation. This single pointer allows us to access the memory just as we would a built-in array. We also need to keep track of how many variables (elements) are allocated in the array to avoid buffer overruns. This allows us to easily allocate and release any number of anonymous variables at runtime.

However, in C++ we rarely use "dynamic memory" directly. Although necessary in C Programming, C++ programmers prefer to use resource handles or "smart" pointers instead. In this way the language itself handles the underlying memory allocations for us, ensuring that memory is allocated and released in a timely manner, and thus minimising the risk of creating resource leaks -- a common problem with low-level C Programming. The C++ std::vector is an example of a resource handle and most non-trivial C++ programs will include one or more std::vector<std::unique_ptr<T>> variables to keep track of any number of pointers to type T objects.

What else can I help you with?

Related Questions

What is the difference between static memory versus dynamic memory?

Dynamic memory can be declared at run-time using the new and delete operators (or malloc and free in C), while static memory must be declared at compile-time.


What is the difference between static and dynamic fighting?

Static stays the same and dynamic is always different.


Difference between static memory devices and dynamic memory devices?

static memory devices are those which stores data permanently as long as power is applied. and dynamic memory devices are those in which data is not permanently stored even with power applied,unless data is periodically rewritten into memory.


Difference between dynamic and static memory?

dynamic is the study of motin,while static is at rest it means that the memory which is used at motin time as by usic calloc,mallaoc,free function ,on the other hand static is the memory which only used at compile time or at the time of work in during progrmming,static memory has drawback ,the drawback is that in this memory ware unable to use the excess memory as we allocate already to cover or improvre this type of problem we use dynamic memory allocation.


What is Difference between static and dynamic list?

A static one cannot change, while a dynamic one can.


Why main memory use dynamic memory rather than static memory?

Dynamic memory changes and static stays the same.


What is the difference between static pressure and dynamic pressure?

The main difference of static pressure and dynamic pressure is:- static pressure is exerted by fluid at rest but dynamic pressure is pressure exerted by fluid in motion.


What is the general difference between a static IP and a dynamic IP?

The general difference between a static IP and dynamic IP is that a static IP is reserved and does not change. A dynamic IP on the other hand changes each time one logs on.


What are the difference between static and dynamic hashing in DBMS?

Search operation in static hashing is time consuming, but in dynamic hashing it is not.


What is the difference between dynamic torque and static torque ratings?

One difference between dynamic torque and static torque is the level of difficulty to measure. Static torque is each to measure, while dynamic torque is not. This is because it requires a transfer of an electric or magnetic effect.


Difference between static multiplier dynamic multiplier?

Type your answer here...


What is static and dynamic memory?

A computer is built to contain two kinds of memory. There is static memory, which stays in the computer after it is powered off. Then there is dynamic memory, which is memory that is only accessible while the computer is on. The hard drive is an example of static memory, while RAM is an example of dynamic memory.