answersLogoWhite

0

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.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

What does multiplying a pointer by 5 in C plus plus mean How does it change the pointer's value?

Multiplication is yet another thing, what you should never do with pointers.


What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


What is 'this' pointer in c plus plus?

Address of the current object.


What is null object in c plus plus?

a pointer that is not pointing to anything


What is an address in C plus plus programming?

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.


Which function is used to determine the position of the put pointer in a file in c plus plus?

The function ftell returns the position of the file pointer for a file.


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.


What does a - followed by a greater than symbol mean in a c plus plus program?

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.


What is the concept of asterisk in c plus plus?

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


Call by reference using pointer in c plus plus?

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


Can you control ports through c plus plus?

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.


How this pointer works in C plus plus?

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.