This might sound like a really stupid question but what gain do I get for using pointers? I use pass by reference and have used pointers a lot but have never really thought of the actual saving achieved by doing this. I have just been taught that using pointers is faster then copying values in c++.

I understand that complexity issues are usually to get something from n^2 to nlogn for example, but if you do a pass by integers into C++ this copies the values to a new segment of memory.

You can then do the calculations and return the answer and use this, or pass by ref to affect what your doing directly.

Sorry to ramble my question is how much time does it take to copy to memory?
If a program was to use only pointers would it be 50% quicker then the same program that used unnecessary int or float passes?

Is the difference so small that no-one has ever really studied or bothered with it?

Many thanks

If we're only talking about pass by reference vs. pass by value, then the benefit of pointers only shows up in two cases:

  1. You want to use output parameters rather than input parameters. Passing by reference enables this.
  2. The pointed-to object is relatively large compared to a pointer.

Comparing the passing of a pointer to the passing of an int or a float doesn't show the benefits of pointers because int and float are comparable in terms of size. Now if you have a composite type that amounts to several times the size of a pointer, or arrays where the size is variable and unknown, using a pointer with a small and known size is preferable.

A pointer is probably the same size as an integer, so you probably won't gain any resources by using pointers when passing simple integers, but take a structure for example.


struct SomeType{
int x;
int y;
char name[128];
float delta;
std::string etc;

If you use a pointer or reference you won't copy the entire structure's contents.

void foo(SomeType * const st){
	st->x = 100;
	st->y = 50;
	st->delta = 1.0f;
	//Constant pointer so you don't
	//change what the pointer points to.
	//SomeType s;
	//st = &s;

Awesome thanks for the help and great explanation