1. ANANDIBAI DAMODAR KALE DEGREE
COLLEGE
STUDENT NAME :-
JIWANI DUBEY (17)
THE BOOL DATA TYPE,THE OPERATOR NEW AND DELETE
GUIDED BY:-
Department Of Information Technology
2. THE BOOL DATA TYPE
In computer programs, there are three types of data: text, numbers and Booleans. A Boolean data type is a value that
can only be either true or false.
A true Boolean value might indicate that the object is valid (e.g. an email address has been typed correctly). A false
Boolean value indicates that the object is invalid and has not been done correctly (e.g. you’ve forgotten to fill out a
required field).
Boolean values have two possible states: true and false. In binary, these are represented by 1 and 0.
Boolean algebra is a type of math that deals with operations on logical values, including binary variables. It is the
foundation for decisions in programs, so it’s important to understand how Booleans work.
3. New and delete operators in C++
Introduction
The new operator is used to dynamically allocate memory on the heap for an object or an array of objects. And
delete operator is usd to deallocate the memory.This article focuses on two very important operators :New and
delete operators in C++.These operators are explained with programmig example.
The new and delete operators are integral parts of every programming language. They allow the programmer to
create and delete variables in the program. The use of these operators is essential for creating and managing
different program elements. These are also helpful when debugging a program or removing invalid data from a
program. A programmer needs to know how to use these operators effectively to create clean and efficient
programs.In this article you will learn New and delete operators in C++ with programming examples.
4. New operator
The “new” operator in C++ is used to allocate memory dynamically for a
variable or an object at runtime. This means that the memory is allocated
during the execution of the program, as opposed to being allocated at compile
time. When the “new” operator is called, it reserves a block of memory that is
large enough to hold the object being created and then returns a pointer to the
first byte of that memory block.
Syntax
Here is the syntax of the new operator in C++ language.
1. Pointer_name=new datatype;
Here is the syntax to initialize the memory,
Example
int *ptr=new int;
5. 2. pointer_variable = new datatype(value);
Here is the syntax to allocate a block of memory.
Example
int *ptr=new int(10);
This means you have given 10 as the Value to the pointer ptr.
3. pointer_variable = new datatype[size];
Example
int *ptr=new int[];
6. This means the pointer will point to the base address of an array
For example, the following code dynamically allocates memory for an integer and assigns the value 42 to it:
int *p = new int; //allocate memory for an int
p = 42; //assign value 42 to the memory location
Let’s understand it with example.
#include <iostream>
int main() {
int* pInt = new int; // dynamically allocate memory for an int
*pnt = 5; // store the value 5 in the allocated memory
std::cout << *pnt; // output the value stored in the allocated memory
delete pnt; // deallocate the memory to prevent memory leak
return 0;
}
Output: 5
7. The new operator is used to dynamically allocate memory for the variable on
the heap, which is a region of memory that remains allocated until explicitly
deallocated using the delete operator. The pointer pnt is used to access the
memory location where the int is stored.
8. Delete operator
The delete operator is used to deallocate memory that was previously allocated on the
heap using new. It takes a pointer to the memory to be deallocated as an argument.
For example:
delete p; // Deallocates the memory pointed to by p
The “delete” operator is used to deallocate memory that the “new” operator
previously allocated. Once a block of memory has been allocated by “new,” it is
important to deallocate it when it is no longer needed so that other parts of the
program can reuse the memory. The “delete” operator releases the memory back to
the system, and other parts of the program can use it.
Note: Use delete to deallocate memory allocated with new to avoid memory leaks.
Memory leaks occur when the program allocates memory dynamically but does not
deallocate it properly. This causes the program to consume more memory gradually,
eventually leading to poor performance or even crashing the program.
9. New and delete operators example
#include <iostream>
int main()
{
int* ptr1 = new int; // dynamically allocate memory for an int
*ptr1 = 5; // store the value 5 in the allocated memory
float *ptr2 = new float(20.324);
int *ptr3 = new int[28];
std::cout << "Value of pointer variable 1 : " << *ptr1<<std::endl;
std::cout << "Value of pointer variable 2 : " << *ptr2<<std::endl;
if (!ptr3)
std::cout << "Allocation of memory failedn";
else {
for (int i = 1; i < 15; i++)
ptr3[i] = i+1;
std::cout << "Value of store in block of memory: ";
for (int i = 1; i < 15; i++)
std::cout << ptr3[i] << " ";
}
std::cout << *ptr1; // output the value stored in the allocated memory
delete ptr1;// deallocate the memory to prevent memory leak
delete ptr2;
delete ptr3;
return 0;
}
10. Output
Value of pointer variable 1 :
5
Value of pointer variable 2 :
20.324
Value of store in block of memory:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 5
11. Explanation of code
The first line uses the “new” operator to allocate memory dynamically for an integer variable and assigns the allocated memory address to
the pointer variable ptr1. The value 5 is stored in the allocated memory using the pointer variable ptr1.
The second line uses the new operator to allocate memory for a float variable dynamically, and assigns the allocated memory address to
the pointer variable ptr2. The value 20.324 is also assigned to this pointer.
The third line uses the new operator to dynamically allocate memory for an array of 28 integers and assigns the allocated memory address
to the pointer variable ptr3.Then the code uses std::cout to output the values stored in the allocated memory for the pointer variables
ptr1, ptr2, and ptr3.
In the final lines of the code, the delete operator is used to deallocate the memory allocated for the pointer variables ptr1, ptr2, and ptr3
to prevent memory leaks.
It’s worth noting that in the case of the array (ptr3), the code uses the if (!ptr3) statement to check if the memory allocation was successful
or not, otherwise it will store values in the array and output the values stored in the array using a for a loop.
12. Dynamic Memory Allocation
When the size of the data structure needs to change at runtime:
For example, if a program needs to store a large number of items in an array, and the number of
items is not known at compile time, dynamic memory allocation can be used to create an array of
the appropriate size.
When working with complex data structures: Dynamic memory allocation can be used to create
linked lists, trees, and other complex data structures that require a flexible amount of memory.
When working with polymorphic objects: Dynamic memory allocation is often used to create objects
of different types at runtime. For example, a program might use dynamic memory allocation to
create objects of different classes inherited from a common base class.
When working with large data sets: Dynamic memory allocation can handle large data sets that
cannot fit in the stack memory.
When memory is being used very efficiently: Dynamic memory allocation can ensure that the
program uses only as much memory as it needs, rather than allocating a fixed amount of memory at
compile time.
It’s worth noting that dynamic memory allocation could lead to poor performance if not used
carefully. It could cause memory leaks if the allocated memory is not deallocated properly.