+ Reply to Thread
Results 1 to 2 of 2

Thread: memset is causing crash. Why?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    48

    memset is causing crash. Why?

    Please help.

    My following code causes crash if I use memset.

    If I comment code statement here memset is used then it works fine.

    Why it crashes when I use memset?

    Code:

    Code:
    class MyClass
    {
    	int m_nData ;
    public:
    	MyClass()
    	{
    	}
    
    	virtual ~MyClass() {}
    
    	virtual int GetData()
    	{
    		return 111 ;
    	}
    } ;
    
    void UseData()
    {
    	MyClass *pMyClass = new MyClass ;
    	memset(pMyClass, 0, sizeof(MyClass)) ;
    
    	int value = pMyClass->GetData() ;
    
    	delete pMyClass ;
    	pMyClass = NULL ;
    }

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    46
    When you use below statement:

    Code:
    memset(pMyClass, 0, sizeof(MyClass)) ;
    It zeroed out the class object which means there is virtual table points to NULL!

    So you are calling a virtual function of an object with improper virtual table!

    This is the cause of the issue.

    So avoid memset in this case.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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