The value variable represents the value as primary and uses the & operator
to get the address, whereas the ptr_value variable represents the address as primary and uses
the * operator to get the value.

The name of the pointer i.e. ptr_value represents the location where the variable i.e. value is stored.The * operator is called the indirect value or the dereferencing operator. It gives the value at the location.

The & in &x is an address operator and it is used to assign address to pointer.

Image source : Cpp Primer Plus 5th Edition

In brief, ptr_value is a pointer (an address) and *ptr_value is an int not a pointer.Declaring and Initializing Pointers

Since the address of a char typically looks the same as the address of a double , but char and double use different numbers of bytes and different internal formats for storing values. Therefore, a pointer declaration must specify what type of data to which the pointer points.

The use of spaces around the * operator are optional. int *ptr;emphasize that the combination *ptr is a type int value.int* ptr;

emphasize that int* is a type, pointer-to- int .

int* p1, p2;

creates one pointer ( p1 ) and one ordinary int ( p2 ).

Dangers in Pointer

When you create a pointer in C++, the computer allocates memory to hold an address, but it does not allocate memory to hold the data to which the address points.

If we omit the step of creating space for data it can cause disaster.double * add; //create a pointer-to-double*add = 420; //place a value in never-never land

add is a pointer, but it is not assigned with any value.Suppose add has 1200 value, the computer attempts to place the data at address 1200 , even if that happens to be an address in the middle of your program code.Allocating Memory with new

The Pointers are important when you allocate unnamed memory duringruntime to hold values. In this case, pointers become the only access to that memory.

In C, you can allocate memory with the library function malloc(). You can still do so in C++, but C++ also has a better way: the new operator.

You tell new for what data type you want memory; new finds a block of the correct size and returns the address of the block and you assign this address to a pointer.int * ptr = new int;Thenew int part tells the program you want some new storage suitable for holding an int .
The new operator uses the type to figure out how many bytes are needed. Then it finds the
memory and returns the address. Next, you assign the address to ptr , which is declared to be of
type pointer-to- int . Now ptr is the address and *ptr is the value stored there.

Freeing Memory withdelete

We use new to request memory and delete operator to return memory to the memory pool when we are finished with it. This free memory can be reused by other parts of program. int * ptr = new int; // allocate memory with new. . . // use the memorydelete ptr; // free memory with delete when done This removes the memory to which ptr points; it doesn’t remove the pointer ptr itself. You can
reuse ptr.

You should always balance a use of new with a use of delete ; otherwise, you can wind up with a memory leak—that is, memorythat has been allocated but can no longer be used.

Comments

Post a Comment

Popular Posts

Computer programming is one of the fastest growing field. If you are a programmer then you can be hired by a top tech company or you can also work as a freelancer and earn decent amount. So the question arises, how to learn to code ? Initially it was very difficult to learn programming. As there was no internet and you have to relay on books or your colleagues’ code. Now you can find tons of material on the internet. You can learn any computer programming language you want. I am going to tell you some sites which you should visit regularly.

Huffman Code compress data very effectively: saving of 20% to 90% are typical, depending on the characteristics of the data being compressed.Huffman's greedy algorithm uses a table giving how often each character occurs (i.e., its frequency) to build up an optimal way of representing each character as a binary string. This algorithm constructs an optimal prefix code called a Huffman Code.

What is namespace?"In general, a namespace is a container for a set of identifiers Namespaces provide a level of direction to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name. " Or According to Wikipedia “In computer programming namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisionsbetween multiple identifiers that share the same name.”