Memset on a struct with a class object in it?

What would the effect of memset be here:

Page 1 of 1

11 Replies - 4531 Views - Last Post: 13 March 2010 - 06:59 AM Rate Topic: -----

#1 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Memset on a struct with a class object in it?

Posted 12 March 2010 - 10:52 PM

What happens if I use memset to zero a structure with a class object in it???

if I have:
struct UserAccount
{
	long int pin;
	string userName; //<-- string class object
	double balance;
};



and I do:

UserAccount currentUser;
memset( &currentUser, 0, sizeof(UserAccount) );



also, is it just a matter of preference to do use the type for the third parameter, or is there a rule to use the name of the type instead, in case someone should change the type, or for any other reason(s) ??

What about using memset() on a class??

This post has been edited by taylorc8: 12 March 2010 - 10:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Memset on a struct with a class object in it?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:06 PM

I would be inclined to avoid doing that. I'm not sure, but I'd be afraid that that would cause a memory leak. You'd be deleting the address of the string's contents, but I doubt that that would invoke the string's destructor, so the memory allocated to the string's contents would be lost. Just my guess.

Quote

What about using memset() on a class??

Assuming you mean a class object, it's no different than a struct. If it doesn't contain a pointer to dynamically-allocated memory (or a member variable which in turn contains a pointer to dynamically-allocated memory -- like a string) you should be OK. But if using memset means directly or indirectly erasing an address which points to dynamically-allocated memory, that would be bad.

This post has been edited by r.stiltskin: 12 March 2010 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:10 PM

View Postr.stiltskin, on 12 March 2010 - 10:06 PM, said:

I would be inclined to avoid doing that. I'm not sure, but I'd be afraid that that would cause a memory leak. You'd be deleting the address of the string's contents, but I doubt that that would invoke the string's destructor, so the memory allocated to the string's contents would be lost. Just my guess.


Well, a quick google turned up something like this:
http://www.devx.com/tips/Tip/14473

and another page
http://forum.codecal...g-c-struct.html

So, I would say, I could put a constructor in the struct to memset the members that aren't floating point or class objects, and also initialize the class objects. :)
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:26 PM

Actually, I wonder what the worry is about floating-point numbers. Under IEEE754, zeros in all the bits does represent 0. I believe that standard applies to all PCs and Macs.

As far as the strings are concerned, you could make userName a c-string instead & then it would be perfectly safe to use memset.
Was This Post Helpful? 0
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:30 PM

I'm aware of using an array for the username, but a constructor seems like a good idea, because every struct will be zeroed when it's created, and this does happen when someone creates an array of them too, doesn't it?
struct UserAccount
{
	UserAccount()
	{
		pin=0;
		userName="";
		balance=0.0;
	}
	long int pin;
	string userName;
	double balance;
};


UserAccount users[20]; //constructor zeros them all?


This post has been edited by taylorc8: 12 March 2010 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:35 PM

View Posttaylorc8, on 13 March 2010 - 01:10 AM, said:

So, I would say, I could put a constructor in the struct to memset the members that aren't floating point or class objects, and also initialize the class objects. :)


Wait a minute -- I just realized that all you're trying to do is initialize a new struct instance -- so why do you want to bother with memset at all?

What's wrong with
struct UserAccount
{
    long int pin;
    string userName; //<-- string class object
    double balance;

    UserAccount() : pin(0), balance(0.0) {}     
};


You don't have to worry about the string: it's automatically initialized to an empty string.

And if you have a struct that has (your own) class objects as member variables, the default constructors of your class objects should take care of correctly initializing all of their member variables.
Was This Post Helpful? 1
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:41 PM

I just wanted the knowledge for future use of structs with class objects in them, not necessarily using them in a class to manage it.

But, i remember something about this line:

UserAccount() : pin(0), balance(0.0) {} 


What topic should I look up on that again? I think if I was learning C I would know it's intracacies by now, C++ seems to have a little more to learn about oop..

Also, I have a small question about working with files for this project (it's not homework)

I have a marker at the beginning of my file with a 4 byte ID I have chosen (4 letters) and bitwise &'d them with 0x80 (a trick from an old C book) which is supposed to make them beyond the range of printable characters.. but, the way I am doing this, I have opened the file in binary mode, read the 4 bytes to compare with the auth id, and afterwards opened in standard
ios::in only mode, and seeked the get pointer 4 bytes into the file.

Ok, now can I still do ifstreamobj >> variable; with no problems, and does the seekg() work the same in text mode??

This post has been edited by taylorc8: 12 March 2010 - 11:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Memset on a struct with a class object in it?

Posted 12 March 2010 - 11:50 PM

UserAccount() : pin(0), balance(0.0) {} 


This represents the struct constructor. You may be a little confused about that syntax, but that line is equivalent with:
UserAccount()
{
    pin = 0;
    balance = 0;
}


As you can see, the functionality is the same. Just a different syntax. You choose which one to use ...
Classes can also have constructors. You can read here for more info: http://www.codersour...rial-class.aspx

This post has been edited by sarmanu: 12 March 2010 - 11:50 PM

Was This Post Helpful? 1
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Memset on a struct with a class object in it?

Posted 13 March 2010 - 12:01 AM

Oh well, sarmanu beat me to it, but I guess I'll throw this up here anyway in case it adds anything.


This
UserAccount() : pin(0), balance(0.0) {} 
is just a constructor for the struct. In C++, a struct is essentially the same as a class. The difference is that in a class, all members are private by default, and in a struct members are public by default. So just as a class has a constructor, so does a struct. (If you don't write one, the compiler supplies a default constructor.)

This: UserAccount() is obviously the struct name and empty argument list.
This part: : pin(0), balance(0.0) is an "initializer list" which sets the initial value of the int pin to 0 and the initial value of the double balance to 0.0.
and this {} is the body of the constructor. Since I did all the work of the constructor in the initializer list, there is nothing to do in the body, but every function needs a body so this one gets an empty body.
Was This Post Helpful? 1
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Memset on a struct with a class object in it?

Posted 13 March 2010 - 12:16 AM

View Posttaylorc8, on 13 March 2010 - 01:41 AM, said:

I have a marker at the beginning of my file with a 4 byte ID I have chosen (4 letters) and bitwise &'d them with 0x80 (a trick from an old C book) which is supposed to make them beyond the range of printable characters..


I don't get this. Are you sure you don't mean bitwise or? If those 4 bytes are ordinary ASCII chars they're all values of less than 128, so the high-order bit is 0 for all of them. The byte 0x80 in binary is 10000000. If you & that with, say, 01111111, the result is 00000000. In fact, if you and 10000000 with 0??????? (where each ? is either 0 or 1), you get all 0s.

On the other hand, if you or the letters with 0x80 it replaces their leading 0 with a 1 (essentially making them negative).
Was This Post Helpful? 0
  • +
  • -

#11 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Memset on a struct with a class object in it?

Posted 13 March 2010 - 01:00 AM

Hmm, I'll have to check up on that
I think it was a
0x80808080

or something.. but, I need some sleep, guys..
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Memset on a struct with a class object in it?

Posted 13 March 2010 - 06:59 AM

It would still have to be |, not &.

With & the result bit is 0 unless the corresponding bit in both operands is 1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1