
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the difference between new/delete and malloc/ free in C/ C++?
new/ delete
The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.
The delete operator is used to deallocate the memory. User has the privilege to deallocate the created pointer variable by this delete operator.
Here is an example of new/delete operator in C++ language,
Example
#include <iostream> using namespace std; int main () { int *ptr1 = NULL; ptr1 = new int; float *ptr2 = new float(299.121); int *ptr3 = new int[28]; *ptr1 = 28; cout << "Value of pointer variable 1 : " << *ptr1 << endl; cout << "Value of pointer variable 2 : " << *ptr2 << endl; if (!ptr3) cout << "Allocation of memory failed\n"; else { for (int i = 10; i < 15; i++) ptr3[i] = i+1; cout << "Value of store in block of memory: "; for (int i = 10; i < 15; i++) cout << ptr3[i] << " "; } delete ptr1; delete ptr2; delete[] ptr3; return 0; }
Output
Here is the output
Value of pointer variable 1 : 28 Value of pointer variable 2 : 299.121 Value of store in block of memory: 11 12 13 14 15
malloc/ free
The function malloc() is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. It returns null pointer, if fails.
The function free() is used to deallocate the allocated memory by malloc(). It does not change the value of pointer which means it still points the same memory location.
Here is an example of malloc/free in C language,
Example
#include <stdio.h> #include <stdlib.h> int main() { int n = 4, i, *p, s = 0; p = (int*) malloc(n * sizeof(int)); if(p == NULL) { printf("\nError! memory not allocated."); exit(0); } printf("\nEnter elements of array : "); for(i = 0; i < n; ++i) { scanf("%d", p + i); s += *(p + i); } printf("\nSum : %d", s); free(p); return 0; }
Output
Here is the output −
Enter elements of array : 32 23 21 8 Sum : 84
- Related Articles
- Difference between new and malloc( )
- How do malloc() and free() work in C/C++?
- malloc() vs new() in C/C++
- delete() and free() in C++
- Delete and free() in C++ Program
- What is the difference between a destructor and a free function in C++?
- What is malloc in C language?
- Difference Between malloc and calloc
- new and delete operator in C++
- Difference between "new operator" and "operator new" in C++?
- What is a malloc function in C language?
- What is the difference Between C and C++?
- What is difference between instantiating a C++ object using new vs. without new?
- What is the difference between MySQL TRUNCATE and DELETE command?
- What is the difference between | and || operators in c#?
