Page 1 of 1

Pointers, Plain and Simple. Rate Topic: -----

#1 37Liion  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-February 10

Posted 13 October 2011 - 10:41 AM

We were given an example on pointers earlier in the semester.
In order to heighten retention, I decided to rewrite the example and provide comment equivalent to the discussion that would be had in a classroom environment.

First, the code with no comments:
#include <iostream>

using namespace std;
void change_value(int *my_ptr) {
	*my_ptr += 10;
}
void change_value (int val) {
	val = 10;
}
int main() {
	int value = 5;
	int *value_ptr;
	value_ptr = &value;

	cout << "value: " << value << endl;
	cout << "*value_ptr: " << *value_ptr << endl;

	change_value(value);
	cout << "change_value(value);" << endl;
	cout << "value: " << value << endl;

	change_value(*value_ptr);
	cout << "change_value(*value_ptr);" << endl;
	cout << "value: " << value << endl;

	change_value(value_ptr);
	cout << "change_value(value_ptr);" << endl;
	cout << "value: " << value << endl;

	change_value(&value);
	cout << "change_value(&value);" << endl;
	cout << "value: " << value << endl;

	cin >> value; 

        return 0;
}


Can you guess the values of value each time it is printed out?
Write down or remember your answer, then compile and run.
Don't worry if you made a mistake, that's why you're here!

The correct order of values is:
Spoiler


Lets walk through it a couple lines at a time.

Prep Work:
//We need std::cout from <iostream> to print to the screen.
#include <iostream>
//And I hate typing std:: multiple times so we use the std namespace.
using namespace std;


"Working" change_value function:
//Our "working" change_value function. Given an int pointer, 
//it will increase the value of the int that the pointer is pointing to by ten.
void change_value(int *my_ptr) {
	*my_ptr += 10;
}


"Dud" change_value function:
//Our "dud" change_value function. It takes in an int by value, thus the variable
//itself is never passed in but instead its value, equivelent to int(val) where
//val is the variable supplied.
void change_value (int val) {
	val = 10;
}


main() and variable declaration:
int main() {
	//declare an int variable named "value" and assign it the value 5
	int value = 5;
	//declare a pointer to an int;
	int *value_ptr;
	//give that pointer the address of the int variable "value"
	value_ptr = &value;
        //print out the values of "value" and the int to which "value_ptr" points.
	cout << "value: " << value << endl;
	cout << "*value_ptr: " << *value_ptr << endl;



Our first attempt at increasing the value of "value":
        //Does not change value of "value" because we are passing by value 
	//and the variable "value" will never be touched. 
	change_value(value);
	cout << "change_value(value);" << endl;
	cout << "value: " << value << endl;



That didn't work, shame. Let's try again:
        //Does not increase "value" by ten because we are dereferencing before passing;
	//thus we are passing an int, not a int pointer. See previous comment.
	change_value(*value_ptr);
	cout << "change_value(*value_ptr);" << endl;
	cout << "value: " << value << endl;



Darn, still got not doing anything there either. I know, lets try this:
        //Ahh, finally a change_value call that actually changes the value of "value".
	//Notice that we are NOT dereferencing the point
	change_value(value_ptr);
	cout << "change_value(value_ptr);" << endl;
	cout << "value: " << value << endl;



Success! But everybody knows there's more than one way to do the same thing. Does this work?
        //Finally, it is worth noting that you do not need to create a pointer variable
	//in use a function that takes a pointer. Just reference the variable you wish
	//to pass in using '&'.
	change_value(&value);
	cout << "change_value(&value);" << endl;
	cout << "value: " << value << endl;

        return 0;



It does! Well, now that we've covered all the possibilities, you should be a little more prepared for experimenting with pointer use in your functions. Don't get overwhelmed. Slow down and think about what you are trying to accomplish and the syntax will come to you. When you think "address of" type '&' and when you think "value of the variable the pointer is pointing to" type '*'.

Good luck D.I.C.'lets :)

The full, commented source code.
#include <iostream>

using namespace std;
//Our "working" change_value function. Given an int pointer, 
//it will increase the value of the int that the pointer is pointing to by ten.
void change_value(int *my_ptr) {
	*my_ptr += 10;
}
//Our "dud" change_value function. It takes in an int by value, thus the variable
//itself is never passed in but instead its value, equivelent to int(val) where
//val is the variable supplied.
void change_value (int val) {
	val = 10;
}
int main() {
	//declare an int variable named "value" and assign it the value 5
	int value = 5;
	//declare a pointer to an int;
	int *value_ptr;
	//give that pointer the address of the int variable "value"
	value_ptr = &value;
	//print out the values of "value" and the int to which "value_ptr" points.
	cout << "value: " << value << endl;
	cout << "*value_ptr: " << *value_ptr << endl;
	//Does not change value of "value" because we are passing by value 
	//and the variable "value" will never be touched. 
	change_value(value);
	cout << "change_value(value);" << endl;
	cout << "value: " << value << endl;
	//Does NOT increase "value" by ten because we are dereferencing before passing;
	//thus we are passing an int, not a int pointer. See previous comment.
	change_value(*value_ptr);
	cout << "change_value(*value_ptr);" << endl;
	cout << "value: " << value << endl;
	//Ahh, finally a change_value call that actually changes the value of "value".
	//Notice that we are NOT dereferencing the point
	change_value(value_ptr);
	cout << "change_value(value_ptr);" << endl;
	cout << "value: " << value << endl;
	//Finally, it is worth noting that you do not need to create a pointer variable
	//in use a function that takes a pointer. Just reference the variable you wish
	//to pass in using '&'.
	change_value(&value);
	cout << "change_value(&value);" << endl;
	cout << "value: " << value << endl;
	cin >> value; //in place for those not executing from the command line, prevents window from disappearing.

        return 0; // return success to the operating system
}

This post has been edited by JackOfAllTrades: 16 October 2011 - 05:25 AM
Reason for edit:: Changed void main() to int main()


Is This A Good Question/Topic? 0
  • +

Replies To: Pointers, Plain and Simple.

#2 finalfantasyfreak15  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 19-February 11

Posted 22 January 2012 - 01:19 AM

Hi,

I am having trouble understanding how memory is allocated when you use a pointer or a reference. From my understand both pointers and references allocate memory on the heap. A variable that is neither of these (just a regular one) is allocated on the stack right?

So, from your example where you have:
int value = 5;
int* ptr = &value;

What is put on the stack and what is put on the heap and how do things point to each other? And what about if you had this:

vector<int*>* v;

Would that create a pointer to a vector that contains pointers to ints?

Thanks for any help!
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Posted 22 January 2012 - 07:49 AM

Quote

From my understand both pointers and references allocate memory on the heap. A variable that is neither of these (just a regular one) is allocated on the stack right?
So, from your example where you have:
int value = 5;
int* ptr = &value;

What is put on the stack and what is put on the heap and how do things point to each other? And what about if you had this:


A pointer and a reference can point to or reference a variable that has been created on the stack or on the heap. A pointer is a pointer, it doesn't care where in memory the variable resides.

In your example, if the snippet is inside a function, value and ptr would be created on the stack and ptr would point to (contain the address of) value.
If the snippet is outside any function (global) value and ptr would probably be created on the heap, and ptr would still point to (contain the address of) value.

When you use dynamic memory, malloc, new, or std::vector the memory is allocated from the heap.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1