Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Memory Management Operators

By Sripriya R | on October 12, 2007 |
C++ Tutorials

Need for Memory Management operators

The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant.

In programming there may be scenarios where programmers may not know the memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used to handle this situation in C++ programming language.

What are memory management operators?

There are two types of memory management operators in C++:

  • new
  • delete

These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.

New operator:

The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.

General syntax of new operator in C++:

The general syntax of new operator in C++ is as follows:

pointer variable = new datatype;

In the above statement, new is a keyword and the pointer variable is a variable of type datatype.

For example:

int *a=new int;

In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable a holds the address of memory space allocated.

Dynamic variables are never initialized by the compiler. Therefore, the programmer should make it a practice to first assign them a value.

The assignment can be made in either of the two ways:

  1. int *a = new int;
  2. *a = 20;

or

  1. int *a = new int(20);

delete operator:

The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.

General syntax of delete operator in C++:

The general syntax of delete operator in C++ is as follows:

delete pointer variable;

In the above example, delete is a keyword and the pointer variable is the pointer that points to the objects already created in the new operator. Some of the important points the programmer must note while using memory management operators are described below:

  • The programmer must take care not to free or delete a pointer variable that has already been deleted.
  • Overloading of new and delete operator is possible (to be discussed in detail in later section on overloading).
  • We know that sizeof operator is used for computing the size of the object. Using memory management operator, the size of the object is automatically computed.
  • The programmer must take care not to free or delete pointer variables that have not been allocated using a new operator.
  • Null pointer is returned by the new operator when there is insufficient memory available for allocation.

Example:

To understand the concept of new and delete memory management operator in C++:

  1.  #include <iostream>
  2. using namespace std; 
  3. void main()
  4. {
  5. 	//Allocates using new operator memory space in memory for storing a integer datatype
  6. 	int *a= new int;
  7. 	*a=100;
  8. 	cout << " The Output is:a= " << *a;
  9. 	//Memory Released using delete operator
  10. 	delete a;
  11.  
  12. }

The output of the above program is

In the above program, the statement:

int *a= new a;

Holds memory space in memory for storing a integer datatype. The statement:

*a=100

This denotes that the value present in address location pointed by the pointer variable a is 100 and this value of a is printed in the output statement giving the output shown in the example above. The memory allocated by the new operator for storing the integer variable pointed by a is released using the delete operator as:

delete a;

« « C++ Void Pointer and Null Pointer
C++ Dereference Operator » »

Author Description

Avatar

Free Training

RSSSubscribe 391 Followers
  • Popular
  • Recent
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Objects and Classes

    August 14, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Multidimensional Arrays

    September 4, 2007 - 0 Comment
  • C++ Variables Scope in Functions

    October 17, 2006 - 0 Comment
  • How to Access C++ Class Members

    August 21, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Structure

    August 19, 2007 - 0 Comment
  • C++ Inheritance

    August 19, 2007 - 0 Comment
  • C++ Standard Input Output Stream

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions
  • C++ Friend Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT