mr5
mr5

Reputation: 3580

Clarification about pointers

Object* pObject;
Object object;
pObject = &object;

Does object and pObject gets the same amount of memory allocated?

Object* pObject1, pObject2, pObject3;
Object object;
pObject1 = &object;
pObject2 = &object;
pObject3 = &object;

And

pObject1 = &object;
pObject2 = pObject1;
pObject3 = pObject1;

What is the difference between the two? Is it safe to dereference the pObject2 and pObject3 on the second example?

Upvotes: 2

Views: 132

Answers (3)

Khaled.K
Khaled.K

Reputation: 5940

A pointer is a variable, that store a memory address

The stored address in the pointer is the address of the first-block of the memory-structure of the object it points at

The syntax of a pointer that points at an object of type TYPE:

TYPE * pointer; // define a pointer of type TYPE

NULL (address 0) denotes a pointer that doesn't point anywhere currently:

pointer = 0;

To get the memory address of a variable or object:

pointer = &object; // pointer now stores the address of object

To get the pointed TYPE variable, you de-reference the pointer:

assert(&(*pointer) == &object); // *pointer ~ object

In example:

int a = 10; // type int
int * b = &a; // pointer to int
int* * c = &b; // pointer to int*

printf(" %d \n ", a );
printf(" %d \n ", *b );
printf(" %d \n ", **c );

char t [256] = "Not Possible ?";

char * x = t;
char * y = (x + 4); // address arithmetic

printf(" %s \n ", x ); // Not Possible ?
printf(" %s \n ", y ); // Possible ?

void* is a special pointer-type that can store any pointer of any degree, but cannot be used until you cast it to a compatible type ..

void * z = &c; // c holds int**

printf(" %d \n ", **((int**)c) );

Upvotes: 1

Jay
Jay

Reputation: 9656

In the first code snippet, there is only one object in memory. object is of type "Object". pobject is of type "address of Object".

Translating:

Object* pObject;    // Objectp is a POINTER that may be used for objects of type Object
Object object;      // Allocate on the stack an "Object". Call it "object"
pObject = &object;  // pObject points (that is, stores the ADDRESS of) object

In your second code, there would be three variables of type "address of something of type Object", all holding the address of Object. Pointers are like "numbers that represent memory addresses". However, as Gorpik menitoned, you should declare them as

Object *pObject1, *pObject2, *pObject3;

That is one reason it is interetsing to keep the asterisk close to the name -- in this case pObject -- and not the type -- in this case Object. Read it like "Of type Object: what pointer pObject1 points to, what pointer pObject2 points to, etc".

The third code snippet has the same effect. All three variabes get the address of object.

Upvotes: 5

Gorpik
Gorpik

Reputation: 11028

First, you have a declaration bug. The following line:

Object* pObject1, pObject2, pObject3;

does not declare three pointers; it declares a pointer (pObject1) and two objects (pObject2 and pObject3), the pointer being uninitialised and the objects, default initialised.

Let's assume we fix this bug, such as:

Object* pObject1, *pObject2, *pObject3;

In the first part, the strict answer to your question is no. A pointer and an object do not generally get allocated the same amount of memory. But I guess you mean if the object occupies the same memory as whatever is pointed by the pointer. In this case, pObject points to exactly the memory area occupied by object, so the answer is yes. But pObject will usually occupy some memory by itself, so it needs more memory altogether (that for the pointer itself and that for the object pointed).

In the second case, both pieces of code are equivalent. pObject1, pObject2 and pObject3 end up having the same value, which is the memory address of object. Dereferencing pObject2 and pObject3 is perfectly safe.

Upvotes: 3

Related Questions