Summary of Pointer operators and symbols
new
The new
operator in C++ is used to allocate memory on the heap for new objects or arrays of objects.
Dynamic memory allocation: The
new
operator allocates memory dynamically on the heap. This memory is allocated at runtime, not at compile time, which allows you to create objects when you need them, and with sizes determined at runtime.Memory allocation for objects: When you use the
new
operator to create an object, the memory allocated is enough to hold the object's data (its member variables) and the vtable for the object (if the object's class has virtual functions).Constructor call: After memory allocation, the
new
operator calls the constructor for the object. This allows the object to be properly initialized before you start using it.Return of pointer: The
new
operator returns a pointer to the newly created object.
delete
The delete
operator in C++ is used to free up the memory that was allocated dynamically using new
.
Destructor call: When you use
delete
on a pointer to an object, it calls the destructor for the object. This gives the object a chance to clean up any resources it has allocated.Memory deallocation: After the destructor is called, the memory that was allocated for the object is freed, meaning it's returned to the system and can be reused for future allocations.
Pointer setting: Most implementations of
delete
don't automatically set the deleted pointer tonullptr
, meaning that the pointer still points to the memory location that was just deallocated. This is called a dangling pointer, and it can lead to bugs if you try to access the memory through the pointer. As a good practice, you should manually set the pointer tonullptr
after deleting it.
&
In C++, the ampersand (&
) is used for a couple of different purposes:
Reference operator: The
&
character is used when declaring a reference type. A reference is a type of C++ variable that acts as an alias to another object or valueAddress-of operator: If you use the
&
character in an expression, it acts as the "address-of" operator, which returns the memory address of its operand.Pass by reference: In function parameter lists,
&
is used to indicate that a function should receive a reference to an object rather than a copy of the object. This is useful when you want a function to modify its arguments, or when you want to avoid copying large objects.
In C++, the asterisk (*
) symbol is used for several different purposes:
Pointer declaration: The
*
is used when declaring a pointer variable. A pointer is a variable that stores the memory address of another variable.Dereference operator: When used in an expression,
*
is the dereference operator, which gives the value stored at the memory address held by a pointer.Creating objects of class: The
*
is used to create objects dynamically (on heap) in C++Multiplication operator: The
*
is also used as the multiplication operator in arithmetic expressions.
In general, it's important to remember that *
in C++ is heavily context-dependent, and its meaning can change based on where and how it's used.
In C++, the ->
operator is used to access members (methods or data) of an object through a pointer. It combines the dereference operator *
and the dot operator .
into a single operator. The “this” is a pointer.
In C++, **
is typically used in the context of pointers to pointers. It's a way of declaring a variable that can hold the address of a pointer variable, which in turn holds the address of another variable. It's also worth noting that **
is commonly used in C++ when dealing with multi-dimensional arrays and with function parameters that need to modify pointer arguments.
In C++, nullptr
represents a null pointer value, which means a pointer that doesn't point to an object or function. This keyword was introduced in C++11 to replace the use of NULL
or 0
for null pointers.
Here are a few key things to know about nullptr
:
Type-safe: Unlike
NULL
or0
,nullptr
is a keyword that represents a pointer type, which makes it type-safe. It can be implicitly converted and compared to any pointer type, but not to integral types, except forbool
.Value: The value of
nullptr
is a prvalue (pure rvalue) constant and is converted to any pointer type whenever needed.Usage:
nullptr
is used in pointer initialization, pointer comparison, and in conditions to check for null pointersNull pointer exceptions: If you try to access memory through a null pointer, your program will typically crash with a "segmentation fault" or similar error. Therefore, before dereferencing a pointer, it's a good idea to check whether it's
nullptr
.Function Overloading:
nullptr
can be used in function overloading. If a function is overloaded with both integer and pointer arguments, usingnullptr
will select the pointer version.
In conclusion, it's usually a good idea to initialize pointers with nullptr
if they're not immediately assigned a valid memory location. This makes it clear that they're not supposed to point anywhere yet, and helps prevent bugs due to uninitialized pointers.
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark