Having trouble with pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 8
Reputation: fireballnelson is an unknown quantity at this point 
Solved Threads: 0
fireballnelson fireballnelson is offline Offline
Newbie Poster

Having trouble with pointers

 
0
  #1
Dec 28th, 2008
A pointer points to a memory location containing a specified value right? If a pointer is defined without something to point at, it can point anywhere, right? Here is my question: What are the practical uses of pointers? I mean why wouldn't you just make a reference to the variable itself instead of having one more name to remember?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Having trouble with pointers

 
0
  #2
Dec 28th, 2008
  1. int *x = new int();
How can you dynamically allocate memory on the heap using a reference?
Last edited by Comatose; Dec 28th, 2008 at 11:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Having trouble with pointers

 
1
  #3
Dec 29th, 2008
Originally Posted by Comatose View Post
How can you dynamically allocate memory on the heap using a reference?
  1. int &x = *new int();
This should do it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Having trouble with pointers

 
0
  #4
Dec 29th, 2008
Well shut me up.....
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Having trouble with pointers

 
0
  #5
Dec 29th, 2008
dougy83 you could make that assignment to hold the allocation, but to me it looks like a memory leak waiting to happen and in a professional setting I would fail your code review for it.

There are also data structures that lend themselves to pointers, for example linked lists.

As another example I can remember a buffer that we implemented between a producer and a consumer. The producer tended to produce data in bursts and we couldn't make the producer wait until the consumer cleared out enough of the buffer. We ended up growing the buffer when it ran out of space. (The allocation of a new buffer and copying the data with memcpy was pretty quick.) The class that managed the buffer had to use a pointer to reference the buffer and could not have used a reference.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Having trouble with pointers

 
0
  #6
Dec 29th, 2008
Originally Posted by Murtan View Post
dougy83 you could make that assignment to hold the allocation, but to me it looks like a memory leak waiting to happen and in a professional setting I would fail your code review for it.
It depends on context. For example, it's all right in a proxy class implementation:
  1. // Declarations (no dependencies)
  2. class A;
  3. class ProxyA {
  4. public:
  5. ProxyA();
  6. virtual ~ProxyA();
  7. virtual void doit();
  8. // ... other interface members
  9. private:
  10. A& a;
  11. };
  12. // Implementation with a very specific
  13. // types and other dependencies...
  14. class A {
  15. public:
  16. void done() {
  17. std::cout << "Done!" << std::endl;
  18. }
  19. };
  20.  
  21. ProxyA::ProxyA():a(*new A)
  22. {}
  23.  
  24. ProxyA::~ProxyA() {
  25. delete &a;
  26. }
  27.  
  28. void ProxyA::doit() { return a.done(); }
  29.  
  30. // You have comfort and clear reference to
  31. // the real handler class in Proxy members.
  32. // No need in pointer != 0 tests in member functions...
It seems sacramental Type* p = new Type; "looks like a memory leak waiting to happen" for language purists (and C#/Java guys )...
Last edited by ArkM; Dec 29th, 2008 at 6:49 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Having trouble with pointers

 
0
  #7
Dec 29th, 2008
I will agree that the allocation will not leak if the ProxyA doesn't get lost, but in that context, why allocate it at all?

It could just be a member of the proxy.

  1. class ProxyA {
  2. public:
  3. ProxyA();
  4. virtual ~ProxyA();
  5. virtual void doit();
  6. // ... other interface members
  7. private:
  8. A a;
  9. };

Then you never have to allocate or deallocate it explicitly.
Last edited by Murtan; Dec 29th, 2008 at 7:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Having trouble with pointers

 
0
  #8
Dec 29th, 2008
You don't understand: it's the case when class A depends on a very specific headers, may be #import and other implementation-specific stuff. If you declare A a; then you MUST include all these stuff in all your modules (for example, class A has _bstr_t and _variant_t and _ConnectionPtr and other exotic type members) - it's too cumbersome (sometimes dangerous) practice.
The ProxyA is one of possible forms of such peculiarities incapsulation.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Having trouble with pointers

 
0
  #9
Dec 29th, 2008
Ok so its a form of hiding the full implementation of A from the users of ProxyA which provides a subset of the full implementation without having to re-write any code because it uses A to do the work.

Because ProxyA only has a reference (or pointer) to an A, you don't have to have the full A headers in the ProxyA header file (which the users of ProxyA will have to include) you only have to have them in the ProxyA source file. I can see that as a useful abstraction, and in the context you presented it the reference holding the allocation would be safe.

You mentioned that the members of ProxyA don't have to keep checking if the pointer is NULL because you use a reference. The only protection you have is that the constructor will fail if you can't get memory allocated. If you added your own test to the constructor, you could get the same safety with a pointer, but you do have to do the extra work.

The problem I've always encountered with this type of proxy was that it either the features proxied were limited, or the proxy would have to re-declare items from the class being proxied. An example might be an enum value used for a paramter to one of the methods of the A class. Either ProxyA must presume (or be able to determine for itself) what the enum parameter should be, or the enum has to be re-declared in the ProxyA scope. (and I hate duplicating anything, especially by hand.) If you throw in a structure passed by reference to another method, and you start to wonder just how much of the A header you didn't have to include.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: Having trouble with pointers

 
1
  #10
Dec 29th, 2008
Originally Posted by fireballnelson View Post
A pointer points to a memory location containing a specified value right? If a pointer is defined without something to point at, it can point anywhere, right? Here is my question: What are the practical uses of pointers? I mean why wouldn't you just make a reference to the variable itself instead of having one more name to remember?
A pointer is basically just an integer. That integer represents a memory location. When you declare an integer without giving it a value, it COULD contain any number, but that variable will be useless to you unless you assign a specific value to it. The same holds true for pointers, however, its much more dangerous to use pointers like that because without knowing exactly what that pointer 'points' to, you could be messing with a memory address that shouldn't be messed with. Ultimately causing errors and crashes.

I mostly use pointers while communicating with USB devices. Most times, I've had to poll the device to return me some data. Rather than having the device create a structure, fill it, and return the needed data, I call a function inside the devices *.dll and pass it a pointer to a structure already defined in my code. The device then simply uses that pointer to my structure and fills it with the data I need. It can then return a simple integer, which can be used for error checking.

Don't know if that helps at all. Just my $0.02.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC