0% found this document useful (0 votes)
12 views11 pages

01 03 Memory

The document explains memory management in C and C++, focusing on dynamic memory allocation using malloc(), calloc(), new, and deallocation using free() and delete. It details the syntax and usage of these operators, highlighting differences between them, such as the fact that new and delete are operators specific to C++, while malloc() and free() are standard library functions. Additionally, it discusses the implications of memory management, including the concept of dangling pointers and the importance of proper memory deallocation.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

01 03 Memory

The document explains memory management in C and C++, focusing on dynamic memory allocation using malloc(), calloc(), new, and deallocation using free() and delete. It details the syntax and usage of these operators, highlighting differences between them, such as the fact that new and delete are operators specific to C++, while malloc() and free() are standard library functions. Additionally, it discusses the implications of memory management, including the concept of dangling pointers and the importance of proper memory deallocation.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Memory Management Operators

C language, we use the malloc() or calloc() functions to allocate the memory


dynamically at run time, and free() function is used to deallocate the dynamically
allocated memory.

C++ also supports these functions, but C++ also defines unary operators such
as new and delete to perform the same tasks, i.e., allocating and freeing the
memory.

New operator
A new operator is used to create the object while a delete operator is used to delete
the object. When the object is created by using the new operator, then the object will
exist until we explicitly use the delete operator to delete the object. Therefore, we can
say that the lifetime of the object is not related to the block structure of the program.

Syntax

1. pointer_variable = new data-type

The above syntax is used to create the object using the new operator.

Example 1:
1. int *p;
2. p = new int;

In the above example, 'p' is a pointer of type int.

Example 2:

1. float *q;
2. q = new float;

In the above example, 'q' is a pointer of type float.

In the above case, the declaration of pointers and their assignments are done
separately. We can also combine these two statements as follows:

1. int *p = new int;


2. float *q = new float;

Assigning a value to the newly created object


Two ways of assigning values to the newly created object:

o We can assign the value to the newly created object by simply using the assignment
operator. In the above case, we have created two pointers 'p' and 'q' of type int and
float, respectively. Now, we assign the values as follows:
1. *p = 45;
2. *q = 9.8;

We assign 45 to the newly created int object and 9.8 to the newly created float object.

o We can also assign the values by using new operator which can be done as follows:

1. pointer_variable = new data-type(value);

Let's look at some examples.

1. int *p = new int(45);


2. float *p = new float(9.8);
How to create a single dimensional array
As we know that new operator is used to create memory space for any data-type or
even user-defined data type such as an array, structures, unions, etc., so the syntax
for creating a one-dimensional array is given below:

1. pointer-variable = new data-type[size];


Examples:
1. int *a1 = new int[8];

In the above statement, we have created an array of type int having a size equal to 8
where p[0] refers first element, p[1] refers the first element, and so on.

Delete operator
When memory is no longer required, then it needs to be deallocated so that the
memory can be used for another purpose. This can be achieved by using the delete
operator, as shown below:

1. delete pointer_variable;

In the above statement, 'delete' is the operator used to delete the existing object,
and 'pointer_variable' is the name of the pointer variable.

In the previous case, we have created two pointers 'p' and 'q' by using the new
operator, and can be deleted by using the following statements:

1. delete p;
2. delete q;

The dynamically allocated array can also be removed from the memory space by
using the following syntax:

1. delete [size] pointer_variable;


In the above statement, we need to specify the size that defines the number of
elements that are required to be freed. The drawback of this syntax is that we need to
remember the size of the array. But, in recent versions of C++, we do not need to
mention the size as follows:

1. delete [ ] pointer_variable;

malloc() vs new in C++

Both the malloc() and new in C++ are used for the same purpose.
They are used for allocating memory at the runtime.
But, malloc() and new have different syntax.
malloc() is a standard library new is an operator
function that is predefined in
a stdlib header file
What is new?

 The new is a memory allocation operator,

 which is used to allocate the memory at the runtime.

 The memory initialized by the new operator is allocated in a heap.

Syntax of new operator


Enter the
number :15
Entered

What is malloc()?

A malloc() is a function that allocates memory at the runtime.


This function returns the void pointer, which means that it can be assigned to any
pointer type. This void pointer can be further typecast to get the pointer that points to
the memory of a specified type.

The syntax of the malloc():


If we do not use the free() function at the correct place, then it can lead to the cause
of the dangling pointer.

Dangling pointer is a pointer pointing to a memory location that has been freed (or
deleted).

free vs delete in C++

free() function

 The free() function is used in C++ to de-allocate the memory dynamically.


 It is basically a library function used in C++, and it is defined in stdlib.h header
file.
 This library function is used when the pointers either pointing to the memory
allocated using malloc() function or Null pointer.

Syntax of free() function


Suppose we have declared a pointer 'ptr', and now, we want to de-allocate its
memory:

Delete operator

 It is an operator which is used to de-allocate the memory dynamically.


 This operator is mainly used either for those pointers which are allocated using a
new operator or NULL pointer.

Syntax

If we allocate the memory to the pointer using the new operator, and now we want to
delete it.

To delete the array

 It is either used to delete the array or non-array objects which are allocated by
using the new keyword.

 we use delete[] and delete operator


 The new keyword allocated the memory in a heap; therefore, we can say that the
delete operator always de-allocates the memory from the heap
 It does not destroy the pointer, but the value or the memory block, which is
pointed by the pointer is destroyed.
free() function works with a calloc
OP Ponter is NULL
delete operator.
how delete works with an array of objects.
Differences between delete and free()

The delete is an operator that de- the free() is a function that destroys the
allocates the memory dynamically memory at the runtime

The delete operator is used to delete the free() function is used to delete the
the pointer, which is either allocated pointer that is either allocated using
using new operator or a NULL pointer malloc(), calloc() or realloc() function or
NULL pointer.

the delete operator destroys the the free() function does not call the
allocated memory, then it calls the destructor; it only frees the memory from
destructor of the class in C++, the heap.

The delete() operator is faster than the free() function.

You might also like