C++

Topic: Pointers

Explain about C++ Pointers and new

I can think of only one significant area in which it looks like pointers are never going to go away for C++ programmers, at least not any time soon. When you use the new keyword, either to dynamically allocate an object or dynamically allocate a primitive data item (integer or floating-point), the keyword returns an address, which you normally assign to a pointer.int *p1 = new int;   // p points to a dyn. allocated integerOne thing I like about C++ is that, unlike Java, C#, and VB.NET, it recognizes no fundamental difference between primitive types and classes. Classes, in C++, are simply types that you create yourself, in effect extending the language.Therefore, you dynamically allocate and refer to an object—an instance of a class—in precisely the same way you’d dynamically allocate an integer:MyClass *p2 = new MyClass;   // p points to a dyn. allocated objectBut this syntax mandates pointer usage; I can see no safe way of coercing such a pointer value into a reference. You are stuck with pointers, at least in current, standard versions of C++. (Because, as shown earlier, pointers and references in function arguments usually have the same underlying implementation. It would be theoretically possible, I suppose, to trick the compiler into accepting one for the other, but don’t ever try that at home!)Interestingly enough, Java, C#, and VB.NET all use references in this situation, which is relevant to my learning problem that I mentioned at the beginning of this article. In these languages, you can declare an object variable (that is, a variable with class type), but it is always a reference. That means it “points nowhere” unless initialized with either (1) an object allocated with new or (2) the name of an existing object. Because references are used consistently in this context, they work just fine—as long as you understand how references work.There is one other reason why you will continue to work with pointers in C++. The use of pointers is deeply ingrained in the C++ standard library inherited from C. Many of the newer language features, such as range-based for and the items in the Standard Template Library (including the C++ string class) tend to minimize pointer usage, and in doing so present far fewer opportunities for you to inadvertently shoot yourself in the foot.And yet C++ will always support pointers because, despite their dangers and difficulties, there will always be that occasional op-system or device-driver writer that needs them.

Browse random answers:

What is Pointers in c++ ?
What is importance of const. pointer in copy constructor?
Explain "passing by value", "passing by pointer" and "passing by reference"
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at: * const char ** char const ** char * const
Why Pointers are not used in C Language. What are the main differences between C and C++
Explain about Reference operator (&) ?
Explain about declaring variables of pointer types with example ?
Explain about pointers and arrays ?
Explain about Pointer initialization ?
Explain about Pointer arithmetics ?
Explain about Pointers to pointers ?
Explain the void pointers with example ?
What is Null pointer ? 
What is Pointers to functions ?
What Are Pointers?
What are Using Pointers in C++ ?
Explain Accessing the Value at the Memory Address held by a Pointer ?
Why Use Pointers?
What are C++ Pointer Syntax ?
Explain about Taking Stock of Pointers ?
What are pointers? Why should you care?
Explain about Manipulating Data with Pointers and example ?
What are Array of pointers ?
Where do we use pointers?
Why do we need pointers?
Allocating a memory from the "Free Store" ?
Explain about Allocating a memory with new operator ?
Explain about De-allocating a memory with delete operator ?
Explain about Data type class pointers with example ?
Explain about memory leaks associated with pointers ?
Explain about memory leak caused by pointer reassignment ?
When local variable’s name is same as member’s name
What are function Pointer Syntax ?
What are Reading Function Pointer Declarations ?
What are Using a Function Pointer ?
What are Function Pointers in the Wild ?
How to Using Polymorphism and Virtual Functions Instead of Function Pointers (C++) ?
What are Benefits of Function Pointers ?
Where They get Useful  Function Arguments ?
Explain Smart Pointers (Modern C++) ?
What are Uses for smart pointers ?
What are Kinds of Smart Pointers ?
What are another Use of Pointers Efficient Array Processing ?
Explain about C++ Pointers and new
What are problems with pointers ?
Explain about generic smart pointer class ?
How to initialize a vector of pointers?
Explain about c++ Pointer Arithmetic ?
Explain about incrementing a Pointer ?
Explain  decrementing a Pointer ?
Explain pointer Comparisons ?