SlideShare a Scribd company logo
1 of 13
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
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.
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.
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;
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[];
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
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.
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.
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;
}
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
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.
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.
ADK COLEGE.pptx

More Related Content

Similar to ADK COLEGE.pptx

C++ tutorial boost – 2013
C++ tutorial   boost – 2013C++ tutorial   boost – 2013
C++ tutorial boost – 2013Ratsietsi Mokete
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Salman Qamar
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTAkhilMishra50
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfmalavshah9013
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementAnil Bapat
 
GLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meetingGLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meetingArun Kumar
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2Prerna Sharma
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Process Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docxProcess Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docxstilliegeorgiana
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxesuEthopi
 

Similar to ADK COLEGE.pptx (20)

memory
memorymemory
memory
 
C++ tutorial boost – 2013
C++ tutorial   boost – 2013C++ tutorial   boost – 2013
C++ tutorial boost – 2013
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 
Pointers
PointersPointers
Pointers
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointers
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
16829 memory management2
16829 memory management216829 memory management2
16829 memory management2
 
See through C
See through CSee through C
See through C
 
GLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meetingGLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meeting
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
06 linked list
06 linked list06 linked list
06 linked list
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Process Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docxProcess Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docx
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptx
 

Recently uploaded

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

ADK COLEGE.pptx

  • 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.