Preview

Dynamic Memory Allocation

Good Essays
Open Document
Open Document
425 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Dynamic Memory Allocation
Dynamic Memory Allocation

Every Data or Instruction to be processed must be loaded into internal memory before its processing takes place. This process is called Memory allocation. These are of two types: Static and Dynamic Memory Allocation.

First let’s talk about Static memory allocation. Here we reserve a certain amount of memory by default inside our program to use for variables, etc and this static memory is allocated during the compilation of the Program. Once we reserve this memory, no other program can use it, even if we are not using it at the time.

But many times we are not aware in advance about how much memory we will need to store particular information in a defined variable. The size of required memory may be determined only during the run time , i.e. when the program is being run.

Here Dynamic memory comes into play. By using Dynamic memory allocation, the programmer can allocate memory to a program during run time.
The program uses its allocated memory only when it is active. In its inactive state, the program will use much less memory, hence allowing other programs to take up the unused memory. You can even de allocate memory so as to free up memory for other programs, but we need to do this manually.

In C++, an operator called new is used for Dynamic memory allocation. To allocate a single variable dynamically, we use the following syntax:

int *data = new int; // dynamically allocate an integer named data.

The new operator returns the address of the variable that has been allocated. This address can be stored in a pointer, and the pointer can then be dereferenced to access the variable.

int *data = new int; // dynamically allocate an integer
*data = 7; // assign 7 to this integer

When we are done with a dynamically allocated variable, we need to explicitly tell C++ to free the memory for reuse. This is operation is done via the scalar (non-array) form of the delete operator:

delete data; // deallocate memory

You May Also Find These Documents Helpful

  • Good Essays

    Some OS routines directly support application programs as they run and thus must be resident. Other transient routines are stored on disk and read into memory only when needed. Fixed-length partitions can also be used to allocate the set amount of memory that a particular program needs to run. Under dynamic memory management, the transient area is treated as a pool of unstructured free space. When the system decides to load a particular program, a region of memory just sufficient to hold the program is allocated from the pool. Using segmentation, programs are divided into independently addressed segments and stored in noncontiguous memory. Paging breaks a program into fixed-length pages.…

    • 7085 Words
    • 29 Pages
    Good Essays
  • Satisfactory Essays

    SD1230 Lab 1

    • 239 Words
    • 2 Pages

    1. Why is virtual memory addresses used for applications? – So it can have its own address space on the memory.…

    • 239 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    RAM is the random access memory and it is volatile. It holds program instructions and data. The CPU can find…

    • 204 Words
    • 1 Page
    Satisfactory Essays
  • Good Essays

    A. the CPU tells the RAM which address holds the data that the CPU wants to read…

    • 957 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    Garbage Collector in C#: Runtime has a Garbage Collector Which cleans the objects that are not in use.…

    • 485 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    It 240 Appendix F

    • 301 Words
    • 2 Pages

    | |way or even be deleted. It is a permanent form of memory that’s generally used |…

    • 301 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Comp 220

    • 1463 Words
    • 6 Pages

    Pointers are, essentially, address variables, or variables that hold as their value the address of other variables. In terms of memory management, they are very powerful devices, and they more closely and efficiently use the actual internal hardware registers of the microprocessor that the program operates on.…

    • 1463 Words
    • 6 Pages
    Good Essays
  • Satisfactory Essays

    POS355 Week 1 Individual

    • 574 Words
    • 2 Pages

    There are several items that are pertinent to memory management such as, basic hardware, the binding of symbolic memory addresses to definite physical addresses and the difference between logical and physical addresses. The most important task that memory management executes is the distribution and collection of memory…

    • 574 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Random-access-memory (RAM) is a very important specification of the computer. By ensuring you have enough RAM, the faster your system processes, you can store more data, and allow more programs to run at the same time. As the OS allows the different task to complete for the processor, the RAM is used as temp storage, when the processor no longer needs them the OS clears it. Virtual memory is the hard drive space borrowed to use if there isn’t enough…

    • 580 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Revision Gcse Ict

    • 1310 Words
    • 6 Pages

    * Memory: RAM (random access memory), the higher the RAM the more programs you can access and open at once.…

    • 1310 Words
    • 6 Pages
    Good Essays
  • Good Essays

    Memory management is an important characteristic of an operating system. Main memory is divided into two parts one for resident programs, and the other for the program currently in use. The user part must be subdivided to accommodate multiple processes (Stallings, 2012). When a few processes are in the memory then much of the time all of the processes will have to wait for input /output and the processor will be idle. According to Stallings (2012),. The requirements that memory management is supposed to satisfy are Relocation, Protection, Sharing, Logical organization, and Physical…

    • 573 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    It250 Exam Review

    • 856 Words
    • 4 Pages

    5. (Page 34)Where are programs and data temporarily stored when there is not enough RAM to hold all the information it is processing? Swap file…

    • 856 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Nt1110 Computer Memory

    • 781 Words
    • 4 Pages

    8. CD ROM – A device used to read CD-ROMs. If capable of writing to the…

    • 781 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    For any operating system to function properly, one of the vital tasks it must be able to do is manage memory. When a program runs on a computer, it first must be loaded into memory before it can execute. There are five different requirements memory management must satisfy in order to execute the program so it runs without errors or corruption. These requirements are relocation, protection, sharing, logical organization, and physical organization.…

    • 730 Words
    • 3 Pages
    Good Essays
  • Good Essays

    1. When determining the appropriate hardware components of a new information system, what role must the user of the system play?…

    • 1937 Words
    • 8 Pages
    Good Essays