I have a simple question that whether only using pointers instead of normal variables increase the efficiency of the program either time wise or memory wise?
For an instance if I use following program to swap two integers.
#include<iostream>
#include<conio.h>
#include<new>
using namespace std;
int main()
{
int *a=new int;
int *b=new int;
int *c=new int;
cin>>(*a)>>(*b);
*c=*a;*a=*b;*b=*c;
cout<<"swapping";
cout<<*a<<*b;
getch();
}
The only thing I can think of that might make using a pointer slower than using a local variable is that the generated assembly might involve more complex memory addressing, thus resulting in larger machine op-codes, which in turn would take ever-so-slightly more time to execute.
That time difference would be so negligible though that you shouldn’t worry about it.
What you should consider is that allocation on the stack is much faster than allocation on the heap. In other words:
is slower than:
but only because of the allocation
new int, not because you are using a pointer.