Assignment Operator Access Violation

This is a discussion on Assignment Operator Access Violation within the C++ Programming forums, part of the General Programming Boards category; I've looked around a bit on Google and at a few different articles but I'm pretty sure I'm not looking ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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 01:54 PM. Reason: Bit of clarification.
    - Leeor

  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
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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.
    - Leeor

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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!
    - Leeor

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, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21