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
Write a c program to determine interchanged values between two variables?[Hint - if loop] What will be the output of the program? How many times this loop will execute? Wr

I want Skype Recorder Application + Setup + Sourcecode + NICE UI Project Description: i want an application built which will allow user to record skype audio or video calls

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Write a C program "team.c" that maintains information for a soccer team. The program will allow you to add and delete players from your team, to search your team for players by nam

Use the program called harris-shell.c and add some code to find the corners in the image checkers.jpg. You should say that a pixel in the image is a corner if it passes the given t

write c language program that shows stack overflow..

Problem #1: plugging the memory leak. When you employ the "normal" new operator, for example Foo* p = new Foo(), the compiler generates some special code to manage the case while t

Q: But operator overloading makes class look ugly; isn't it assumed to make my code clearer? A: Operator overloading makes life simpler for the users of a class, not for develop

Write a program that keeps record of football results. Program stores following information about each match: Description Type and/or size name of home team max. 25 characters name

Introduction. In this assignment you are required to revisit the Assignment 1 topic. You will develop a new program which is more sophisticated, in particular, adding more functio