| | |
Having trouble with pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 0
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?
C++ Syntax (Toggle Plain Text)
int *x = new int();
Last edited by Comatose; Dec 28th, 2008 at 11:21 pm.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
•
•
•
•
How can you dynamically allocate memory on the heap using a reference?
C++ Syntax (Toggle Plain Text)
int &x = *new int();
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
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.
•
•
•
•
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.
c++ Syntax (Toggle Plain Text)
// Declarations (no dependencies) class A; class ProxyA { public: ProxyA(); virtual ~ProxyA(); virtual void doit(); // ... other interface members private: A& a; }; // Implementation with a very specific // types and other dependencies... class A { public: void done() { std::cout << "Done!" << std::endl; } }; ProxyA::ProxyA():a(*new A) {} ProxyA::~ProxyA() { delete &a; } void ProxyA::doit() { return a.done(); } // You have comfort and clear reference to // the real handler class in Proxy members. // No need in pointer != 0 tests in member functions...
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.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
Then you never have to allocate or deallocate it explicitly.
It could just be a member of the proxy.
c++ Syntax (Toggle Plain Text)
class ProxyA { public: ProxyA(); virtual ~ProxyA(); virtual void doit(); // ... other interface members private: A a; };
Then you never have to allocate or deallocate it explicitly.
Last edited by Murtan; Dec 29th, 2008 at 7:21 am.
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
The ProxyA is one of possible forms of such peculiarities incapsulation.
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.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
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.
•
•
Join Date: Dec 2008
Posts: 15
Reputation:
Solved Threads: 3
•
•
•
•
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?
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.
![]() |
Similar Threads
- Deleting an index of array using pointers (C++)
- Hey Just started new topic with arrays, and having some trouble please help (C++)
- Trouble with Pointers (C)
- Trouble with Pointers and Arrays (C)
- Mouse cursor trouble on a two-monitor system (USB Devices and other Peripherals)
- Sorting arrays of pointers with function? (C)
Other Threads in the C++ Forum
- Previous Thread: Copy the file
- Next Thread: where to include header files?
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






