pointers references c++


pointers references c++ :

Where we use reference and where we use pointers ? Differences ,advantages and disadvantages .


pointers references c++

1)A reference is constant, you can’t bind to a different object once it has been initialized.
2)A reference must be initialized, whereas a pointer needn’t be.
3)A reference must always point to a valid object, a pointer may be NULL
4)A reference is automatically dereferenced. You don’t use *ref to access the actual object it is bound to
5) you can’t create a reference to a reference. Though you can create a pointer to a pointer
6)Unlike a pointer, a reference is treated as if it were the object bound to it, i.e. you access members using the reference itself.

-A reference is basically a pointer with restrictions (has to be bound on creation, can’t be rebound/null). If it makes sense for your code to use these restrictions, then using a reference instead of a pointer allows the compiler to warn you about accidentally violating them
-The conceptual difference is that a reference is a name for an object, and a pointer is an object containing the address of another object

-Prefer references over pointers

Common mistakes while using Pointers:

char *x = 0; //The pointer x is initialized to 0, equivalent to NULL (in fact, NULL is a stand-in for 0)
*x = 3; //Segmentation fault.Accessing a null pointer

A common mistake is not to  check the return from malloc to make sure that the system isn’t out of memory. Another common mistake is to assume that a function that calls malloc doesn’t return NULL even though it returns the result of malloc.

Note that in C++, when you call new, it will throw an exception, bad_alloc, if sufficient memory cannot be allocated. Your code should be prepared to handle this situation cleanly, and if you choose to catch the exception and return NULL inside a function that ordinarily returns a new’ed pointer

char *create_memory()
char *x = malloc(10);
if(x == NULL)
return NULL;
strcpy(x, “a string”);
return x;

void use_memory()
char *new_memory = create_memory();
new_memory[0] = ‘A’; /* make it a capital letter */
We did a good thing by checking to make sure that malloc succeeds before using the memory in create_memory, but we don’t check to make sure that create_memory returns a valid pointer!

This is a bug that won’t catch you until you’re running your code on a real system unless you explicitly test your code in low memory situations.

Pass an argument by reference:

When a ref is passed, it is the alias. So whatever the called function does with the ref, the change would be reflected in the original variable too. So this might be a disadvantage too. The method of passing offers advantage of lesser stack memory used as pass by value since it will use stack.

Why references was needed in C++, when pointers were already there in C?

  • To Support Operator overloading
  • Using pointers alone you can’t properly pass-by-reference or return-by-reference.
  • Some methods require a reference. Like implementation of a copy-constructor?
  • Sometimes you need to enforce aliasing. You can’t do that with pointers – they can be changed. References cannot bind to something different. So when you initialize a reference, you guarantee it will refer to the same object through its scope.

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at [email protected] along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

One thought on “pointers references c++”

Comments are closed.