answersLogoWhite

0


Best Answer

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free.

dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

int x = 20;

// Using calloc/free:

char * c = calloc( x, sizeof( char )); // Allocate 20 bytes.

free( c ); // Deallocate.

// Using malloc/free:

char * m = malloc( x * sizeof( char )); // Allocate 20 bytes.

free( m ); // Deallocate.

// Using new/delete:

char * p = new char[ x ]; // Allocate 20 bytes.

delete [] p; // Deallocate.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

int stc; // static

int* dyn = new int; // dynamic

The difference between the two is that the former is allocated on the stack, in the program's data segement, while the latter is allocated on the heap, in free memory. The stack is very much smaller than the heap, thus the stack should only be used for small and simple variables that are used often but that do not change size (such as small static arrays), while the heap should be used for much larger structures, structures that vary in size (such as dynamic arrays), or temporary structures that are seldom required or highly volatile.

Note that both stc and dyn in the example above are allocated on the stack, but the memory pointed to by dyn is allocated on the heap. dyn simply stores the memory address of that allocation.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Dynamic memory allocation can be done using functions like malloc, calloc in C language.

The declaration of malloc is void* malloc(size_t).

Ex1. Dynamic memory allocation using malloc for an integer.

Steps

  1. Declare a pointer to data type to which you want to allocate memory dynamically. int*p;
  2. Find the size of the element for which you want to allocate the memory. Lets assume it's 10.
  3. Allocate memory using malloc, p = malloc(sizeof(int) * 10);
  4. Now p will hold the starting address of the dynamically allocated memory.

p= 100

----------------------------------------------------------

| | | | | | | | |

----------------------------------------------------------

100 104 108 112 116 120 124 128

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

yes, In C its possible to allocate array in expanded memory at run time

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

No.

If you have to for homework then do so. I/we have not been set such a task.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Static and dynamic memory allocation in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How does C Plus Plus handle dynamic memory allocation?

To allocate memory in C++ you use the new operator. To release the memory, you use the delete operator.double *myArray = new float [1000];//check and usedelete [] myArray;myClass *myClassInstance = new myClass;//check and usedelete myClassInstance;


How is dynamic initialisation achieved in c plus plus?

The C++ standard has this to say about dynamic initialisation:"Objects with static storage duration shall be zero-initialised before any other initialisation takes place. Zero-initialisation and initialisation with a constant expression are collectively called static initialisation; all other initialisation is dynamic initialisation."


Why does C plus plus allows static binding and not dynamic binding?

Dynamic binding, or late binding, is when the object code for the class does not get loaded into memory until it is needed. This saves time at module load time, at the cost of delayed execution for the first invocation.


What do you mean by free store in c plus plus?

The free store in any language refers to the heap. The three main areas of memory that all C++ programs use are the heap, the call stack and static memory. Static memory is allocated at compile time, is fixed-length and caters for all static variables, global variables and constant variables. Call stacks are also fixed-length and are allocated to threads of execution as they are instantiated (each thread has its own stack). The free store or heap is essentially all remaining memory accessible to our program. To use the heap we must request memory from the system as it is required and release it when we are finished with it.


Which binding in C plus plus is preferred and why?

There is no preference as such. The type of binding you use is more dependant upon the design and circumstance rather than any preference you may have. Static binding is certainly more predictable and therefore easier to program, but dynamic binding offers much greater flexibility.

Related questions

C plus plus uses dynamic memory management?

No, C++ does not use dynamic memory management. The programmer is entirely responsible for releasing dynamic memory when it is no longer required. When static objects fall from scope, their destructors are called automatically, but there is no automatic garbage collection for dynamic objects. Allocated memory remains allocated until the programmer manually releases it, or the thread that owns the memory is terminated.


Static vs dynamic binding in c plus plus?

Static binding occurs at compile time. Dynamic binding occurs at runtime.


What is memory leaking in c plus plus?

