answersLogoWhite

0


Best Answer

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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

A pointer is a variable that holds an address in memory.

int * pPointer = new int;

This code creates a pointer to an integer, and assigns it an address of an integer object on the heap.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A pointer is a reference to some chunk of memory on your computer. To actually get the value out of said chunk of memory, you must dereference it (i.e. *pointer).

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A pointer is an object that contains the address of another object, hence the term pointer, meaning that one object points to another.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is mean by pointer in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.