Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memset an object

Tags:

c++

#include <iostream>
#include <string.h>
using namespace std;

class A
   {
     private:
  int a;
  int b;
  public:
  A():a(10),b(20){};
  A(int ad,int bd):a(ad),b(bd){};
  void printvalues()
  {
        cout<<a << " " <<b<<endl;
  }
};

int main()
{

  A a(5,12);

  memset(&a,sizeof(A),0);
  a.printvalues();

  return 0;
}

memsetting the object to 0, do not seem to have any effect on the object. Can anyone help me understand this behavior. output: 5 12

like image 235
vamsi Avatar asked Jul 27 '26 04:07

vamsi


1 Answers

You have the arguments to memset the wrong way round. It's memset(addr, value, number).


Note: In C++, memset is usually avoided.
like image 68
Oliver Charlesworth Avatar answered Jul 29 '26 21:07

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!