![]() |
|
#1
|
|||
|
|||
Assignment Operator and PointerRecently, i have found out one article where best explained the assignment operator where you can found at
http://www.icu-project.org/docs/pape..._operator.html This article describes the underlying C++ memory management and assignment operator. Basically, i not very understand the whole articles. I still prefer to have some picture. Consider the following class definition: CPP / C++ / C Code:
You have a class, TFoo, which descends from a class, TSuperFoo, and which has two data members, both of which are pointers to objects of class TBar. For the purposes of this exercise, consider both pointers to have owning semantics and TBar to be a monomorphic class. Write the assignment operator for this class. Solution: CPP / C++ / C Code:
As far as i know, owner pointer is the first pointer to the object and aliasing pointer is the second pointer pointed to the object. (Correct me if i wrong) CPP / C++ / C Code:
Please correct me if i wrong. I don't understand the operation of the code. Therefore, i hope someone will get clear my doubts. My Question is i don't understand the algorithm inside the assignment operator. Why we need to delete before copied ? After delete the memory, how we can copy to another object ? CPP / C++ / C Code:
I hope you all can help me out because i have post the same thread more than 4 forums. Thanks for your help. Your help is greatly appreciated by me and others. __________________
Linux is the best OS in the world. Last edited by LuciWiz : 04-Sep-2007 at 09:26.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Assignment Operator and PointerI know i should not urge for help but i really need your help.
I hope you all can help me. Please help me. Thanks. __________________
Linux is the best OS in the world. |
|
#3
|
|||
|
|||
Re: Assignment Operator and PointerQuote:
You need to look at the assignment operator! CPP / C++ / C Code:
We are taking a const reference to the type. However, when we delete the memory pointed to by m_name, we're deleting whatever memory was allocated by the this instance of A not the instance of the reference we received in this operation. How do we know that? Because we checked it! CPP / C++ / C Code:
We are "freeing" this.m_name NOT rhs.m_name. In fact, since rhs is const, we can not free it. So when we invoke: CPP / C++ / C Code:
...we are passing in rhs.m_name, but we are deleting this.m_name: CPP / C++ / C Code:
Since we don't see how the ctor was implemented, we can only assume that the memory for m_name was dynamically allocated in the ctor (or the pointer is assigned as NULL). Furthermore, since there is no defined "setName" operation, we don't know what to expect for sure, however, we should see something very similar to this assignment operator in that setName would check to see if there was a valid m_name pointer, if so, delete it, then allocate new memory for the char* passed in on the SetName call and then strcpy the passed in value to the m_name. In fact, we'd probably expect setData to be a private operation and setName to be a public operation that simply invokes setData. ...unless, of course, m_name should really be "m_data" or setData should really be "setSentence" or whatever! It is relatively impossible to know what would be appropriate when using bogus names like "A" for the class name...when you have "conflicting" names at work. I'd recommend using more whitespace in the code that you post so that it is clearer to see all of the operators and the interactions within the code. :davis: |
|
#4
|
|||
|
|||
Re: Assignment Operator and PointerThe program is as below:
CPP / C++ / C Code:
Can you provide me the explicit call of below ? A second, first; second = first; Therefore, i can even understand better. As far as i know, when we invoked a member function, the instance of the class is a implicit this pointer. Do i correct ? Can u explain detail from the explicit call to the assignment operator ? I will very appreciated your help. Thanks for your help. __________________
Linux is the best OS in the world. Last edited by LuciWiz : 09-Sep-2007 at 03:14.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#5
|
|||
|
|||
Re: Assignment Operator and PointerQuote:
I don't know exactly what you're asking. Hopefully this will help: CPP / C++ / C Code:
Output: Code:
ctor <---> bar1 ctor <---> bar2 assignment <---> bar1 = bar2 cctor <---> Foo bar3 = bar1 dtor <---> bar1 going out of scope dtor <---> bar2 going out of scope dtor <---> bar3 going out of scope :davis: |
|
#6
|
|||
|
|||
Re: Assignment Operator and PointerFor instance, this statement
string final = first + second; is equal to the explicit call as below. first.operator+(second). Therefore, i need to know the explicit call of assignment operator. What is the difference between shadow copy and deep copy ? Picture explanation is greatly appreciated by me and others. How to represent in C++ code ? Thanks for your help. __________________
Linux is the best OS in the world. |
|
#7
|
||||
|
||||
Re: Assignment Operator and PointerQuote:
The assignment operator has a signature that looks like: CPP / C++ / C Code:
Quote:
In a class where a member is a pointer type, a shallow (not shadow) copy is where the cctor copies only the pointer, not what is being "pointed to." A "deep" copy is when what is being pointed to is also copied. This is made rather obvious in the case of the char* member pointer. The copy and the original both point to the same memory. When the original or the copy goes away, the other now points to "bad" memory. In the case of a cctor (also assignment operator) that is not implemented by user defined code, that is, the default cctor is used, what happens is that the compiler simply copies the pointer of first to the member pointer of second. In a case where dynamic memory is used and deleted in the dtor, then a double "free" is encountered, since the dtor of first will delete the pointer and second's dtor will try again to delete a copy of a pointer with the same value as first's. Quote:
See: www.fredosaurus.com ...for a very simple explanation. Sorry, no pictures, though. Quote:
How to represent shallow and deep copies? Shallow copies are inherently dangerous and very likely to cause a segfault or at least a double free situation. Here is an example that attempts to free the same memory twice, which is just one of the potential hazards of a shallow copy. shallow.cpp CPP / C++ / C Code:
...here is an example that performs the deep copy/assignment: deep.cpp CPP / C++ / C Code:
:davis: |
|
#8
|
|||
|
|||
Re: Assignment Operator and PointerI really need to understand this issue because this is very basic and important.
FOr instnace, A first, second; second= first; Explicit call of assignment operator. second.operator(first). I don't know whether i correct. In the assignment operator, since we delete the this pointer which in this case is second. Why we need to delete the second->ptr memory. Is something wrong with my explicit call of assignment operator ? Thanks for your help. A picture explanation on how to operation worked is greatly appreciated by me and others as well. A billion thank to your help and explanation. Thanks for your hep. __________________
Linux is the best OS in the world. |
|
#9
|
|||
|
|||
Re: Assignment Operator and PointerQuote:
second.operator=( first ) ...is the operation that is invoked in second = first. Quote:
You don't delete the this pointer. You delete the MEMBER POINTER that represents the dynamic memory contained within the class. The assignment operator and deleting the member pointer are different issues. If the class doesn't contain any members that are pointers to dynamically allocated memory, then there is no issue of shallow/deep copy and no issue of deleting the member pointer's memory. CPP / C++ / C Code:
Quote:
Why are you repeating the previous post? I realize that you prefer to have a picture. Imagine the picture. Two different classes, both have a pointer to the same memory as pointed to by some MEMBER POINTER. One of these classes goes out of scope, and therefore, its dtor is invoked causing the memory pointed to by its member pointer to be deleted. However, the other class has not gone out of scope and tries to access its member pointer to the same memory location, but that memory location now contains invalid data because the previous class deleted it when it went out of scope. That is only one example of the problems of a shallow copy. There are numerous others. The "double delete" where the first class goes out of scope and deletes its member pointer, then the second class goes out of scope and tries to delete its copy of the pointer is another problem. :davis: |
|
#10
|
|||
|
|||
Re: Assignment Operator and PointerI really appreciated you explanation. I understand what u mentioned in post 9 but not the whole concept of assignment operator.
Why u delete the second Member pointer ? Do i should create a new sources of first and let second point to it without worry the double delete and dangling pointer. Sorry for my stupidity. A billion thanks for your help. Linux guys have more technical knowledge over window guys. You are really a brilliant programmer. __________________
Linux is the best OS in the world. |
Recent GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The