0

Here's my program

class Base
{
public:
  void fc()
  {
    cout << "Class function is called" << endl;
  }
};

void main()
{
  Base* ptr;
  ptr = 0;
  Base b1, b2;
  *ptr = b1;
  ptr->fc();
  ptr = 0;
  ptr = &b2;
  ptr->fc();
}

It runs OK My question is simple:

You can assign object to pointer by *ptr=b1; or ptr=&b1; so what's the difference between these two, if any? Can you go deeper as why there are two ways of assigning pointer if they achieve the same goal? Are they fundamentally different?

Secondly, some people call '&' reference sign, some say it's 'get address of' can some one clear this out for me ? More over what's the difference between reference and pointer?

Thanks a lot

Linus

  • They are not equivalent, `*ptr=b1;` will copy the object. For your second question the answer is `&` means different things in different contexts, like many of C++'s operators and keywords. For your third question please read a good introductory C++ book. – user657267 Oct 03 '14 at 07:29
  • 3
    In your code `*ptr=b1;` raises undefined behavior. `ptr` isn't initialized with a valid address, at that point. – πάντα ῥεῖ Oct 03 '14 at 07:32
  • _"some people call '&' reference sign, some say it's 'get address of'"_ and some say its 'bit-wise and'. So why not clarify those from a good book, professor lecture, good video tutorial or a web search ? – P0W Oct 03 '14 at 07:33
  • As for the question (though you haven't used references) see here please: http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in – πάντα ῥεῖ Oct 03 '14 at 07:35
  • The symbol "&" has three different meanings in C++. Which one depends on the context. – molbdnilo Oct 03 '14 at 09:50
  • It's like the difference between a person and the house where that person lives. `house = &person` tells you where the person lives. `*house` tells you who lives in the house. `*house = person` gives the house a new occupant. – molbdnilo Oct 03 '14 at 09:58

3 Answers3

1

When you do *ptr = b1, what you are doing is as follows: 1) Dereferencing the object that is already at the memory location pointed to be ptr 2) Copying over the members of b1 to the object deferenced in 1

Which is why, your code will give you a segmentation fault if you utilised a member variable. Consider the following modification for your class:

#include <iostream>
#include <string>

class Base
{
public:
    Base():name("undefined")
    {
    }

    Base& operator=(const Base& other){
        std::cout << "Copying from " << other.name << " to " << name << std::endl;
        name = other.name;
        return *this;
    }

    void fc(){
        cout<<"Class function is called"<<endl;
    }

    std::string name;
};

Now, with the above modifications, the following code will segfault:

int main()
{   Base *ptr; ptr=0;
    Base b1,b2;
    *ptr=b1; // will segfault here
    ptr->fc();
    ptr=0;
    ptr=&b2;
    ptr->fc();
    return 0;
}

In the second case, i.e. when you call

ptr=&b2;

what you are doing is merely setting the address of the memory location that ptr points to. So, the following code WILL run fine:

int main()
{   Base *ptr; ptr=0;
    Base b1,b2;
    ptr=&b2;
    ptr->fc();
    return 0;
}

A more illustrative example will be the following:

int main()
{   Base *ptr;

    Base objA;
    objA.name = "objA";
    Base objB;
    objB.name = "objB";

    ptr = new Base();
    ptr->name = "ptrObj";
    *ptr = objA;
    std::cout << "Ptr is now " << ptr->name << std::endl;

    ptr = &objB;
    std::cout <<  "Ptr is now " << ptr->name << std::endl;    

    return 0;
}

Which gives the following output:

Copying from objA to ptrObj
Ptr is now objA 
Ptr is now objB
balajeerc
  • 3,998
  • 7
  • 35
  • 51
0

You can assign object to pointer by *ptr=b1; or ptr=&b1; so what's the difference between these two,

First one de-references the ptr and changes the value in the address that ptr points to. Second one changes where ptr points in the memory to the address of b1.But in your example since the ptr doesn't point to a valid address in memory dereferencing it will cause undefined behaviour.

Secondly, some people call '&' reference sign, some say it's 'get address of' can some one clear this out for me ?

It depends on the context.For example when you use it like ptr = &b1 it means address-of operator. When you use it in a declaration it means a reference for ex: int &i; means i is a reference to an int.If you use it in a boolean expression it means AND or possibly Bitwise AND operator.There may be other scenarious that I missed but the important thing is to know that what it means depends on where you are using it.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

@balajeerc you'll have memory leak that way... You're pointing to object objB now, and memory that's reserved earlier can't be accessed anymore nor freed.

tomdzon
  • 91
  • 1
  • 3