Thread: Assignment Operator Access Violation

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147

    Assignment Operator Access Violation

    I've looked around a bit on Google and at a few different articles but I'm pretty sure I'm not looking for the right information.

    In the following scenario, I have a very basic assignment operator:

    Code:
    class MyClass
    {
    public:
    	MyClass(Bar *bar) : myBar(bar) {};
    	MyClass(): myBar(NULL) {};
    	MyClass& operator=(const MyClass &rhs)
    	Bar *myBar;
    };
    
    MyClass& MyClass::operator=(const MyClass &rhs)
    {
    	myBar= rhs.myBar;  // explodes here, access violation at 0x00000000
    }
    
    int main()
    {
    	Bar *someBar = new Bar();
    
    	MyClass *myClass = new MyClass(someBar);
    	MyClass *myClass2 = new MyClass();
    
    	*myClass2 = *myClass;
    
    	return 0;
    }
    As a note, myBar is an object that is constructed outside of MyClass and is

    What I expect is that the address of rhs.myBar be assigned to myBar. However, I get an access violation attempting to write to memory 0x00000000 which tells me that I'm trying to write into a null pointer.

    My first thought was that I should be asking for the address of rhs.myBar like this:

    Code:
    myBar = &rhs.myBar;
    But that only results in the error message "cannot convert from Bar *const *' to Bar*'". I'm pretty sure const casting is not applicable here and I'm pretty sure that my first thought is just plain wrong.

    Obviously my code is incorrect. What am I doing wrong here? Where can I find more information about the particular type of operation I'm trying to do?
    Last edited by leeor_net; 11-26-2009 at 02:54 PM. Reason: Bit of clarification.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Does this work?

    Code:
    MyClass& MyClass::operator=(const MyClass &rhs)
    {
            if (this == &rhs)
            {
                     return *this;
            }
    
    	myBar= rhs.myBar;
            return *this;
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I doubt the error is in that code (you are not dereferencing the pointer anywhere). You shouldn't even need to define the assignment operator yourself.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Quote Originally Posted by anon View Post
    I doubt the error is in that code (you are not dereferencing the pointer anywhere). You shouldn't even need to define the assignment operator yourself.
    In this particular code snippet, no, I don't need to override the assignment operator. I tried to keep it as simple as possible with only the things that are relevant. The class that I actually have defined is far more complex and as car as I can tell the problem lies entirely with my pointer assignment.

    As for the first reply, that is not the issue either as I know for sure that I'm not looking at the same object (different heap addresses) although I have a feeling that'll help to prevent silly errors later on.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Aside from missing ; and missing return statement (and the missing delete statements!), the code compiles and runs fine.
    If you encounter a problem, you would have to post the smallest possible amount of code that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    As it turns out I was looking for the wrong information having misdiagnosed the problem. I didn't think my assignment above was wrong and, upon further inspection I found that I had initialized MyClass2 to NULL (e.g., SomeClass() : myClass2(NULL)) and had then attempted the assignment operator.

    Thank you, everyone, for your time. I always appreciate your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM