What would be a practical application for using a pointer to a variables memory address. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What would be a practical application for using a pointer to a variables memory address.

I've been confused about memory addresses and why you would need the address for a variable. Maybe if you planned on inserting some assembler code within a cpp project this might be a valuable resource, but outside of that I'm not really understanding how this as a feature that can be used. If anyone wants to do me a solid and give an example of when this could come in handy or how memory address pointers can be used, it would be much appreciated. (FSociety)

10th Aug 2016, 4:54 AM
Donovan
Donovan - avatar
9 Answers
+ 2
@Umed Jadhav, I meant during the execution. But the same can be true while planning an algorithm too. Let's take an example of a program which asks users for the list of guests to may mail them invitation or print it or whatever. Now there could be 50 guests in your party. However, there may be 100 guests when I am throwing a party. You won't compile a new binary according to the number guests in each party right? So dynamic allocation comes to rescue.
12th Sep 2016, 11:55 AM
Nick D.
Nick D. - avatar
+ 1
Go to code playground and search for "Dynamic memory example". That should give you one of the use case. Think of an array or class which are bigger structures. If you need to use this array in some other place, without the provision of passing an address, you'll be forced to make another copy of this array and consume that much more memory. Let's say it's used at 10 places. That's 10x memory to store same data. Instead, wouldn't it be convenient to just pass the address of the original array to all the 10 subroutines?
10th Aug 2016, 7:42 AM
Nick D.
Nick D. - avatar
+ 1
Correct! Passing the values by reference means giving an address of the memory. And to collect this reference, you need a pointer. Recall how you create a pointer pointing to some variable. It's *p = &var. Looks familiar to what you said about passing by reference? It's not an alternative but it's the same thing. The whole pointer stuffs start making sense when you create and destroy objects dynamically at run time. During the run, you wouldn't know how much memory you'll need in advance while coding. So you cannot have a regular variable hold values for you. If you declare a regular variable, the memory is already reserved for it. What happens when you need more memory? That's where pointers come into picture.
10th Aug 2016, 8:06 PM
Nick D.
Nick D. - avatar
+ 1
When you want to save memory
26th Aug 2016, 5:38 PM
Vicente Hernández Pérez
Vicente Hernández Pérez - avatar
0
@Nick D Wouldn't that be the equivalent of passing a value by reference? That would prevent unneeded copies of values or functions being used. If they serve similar purposes, then which should you prefer over the other. The way I learned to pass values without copying is using int& val_name. This way it returns and modifies the original value rather than creating a copy, like you pointed out. With the mem_address pointer, you get back a hexadecimal value, is this just an alternative way to do the same thing passing by reference does.
10th Aug 2016, 7:53 PM
Donovan
Donovan - avatar
0
#include <iostream> using namespace std; int main() { int val = 10; int* val_addr = &val; int& val_num = val; cout<<"Value: "<<val<<"\n"; cout<<"Value address: "<<val_addr<<"\n"; cout<<"Value Reference: "<<val_num<<"\n"; return 0; } Here's code to try to illustrate / clarify my point/question.
10th Aug 2016, 7:54 PM
Donovan
Donovan - avatar
0
Okay, I see where you're going. It's making more sense now, ill just solidify my understanding by playing with the pointers and trying to use them in a project. Thanks for helping clear that up, Nick.
10th Aug 2016, 8:24 PM
Donovan
Donovan - avatar
0
@nick ... when u say during the run u don't know the memory requirement ... by run u mean while executing the program or while planning the algorithm of program
12th Sep 2016, 11:44 AM
Umed Jadhav
Umed Jadhav - avatar
0
ty @nick
12th Sep 2016, 1:01 PM
Umed Jadhav
Umed Jadhav - avatar