Memory leaking in C++, or in any language that supports dynamic memory allocation, is a failure to release memory when its use is no longer required. This causes the memory image of the process to grow, sometimes without bounds, ultimately causing process failure due to memory exhaustion.


How does C Plus Plus handle dynamic memory allocation?

To allocate memory in C++ you use the new operator. To release the memory, you use the delete operator.double *myArray = new float [1000];//check and usedelete [] myArray;myClass *myClassInstance = new myClass;//check and usedelete myClassInstance;


What are the storage allocation in C plus plus?

They mostly deal with pointers and new operators in memory.


How is dynamic initialisation achieved in c plus plus?

The C++ standard has this to say about dynamic initialisation:"Objects with static storage duration shall be zero-initialised before any other initialisation takes place. Zero-initialisation and initialisation with a constant expression are collectively called static initialisation; all other initialisation is dynamic initialisation."


What is the advantages of Dynamic Allocation in C plus plus?

Static allocations are allocated upon the stack, which has limited space. Dynamic allocations occur at runtime and are allocated on the heap, which is "virtually" unlimited (32-bit systems can only address 4GB of memory, at most, but 64-bit systems are only limited by available disk space). Thus the stack should be used for small data structures and local data that are used often in your code, but are fixed in size. The heap should be used for larger, more general purpose allocations or when the size of the structure is variable.


In c plus plus what is dynamic constructor?

There is no such thing as a dynamic constructor, but there is dynamic construction. struct A {}; foo () { A* x = new A; // dynamic construction. // use x... delete x; } The above instantiates x on the free store (the heap). Note that every call to new must be matched with a call to delete to release the memory resource. If we hadn't deleted x, then x would have fallen from scope when the function returned, but the instance of A would remain in memory and we'd have no way of recovering that memory until the program terminated. In general it is best not to use dynamic allocation like this as it's all too easy to forget to delete the resource when we're done with it. A better method is to a statically allocated smart pointer instead: foo () { unique_ptr<A> x; // static allocation // use x.... } Smart pointers are overloaded so they behave just as if they were raw pointers so you can use them just as you normally would a raw pointer, but you must not delete them. When the smart pointer falls from scope it will invoke A's destructor automatically, thus we no longer need to remember to delete the pointer. This technique is central to Resource Acquisition Is Initialisation (RAII).


Why does C plus plus allows static binding and not dynamic binding?

Dynamic binding, or late binding, is when the object code for the class does not get loaded into memory until it is needed. This saves time at module load time, at the cost of delayed execution for the first invocation.


Does c and c plus plus support static or dynamic binding?

Yes and no. Static vs dynamic binding is not a C or C++ language issue; it is a linker issue. If you link with a .lib file that contains stubs for run-time loading, then the called routine will not be loaded until it is invoked, and it will not be made a part of the load module.


What is memory allocation of class?

Classes are a declaration of a type and therefore do not allocate any memory apart from static members which exist throughout the life of the program (whether objects are instantiated from the class or not). Static member variables are allocated upon the stack, within the program's data segment, and should be listed in descending order of length to make best use of available memory (minimising padding resulting from alignment). When an object is instantiated from a class, memory is allocated according to the sum total of all non-static member variable lengths, plus padding for alignment purposes. As with static member variables, non-static member variables should be listed in descending order of length to make the best use of available memory. Classes that declare virtual methods (including pure-virtual methods) also set aside memory for the virtual table, the size of which is dependant upon the number of virtual methods. Classes that allocate memory during construction or by assignment or class setters will require additional memory, however this memory is not included in the size of the class itself (only the member pointer lengths are included in the class size).


What happens when class libraries leak memory in c plus plus?

A memory leak is when allocated memory that is no longer needed is not deallocated. Eventually, the memory pool is unable to satisfy an allocation request, and the program fails. A memory leak is a programming bug. When class libraries leak memory, they need to be fixed, just like any other piece of code that has bugs. If they came from a vendor, then that vendor needs to fix them.