PDA

Click to See Complete Forum and Search --> : these stupid pointers


abdul
Jul 23rd, 2001, 09:10 PM
I have problem understanding the main theory and concept of pointers. I have two question:

1)

unsigned short int howOld = 50; // make a variable
unsigned short int * pAge = &howOld; // make pointer to howOld


pAge contains the address of Howold in the memory but why cannot I just use howold as variable like in the following code:


unsigned short int yourAge; //Code #1
yourAge = howOld;


Does "yourAge" has the same value in the following code?


unsigned short int yourAge; //Code #2
yourAge = *pAge;


If yourAge is same in both codes, then why dont I just use code#1 ?

Zaei
Jul 24th, 2001, 12:39 AM
Using an unsigned short is a bad example. Imagine...

struct blah
{
DWORD a;
DWORD b;
DWORD c;
DWORD d;
DWORD e;
DWORD f;
DWORD g;
};

Using "sizeof()" on this monster would give you 28 Bytes. So, to pass this to a function, you would hae to copy 28 bytes to some location in memory. But, we have pointers. Calling "sizeof()" on a pointer to "blah", gives you 4 Bytes. You can pass this to a function with no hang ups on copying the data (that has to do with assembly stuff).

So, basically, pointers to variables are pretty much for speed.

On the other hand, pointers can act as arrays. For example:

INT* p;

p = new INT[25];

The pointer "p" now points to a section in memory allocated to hold 25 INT values.

Z.

Megatron
Jul 24th, 2001, 11:44 AM
Zaie is right in the fact that pointers are speed and memory efficient. Another example would be:


class CLargeClass {
// many members here
};

void ManipulateClass(CLargeClass *clc) {
clc-> // manipulate class
}

This saves us from copying the class (by value) hence a lot of memory would be saved.

abdul
Jul 24th, 2001, 01:01 PM
First off:

Can you point to a class with allocating any memory for it. I tried:

AClass *pclass

But it gives me an error. When I allocate memory for the whole class like this:

AClass *pclass = new AClass
//access the member data here
cout<<pclass->member;
delete pclass


then it works fine.
--------------------------------------------------------------------------------
an other question about pointers:

Which one is faster when accessing the private data in a class function:

this->privatedata

OR just

privatedata

-----------------------------------------------------------------------------
So its mean that:


MyClass *cls;
cout<< cls -> member

/*as I stated above that I get an error when I try to point to a class without allocating an memory for it so I cannot run this code*/


is faster than


MyClass cls;
cout<<cls.member

unformed
Jul 24th, 2001, 03:53 PM
Can you point to a class with allocating any memory for it?

no, because it's essentially a null pointer, it's not pointing to anything.

Take this analogy for example.

I give you the address to a house and tell you to go to live there, but if there's not a house already built, you can't really go there.

So what's the purpose of having a pointer to a class?
Primarily for sending and returning values to/from functions.

Say I have the classes CFamily and CPerson
There's a member function
CFamily::GetFather() which returns the father as a CPerson object.

If you declared it as:
CPerson CFamily::GetFather();
the function would have to find the Father member and send a copy of it back.
Also, in the case that the Father did not exist, the function couldn't return NULL because then the program would crash (can't assign NULL to an object)

If it has to return a pointer, it only has to send 4 bytes (the sizeof(ptr) ) and it CAN send a NULL in the event that it didn't find the Father.

But, going back to your original question:
When you declare
CPerson man;
the compiler automatically allocates space for the CPerson object, and creates the object.

When you call
CPerson* pman;
the compiler only allocates memory for the pointer which then needs to be set to the address of a class.

when you call [b]new CPerson[b] it allocates space for (and creates) the object, and returns the address of the object. That's why you can set pman=new CPerson ...

------------------------

hopefully that made sense... ;)

kedaman
Jul 24th, 2001, 04:02 PM
AClass *pclass;
will allocate 4 bytes, a pointer to a Aclass object, in fact a stray pointer, which can point to exactly anywhere, which isn't good, if you try to access it or write to it, your app my crash or work incorrectly.

If you initialize a pointer like this:
AClass *pclass=0;
it points to 0, which is commonly used as a value of a uninitialized pointer, use this way if you want to have pointers not pointing to anything specific. Therefore to check if a pointer is used you do:

if (pclass) then
//the pointer is in use


this->privatedata

won't access private members, but only public members, it's also unnessesary, so there's no need to use this pointer for this purpose. inside the class, module scope is used.

for your last examples, if you don't allocate an object, there's no object to access either.

In case you have a non-member in a class, that is a static member, you can access them using the scope operator on the classname:

MyClass::Memberfunction();

without instantiating any objects.