Results 1 to 6 of 6

Thread: these stupid pointers

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    these stupid pointers

    I have problem understanding the main theory and concept of pointers. I have two question:

    1)
    Code:
    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:

    Code:
    unsigned short int yourAge;               //Code #1
    yourAge = howOld;
    Does "yourAge" has the same value in the following code?

    Code:
    unsigned short int yourAge;               //Code #2
    yourAge = *pAge;
    If yourAge is same in both codes, then why dont I just use code#1 ?
    Baaaaaaaaah

  2. #2
    Zaei
    Guest
    Using an unsigned short is a bad example. Imagine...
    Code:
    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:
    Code:
    INT* p;
    
    p = new INT[25];
    The pointer "p" now points to a section in memory allocated to hold 25 INT values.

    Z.

  3. #3
    Megatron
    Guest
    Zaie is right in the fact that pointers are speed and memory efficient. Another example would be:

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

  4. #4

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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:
    Code:
    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:

    Code:
    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

    Code:
    MyClass cls;
    cout<<cls.member
    Baaaaaaaaah

  5. #5
    Junior Member
    Join Date
    Jun 2001
    Posts
    22
    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...

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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:
    Code:
    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.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width