What I understand about pointers

Pointers are used to access addresses and values of variables at runtime.
to declare a pointer one must first declare the type, then * to show that it is a pointer, and finally followed by the variable’s name.

EX int* count;
To point to another variable’s address you would do the following
count = &address;
To point to another variable’s value…
count = *value;

I am currently unsure of what the benefits of using a pointer are but looking forward to find out why there are some!

In this example actually the assignment will no work.
If value in your example is int
then it should be like this:
*count = value;
And the result is not ‘point to another variable’s value’, but assign value from ‘value’ to the address where count points’

One of the benefits is copy time. When you pass values of any type by value, that means copy them every time. For example:

int square(int nubmer) {
return number * number;
}
int a = 2;
int b = square(a);

When you call square(a) function, and you pass a to it, the value of a is going to be copied, so when you are inside of a function, you already working with a copy of a, that is represented as number;
Same goes for return value:
When you return number * number actually a copy of a result is returned and then assigned to b;
With small data types like int or float or char this is not a problem, and copying is done fast.
But when you will start having custom data types created by you, or use those created by other developers, this can slow down the performance.
Another case is when you need pointers is when you will start creating your objects on a heap, and the only way of reaching them is to have a pointer to the area of memory where it was created. That is a bit more advanced topic. You will reach it later

Thanks for the quick reply! And I meant to say assign value just tired just woke up haha.
Also that make sense for the benefit of pointers to save memory space and loading times if I understood what you are saying correctly.

Yes, it can save you memory (in case when you are creating objects on a stack), and time (because to copy a pointer is faster then to copy all the data of an object)

One time in a book I read that after profiling author found out that like 30% of time his code is doing is copying values of a std::string variable. So all he had to do is to pass them as a reference (which is a special kind of pointer)
Sorry for overloading with information.
Feel free to ask any questions on the topic

1 Like

any chance you could help me out with this? Line trace hit is not being logged

Privacy & Terms