Page 1 of 1

Pointers, and a possible problem - if you're not careful! Rate Topic: -----

#1 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3466
  • View blog
  • Posts: 12,349
  • Joined: 08-August 08

Posted 15 September 2015 - 01:43 PM

There are many tutorials on using pointers, but I don't see many explaining how you can get into trouble with them. Memory leaks can be tricky to spot, here's how.

Look at this code:
#include <iostream>

using namespace std;

void modify(int *m, int val) {
	*m = val;
	cout << *m << endl;
}

void create(int *m, int val) {
	m = new int;
	*m = val;
	cout << *m << endl;
}

int * create(int val) {
	int *n = new int;
	*n = val;
	cout << *n << endl;
	return n;
}

int main(int argc, const char * argv[]) {
	int *one;
	one = new int;

	modify(one, 1);
	cout << one << " " << *one << endl << endl;

	int *two;
	two = new int;
	*two = 100;
	create(two, 2);
	cout << two << " " << *two << endl << endl;

	int *three;
	three = create(3);
	cout << three << " " << *three << endl << endl;

	return 0;
}


Its output on my (yours may use different addresses) machine:

Quote

1
0x100105470 1

2
0x100105480 100

3
0x1001054a0 3

Looks fine right? No, there's a problem with the second set. We thought it would output 2, but it held onto 100. What happened?

It's easy to forget that in C/C++ parameters are generally passed by value (arrays decay to pointers) by default. Even pointers are passed by value, so in line 11 the new generates a new address that is assigned to the copy of the pointer received, not the original pointer. We've basically created a memory leak since this copy can't be accessed beyond the scope of the function, and we don't intend to delete the object (it doesn't have to be just an int) here, we want to continue using it after the function returns. The first and third functions are alternative methods for doing just that. They don't create leaks!

Is This A Good Question/Topic? 4
  • +

Replies To: Pointers, and a possible problem - if you're not careful!

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3466
  • View blog
  • Posts: 12,349
  • Joined: 08-August 08

Posted 15 September 2015 - 01:57 PM

A solution I'd forgotten: change the second function to use a reference to a pointer.
void create(int *&m, int val) {
	m = new int;
	*m = val;
	cout << *m << endl;
}


Now it will refer back to the original pointer, so no leak!
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • But the opposite, you said.
  • member icon

Reputation: 5531
  • View blog
  • Posts: 21,814
  • Joined: 12-December 12

Posted 15 September 2015 - 02:11 PM

This reminded me of the site cdecl
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3466
  • View blog
  • Posts: 12,349
  • Joined: 08-August 08

Posted 16 September 2015 - 06:15 AM

Yes, that's one reason I really don't like pointers, so I prefer C++ to C: I find it difficult to talk about pointers in a way that "normal humans" will understand. Unfortunately, lots of C++ code has pointers (often where references would be better), so you've got to learn to work with them!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1