Page 1 of 1

## Pointers, and a possible problem - if you're not careful! Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=381666&amp;s=7c8fcc215cce80e3afa4bf57f04ce4e9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• 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? 3

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

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• 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!

### #3 andrewsw

• Well, I'll need some beer.

Reputation: 4978
• Posts: 18,899
• Joined: 12-December 12

Posted 15 September 2015 - 02:11 PM

This reminded me of the site cdecl

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• 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!