Tag Archives: cpp-pointer

auto_ptr, unique_ptr, shared_ptr and weak_ptr

Prerequisite – Smart Pointers C++ libraries provide implementations of smart pointers in following types: auto_ptr unique_ptr shared_ptr weak_ptr They all are declared in memory header file. auto_ptr This class template is deprecated as of C++11. unique_ptr is a new facility with a similar functionality, but with improved security. auto_ptr is a smart pointer that manages… Read More »

Dangling, Void , Null and Wild Pointers

  Dangling pointer A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer. There are three different ways where Pointer acts as dangling pointer De-allocation of memory Function Call Output: A garbage Address The above problem doesn’t appear (or p doesn’t become dangling) if x is a static… Read More »

Opaque Pointer

What is an opaque pointer? Opaque as the name suggests is something we can’t see through. e.g. wood is opaque. Opaque pointer is a pointer which points to a DS whose contents are not exposed at the time of its definition. Following pointer is opaque. One can’t know the data contained in STest structure by… Read More »

Functors in C++

Please note that the title is Functors (Not Functions)!! Consider a function that takes only one argument. However, while calling this function we have a lot more information that we would like to pass to this function, but we cannot as it accepts only one parameter. What can be done? One obvious answer might be… Read More »

Understanding nullptr in C++

Consider the following C++ program that shows problem with NULL (need of nullptr) Output: 16:13: error: call of overloaded ‘fun(NULL)’ is ambiguous fun(NULL);   What is the problem with above program? NULL is typically defined as (void *)0 and conversion of NULL to integral types is allowed. So the function call fun(NULL) becomes ambiguous.  … Read More »

Passing NULL to printf in C

Consider the following C code snippet. char* p = NULL; printf(“%s”, p); What should be the output of the above program? The print expects a ‘\0’ terminated array of characters (or string literal) whereas it receives a null pointer. Passing NULL to printf is undefined behavior. According to Section 7.1.4(of C99 or C11) : Use… Read More »