While C and C++ share many similarities, there are some key differences in how pointers are used and managed between the two languages. This tutorial highlights these differences, excluding function pointers, to help you understand how pointers behave in C++ compared to C.
C++ builds on the foundation of C, adding new features and improvements. While the basic concept of pointers remains the same, C++ introduces additional mechanisms like references, smart pointers, and type-safe memory management that make working with pointers safer and more efficient.
In C, pointers are declared and initialized as follows:
int x = 10;
int *ptr = &x; // ptr holds the address of x
In C++, the syntax is the same, but C++ encourages the use of nullptr
instead of NULL
for null pointers:
int x = 10;
int *ptr = &x; // ptr holds the address of x
int *nullPtr = nullptr; // C++ style null pointer
nullptr
as a type-safe alternative to NULL
. nullptr
is of type std::nullptr_t
and avoids ambiguities that can arise with NULL
(which is typically defined as 0
).In C, dynamic memory allocation is done using malloc
, calloc
, and free
:
int *ptr = (int *)malloc(sizeof(int)); // Allocate memory
*ptr = 10;
free(ptr); // Free memory
In C++, dynamic memory allocation is done using new
and delete
:
int *ptr = new int; // Allocate memory
*ptr = 10;
delete ptr; // Free memory
For arrays:
int *arr = new int[5]; // Allocate memory for an array
delete[] arr; // Free memory for an array
new
and delete
are type-safe and do not require explicit type casting.new
calls the constructor for objects, and delete
calls the destructor.malloc
and free
do not call constructors or destructors.C does not have references. Pointers are the only way to indirectly access variables.
C++ introduces references, which are aliases for variables. References are safer and easier to use than pointers in many cases:
int x = 10;
int &ref = x; // ref is a reference to x
ref = 20; // Modifies x
cout << x; // Output: 20
nullptr
and must be initialized when declared.C does not have smart pointers. Memory management is entirely manual using malloc
and free
.
C++ introduces smart pointers to automate memory management and prevent memory leaks:
unique_ptr
: Manages a single object with exclusive ownership.shared_ptr
: Manages an object with shared ownership (reference counting).weak_ptr
: A non-owning pointer that works with shared_ptr
.Example:
#include <memory>
using namespace std;
int main() {
unique_ptr<int> ptr = make_unique<int>(10); // Automatically freed
cout << *ptr << endl; // Output: 10
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
struct A {
int x;
A() : x(0) {
cout << "A constructor (default)\n";
}
A(int x) : x(x) {
cout << "A constructor(" << x << ")\n";
}
~A() {
cout << "A destructor(" << x << ")\n";
}
};
int main() {
A a;
A b(10);
a.x++;
A* p = &a;
p->x += 10;
A* c = new A(100);
// a dangling memory reference you need to delete it using delete c;
unique_ptr<A> ptr = make_unique<A>(1000);
cout << ptr->x << endl;
return 0;
}
While C and C++ share the same basic concept of pointers, C++ introduces several enhancements and additional features that make working with pointers safer and more efficient:
Feature | C | C++ |
---|---|---|
Null Pointer | NULL |
nullptr |
Dynamic Memory | malloc , free |
new , delete |
References | Not available | Supported |
Smart Pointers | Not available | unique_ptr , shared_ptr , weak_ptr |
```