943,883 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 893
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 28th, 2008
0

Having trouble with pointers

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fireballnelson is offline Offline
8 posts
since Oct 2008
Dec 28th, 2008
0

Re: Having trouble with pointers

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Dec 29th, 2008
1

Re: Having trouble with pointers

Click to Expand / Collapse  Quote originally posted by Comatose ...
How can you dynamically allocate memory on the heap using a reference?
C++ Syntax (Toggle Plain Text)
  1. int &x = *new int();
This should do it.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Dec 29th, 2008
0

Re: Having trouble with pointers

Well shut me up.....
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Dec 29th, 2008
0

Re: Having trouble with pointers

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 29th, 2008
0

Re: Having trouble with pointers

Click to Expand / Collapse  Quote originally posted by Murtan ...
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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 29th, 2008
0

Re: Having trouble with pointers

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.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 29th, 2008
0

Re: Having trouble with pointers

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 29th, 2008
0

Re: Having trouble with pointers

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 29th, 2008
1

Re: Having trouble with pointers

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.
Reputation Points: 13
Solved Threads: 3
Newbie Poster
Liinker is offline Offline
15 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Copy the file
Next Thread in C++ Forum Timeline: where to include header files?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC