Pointers are objects that point (in memory) to another object. Their usefulness lies in their ability to dynamically manage memory and data structures. They are also very dangerous because there is a tendency to not adhere to all the rules when dealing with pointers and dynamic memory.
1. Never use a pointer without initializing it with a proper allocation request.
2. Always check a memory allocation request for failure.
3. Never use a pointer beyond the bounds of the original allocation.
4. Never use a pointer after it has been deallocated.
5. Alway deallocate a prior memory allocation when you are done using it.
If xyz is the name of an object in memory, then &xyz is it's address. If pxyz is a pointer to an object of type xyz, then the statement pxyz = &xyz would initialize pxyz to point to xyz. At that point, you could refer to xyz using *pxyz.
If pabc is a pointer to an object of type abc, then the statement pabc = malloc(100*sizeof(abc)) would allocate an array of 100 objects of type abc from the heap, and initialize pabc to point to the first one. You could then use the array with pabc[n] syntax or with *(pabc+n) syntax. You would deallocate the array with free pabc.
C++ Specific:
If pqrs is a pointer to an object of class type qrs, then the statement pqrs = new qrs would allocate memory from the heap, initialize pqrs to point to the object, and fire the constructor for that class. You could then invoke methods of the class with pqrs->method(), etc. or you delete the class with delete pqrs, said deletion firing the destructor.
Pointers in C++ are the same as pointers in C. In both cases, pointers are variables that store a memory address, so they will occupy 4 bytes in a 32-bit system, or 8 bytes in a 64-bit system.
Pointers must have a type to determine the type of data being pointed at. If the type is unknown at compile time, the type may be declared void, and it is up to the programmer to determine the actual type at runtime. However, since C++ is object-oriented, the type may also be a base class (or an abstract base class), rather than a concrete class. In this case there is no need to determine the actual type as the correct behaviour can be determined via virtual methods declared in the base class, or inherited by the base class.
To declare a pointer variable, specify the variable type, followed by the pointer operator (*), followed by the name of the pointer. Thus the following will allocate memory for a pointer to a character type named p:
char * p;
Characters are typically 1 byte in length, but p is a pointer so will be 4 bytes in length (assuming 32-bit architecture). p can therefore point to any valid memory location and treat that memory as if it were a character type (even if it is a completely different type, such as int or float).
Pointers must be initialised before they are used, by assigning a memory address to them. The memory address may be NULL to signify the pointer is initialised but doesn't currently point at anything (a null pointer). Prior to accessing any memory via a pointer, it is always good practice to ensure the pointer is non-NULL first. However, uninitialised pointers can also be non-NULL, so always ensure your pointers are initialised or zeroed (assigned to NULL) as soon as you declare them.
It is also good practice to zero a pointer as soon as the pointer is no longer required, and before the memory being pointed at falls from scope. If the pointer is pointing at dynamic memory on the heap (the free store), then you must maintain at least one pointer to that memory in order to release that memory back to the system. Once released, the pointer must be zeroed.
A pointer is an object that refers to another object. It contains the address of another object. Using dereference syntax (*) you can treat a pointer as an identifier to the object that it points to. Also, you can implicitly treat pointers and arrays in a similar fashion, because array syntax is simply a means of adding an offset to the value of a pointer, before accessing the object to which it points.
The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer
An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.
It is a type of pointer dereference operation. If you have a pointer p to an object that has methods or attributes, you can say (*p).m to refer to the m method of the object, or you can say p->m to do the exact same thing.
An asterisk in C++, such as int *data, is what's known as a pointer. A pointer is like a regular variable, but instead of holding a value, a pointer holds the memory location of the value. It's a somewhat difficult concept, and you can learn more about it here: See related links section below...
Yes, C++ has pointers, which are references to memory locations. which are variables that store memory addresses, or NULL (zero). If the pointer is non-NULL, the pointer is said to dereference the object (or variable) residing at the stored memory address, which permits indirect access to that object so long as the object remains in scope.
Multiplication is yet another thing, what you should never do with pointers.
The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer
Address of the current object.
a pointer that is not pointing to anything
An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.
The function ftell returns the position of the file pointer for a file.
Yes. All string variables are pointers as are other arrays.
It is a type of pointer dereference operation. If you have a pointer p to an object that has methods or attributes, you can say (*p).m to refer to the m method of the object, or you can say p->m to do the exact same thing.
An asterisk in C++, such as int *data, is what's known as a pointer. A pointer is like a regular variable, but instead of holding a value, a pointer holds the memory location of the value. It's a somewhat difficult concept, and you can learn more about it here: See related links section below...
Example: void foo( MyClass& object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer
Yes. If the ports are memory mapped, then you simply need a pointer to that address, and you need to declare the pointer as volatile. If they are I/O mapped, then you need to create an _asm{} block.
The "this" pointer is a pointer to the instance of the object, with scope within a member function of that object. It is not always necessary to use it, as references to variables defined in the object will be implicitly prefixed with "this->", but it can resolve name scoping problems, and it can make the code more readable.