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?
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.
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:
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.
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.
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.
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.