Memory management operator, C/C++ Programming

Memory Management Operator

In C malloc( ), calloc( ), realloc( ), and free( ) are used to mange dynamic memory.  In

addition to these function C++ have derived two unary operators new and delete to perform dynamic allocation of memory.   An object or variable may be created or destroyed by using these functions.  The syntax for to create a dynamic memory allocation is

pointer_variable = new data_type;

 

int *p;

p = new int;

The above two statements can be written in one stroke or line. int *p = new int;

*p = 25; Assigning 25 through pointer

 

It can also be initialized in one stroke or line. The syntax to initialize the new operator is:

pointer_variable = new data_type (value);

int *p = new int(25);

 

The new operator can also be used in array

int *pa = new int[10];  This will create memory space for 10 integers the first element is

referred as p[0].....p[9];

 

When using multi dimension arrays all the array sizes must be specified, the first array may be variable the remaining must be constants.

pm = new int[N][3][3];

To release the memory the syntax is delete pointer_variable;

delete p; will release the memory stored by the pointer variable p. delete [ ]pa; will release dynamically allocated array.

Manipulators:

The most widely used manipulators are endl and setw.

 

cout <<"This is C++ program"<< endl; The endl work just like "\n"

 

The setw operator is used to specify the width of the output.  It is similar to %wd, %w.pf in C. The default is right justified

int x=10, y=1000;

cout<

 

 

Posted Date: 9/20/2012 7:37:24 AM | Location : United States







Related Discussions:- Memory management operator, Assignment Help, Ask Question on Memory management operator, Get Answer, Expert's Help, Memory management operator Discussions

Write discussion on Memory management operator
Your posts are moderated
Related Questions
Explain the working of Array with Strings? A string is defined in C language as an array of characters. A null character (‘\0') is automatically added to the end of the charact

Return by reference: The return value of the function must be an address variable.  In following program the function returns the value of x or y and their types are declare

Classes and Objects A class is a vehicle to execute the OOP features in the C++ language. Once a class is declared, an object of that type can be explained. An object is said t

Yes. "Memory pools" are useful in many situations. The bad news is that I'll need to drag you through the mire of how it acts before we talk about all the uses. Firstly, recall

full coding for lcm in c++

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of p using a simulation method called "Monte Carlo". The following fi

my program does not run more than 9 iterations when ever i want to iterate more than 100 iterations. what is the reason and how to tackle?

Is it possible to pass an entire structure to functions? Yes, it's possible to pass an entire structure to a function in a call by method style. Some programmers prefer to decl

Environment - C, Linux Duration - December 2006 to Feb 2007 Description - TR-106 specifies a generic data model template applicable for all TR-069 enabled devices. Adde

write a program to find area of curve y=f(x) between x and x=b,integrate between the limits a and b using c     #include float start_point, /* GL