CPP

Differences Between C and C++ in Pointers (Excluding Function Pointers)

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.


Table of Contents

  1. Introduction
  2. Pointer Declaration and Initialization
  3. Dynamic Memory Allocation
  4. References in C++
  5. Smart Pointers in C++
  6. Conclusion

1. Introduction

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.


2. Pointer Declaration and Initialization

C

In C, pointers are declared and initialized as follows:

int x = 10;
int *ptr = &x;  // ptr holds the address of x

C++

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

Key Difference:


3. Dynamic Memory Allocation

C

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

C++

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

Key Differences:


4. References in C++

C

C does not have references. Pointers are the only way to indirectly access variables.

C++

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

Key Differences:


5. Smart Pointers in C++

C

C does not have smart pointers. Memory management is entirely manual using malloc and free.

C++

C++ introduces smart pointers to automate memory management and prevent memory leaks:

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;
}

Key Differences:


6. Example from lecutre


#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;
}


6. Conclusion

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

```