get sizeof() to return size of a class

get sizeof() to return size of a class

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 8551 Views - Last Post: 25 November 2010 - 02:44 PM Rate Topic: -----

#1 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

get sizeof() to return size of a class

Posted 23 November 2010 - 11:13 PM

hello guys. i have a problem that can only be solved if i can get the sizeof() function to return the sizeof a class. The only problem is that the class that i am trying to get the size of is a subclass that is derived from a parent class and whenever i enter the class name into the function it return the size of the parent class.

e.g.
Parent p("");
Subclass sub(1); // subclass of parent

seekp(2 * sizeof(Subclass)); // this returns the size of parent


does anyone have any ideas how to get around this problem.

p.s ultimately i need it to word because i am trying to create a random acessfile that can store the variables in the 'sub' constructor.

Is This A Good Question/Topic? 0
  • +

Replies To: get sizeof() to return size of a class

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:31 AM

http://www.parashift...ialization.html
You can't just "write the bytes" of a class and expect it to work.

Even simple stuff like this in C
struct foo {
  int a;
  char c;
  double d;
} bar;
fwrite( &bar, sizeof(bar), 1, fp );


was only really guaranteed to work so long as you didn't recompile the program.

In C++, where classes can have hidden information (particularly when you start inheriting things), then simply "writing the bytes" means you write all that hidden information as well.

But unlike the C case, the data you write is only good (at best) for the life of the program. It is highly unlikely that you could just quit the program, re-run and then "load the bytes" into a clean object and expect it to work.
Was This Post Helpful? 0
  • +
  • -

#3 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:01 PM

View Postjamaican1231, on 23 November 2010 - 10:13 PM, said:

hello guys. i have a problem that can only be solved if i can get the sizeof() function to return the sizeof a class. The only problem is that the class that i am trying to get the size of is a subclass that is derived from a parent class and whenever i enter the class name into the function it return the size of the parent class.

e.g.
Parent p("");
Subclass sub(1); // subclass of parent

seekp(2 * sizeof(Subclass)); // this returns the size of parent


does anyone have any ideas how to get around this problem.

p.s ultimately i need it to word because i am trying to create a random acessfile that can store the variables in the 'sub' constructor.


i am not sure that i get what you are saying. the write function i am using is:

X y; // class object

ifstream file("something.txt",ios::out | ios::in | ios::binary);

seekp(sizeof(<X));

file.write(reinterpret_cast<const char *>(y),sizeof(X));

trying to get the information in y to be stored on a random acess file while x is a subclass of another class .. say Z for example. what happens is that it returns the size of the z class so it dosent store the data correctly .

View PostSalem_c, on 24 November 2010 - 08:31 AM, said:

http://www.parashift...ialization.html
You can't just "write the bytes" of a class and expect it to work.

Even simple stuff like this in C
struct foo {
  int a;
  char c;
  double d;
} bar;
fwrite( &bar, sizeof(bar), 1, fp );


was only really guaranteed to work so long as you didn't recompile the program.

In C++, where classes can have hidden information (particularly when you start inheriting things), then simply "writing the bytes" means you write all that hidden information as well.

But unlike the C case, the data you write is only good (at best) for the life of the program. It is highly unlikely that you could just quit the program, re-run and then "load the bytes" into a clean object and expect it to work.



i am not sure that i get what you are saying. the write function i am using is:

X y; // class object

ifstream file("something.txt",ios::out | ios::in | ios::binary);

seekp(sizeof(<X));

file.write(reinterpret_cast<const char *>(y),sizeof(X));

trying to get the information in y to be stored on a random acess file while x is a subclass of another class .. say Z for example. what happens is that it returns the size of the z class so it dosent store the data correctly .
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:02 PM

You didn't understand salem's point? Then, let's look at what you just showed us.

file.write(reinterpret_cast<const char *>(y),sizeof(X));
What exactly does this do?

Quote

what happens is that it returns the size of the z class
How do you know you get the size of the Z class and not the X class?
Was This Post Helpful? 0
  • +
  • -

#5 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:12 PM

View PostOler1s, on 24 November 2010 - 07:02 PM, said:

You didn't understand salem's point? Then, let's look at what you just showed us.

file.write(reinterpret_cast<const char *>(y),sizeof(X));
What exactly does this do?

Quote

what happens is that it returns the size of the z class
How do you know you get the size of the Z class and not the X class?


u have to bear with me bc i have just strted to learn c and object oriented programming in school.

anyway i know because i created a text file with the z class and filled it with null characters and then did the same thing for the x class as a test to c if my theory was right and when i opened the file they appeared to be identical.

This post has been edited by jamaican1231: 24 November 2010 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:14 PM

Quote

u have to bear with me bc i have just strted to learn c and object oriented programming in school.
Sure, you don't need to apologize for a lack of knowledge. But you are programming this, so we are asking relevant questions for you to consider.

You ignored my question about your code. Please answer that.

Quote

anyway i know because i created a text file with the z class and filled it with null characters and then did the same thing for the x class as a test to c if my theory was right and when i opened the file they appeared to be identical.
So...this is what you told me.

You created a Z class and zeroed out data members. You wrote the byte representation to file, and saw that a bunch of zeroes were written. Then you created an X class and zeroed out data members. You wrote the byte representation to file, and saw that a bunch of zeroes were written. And because a bunch of zeroes were written, you conclude that sizeof(X) is actually getting you sizeof(Z)?

I just want to confirm. Is this the logical argument you are making?
Was This Post Helpful? 0
  • +
  • -

#7 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:18 PM

View PostOler1s, on 24 November 2010 - 07:14 PM, said:

Quote

u have to bear with me bc i have just strted to learn c and object oriented programming in school.
Sure, you don't need to apologize for a lack of knowledge. But you are programming this, so we are asking relevant questions for you to consider.

You ignored my question about your code. Please answer that.

Quote

anyway i know because i created a text file with the z class and filled it with null characters and then did the same thing for the x class as a test to c if my theory was right and when i opened the file they appeared to be identical.
So...this is what you told me.

You created a Z class and zeroed out data members. You wrote the byte representation to file, and saw that a bunch of zeroes were written. Then you created an X class and zeroed out data members. You wrote the byte representation to file, and saw that a bunch of zeroes were written. And because a bunch of zeroes were written, you conclude that sizeof(X) is actually getting you sizeof(Z)?

I just want to confirm. Is this the logical argument you are making?


yeh. and the null characters were all in the same position in the file with the exact same spacing. I don't know if it makes sense to people that know a lot about how data is stored on files but it made sense to me at the time. now that i think about it i should have compared the two classes with the sizeof funtion in an if statement to c if they were equivalent.
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:31 PM

Quote

now that i think about it i should have compared the two classes with the sizeof funtion in an if statement to c if they were equivalent.
And what if they are the same? What if sizeof(X) == sizeof(Z)? Then what? They can't be the same? Why not? They can be the same? Then you don't have a problem with sizeof(X).

Of course, all of this dancing is pointless. Largely, you aren't dealing with your own broken approach. That's what I asked you explain what file.write line of code was doing. That is code you wrote, yes? If you wrote that code, you should be able to tell me what it is doing.
Was This Post Helpful? 0
  • +
  • -

#9 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 08:44 PM

View PostOler1s, on 24 November 2010 - 07:31 PM, said:

Quote

now that i think about it i should have compared the two classes with the sizeof funtion in an if statement to c if they were equivalent.
And what if they are the same? What if sizeof(X) == sizeof(Z)? Then what? They can't be the same? Why not? They can be the same? Then you don't have a problem with sizeof(X).

Of course, all of this dancing is pointless. Largely, you aren't dealing with your own broken approach. That's what I asked you explain what file.write line of code was doing. That is code you wrote, yes? If you wrote that code, you should be able to tell me what it is doing.


ok. What it is doing is writing the data that is in the constructor of the class to a file. i use a seekp function to determine the the position in the file to store the information.

seekp(sizeof(<the class>) * <another variable>);
// i use this along with the write function to store the data

filename.write(reinterpret_cast(const char *)(&<constructor>),sizeof(<classname>) );

this method worked when i used it to store data in the Base class but when I tried to use it to store data in the derived class it did not reserve the correct amount of space to store the data(which i suspect has something to do with the fact that the constructor for the base class is called in the derived class automatically).

Soo basically this is my problem. its really annoying bc i am trying to finish my project b4 friday and this is the only thing getting in my way.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:00 PM

Quote

What it is doing is writing the data that is in the constructor of the class to a file
No...You are writing the data representation of the object itself to file. The constructor is just a function. You are not writing a function pointer to a file. You are writing whatever is in memory for the object to file.

And you contradict yourself too. Look, you said: filename.write(reinterpret_cast(const char *)(&<constructor>),sizeof(<classname>) ); Now, look at your code: file.write(reinterpret_cast<const char *>(y),sizeof(X)); Do you see an & there? I don't. Do you see a constructor there? X y; // class object Well, y isn't the constructor, it's the object.

You are writing whatever is in memory to file. And here's what Salem said: "the data you write is only good (at best) for the life of the program. It is highly unlikely that you could just quit the program, re-run and then "load the bytes" into a clean object and expect it to work."

That data is only meaningful when your program is running. It's not something that can be stored directly outside of the program, while still being meaningful.

You can, however, write out the individual variables to a file, and then read them back in and load that data into an object.
Was This Post Helpful? 0
  • +
  • -

#11 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:12 PM

View PostOler1s, on 24 November 2010 - 08:00 PM, said:

Quote

What it is doing is writing the data that is in the constructor of the class to a file
No...You are writing the data representation of the object itself to file. The constructor is just a function. You are not writing a function pointer to a file. You are writing whatever is in memory for the object to file.

And you contradict yourself too. Look, you said: filename.write(reinterpret_cast(const char *)(&<constructor>),sizeof(<classname>) ); Now, look at your code: file.write(reinterpret_cast<const char *>(y),sizeof(X)); Do you see an & there? I don't. Do you see a constructor there? X y; // class object Well, y isn't the constructor, it's the object.

You are writing whatever is in memory to file. And here's what Salem said: "the data you write is only good (at best) for the life of the program. It is highly unlikely that you could just quit the program, re-run and then "load the bytes" into a clean object and expect it to work."

That data is only meaningful when your program is running. It's not something that can be stored directly outside of the program, while still being meaningful.

You can, however, write out the individual variables to a file, and then read them back in and load that data into an object.


i think i c what you are saying. but. y did this method work 4 the base class (the information is on the file and i can print this data as long as i have the starting bite position for the data). this is the exact code i use

void Guest::createfile(int roomno, Guest guest)
{
ofstream Guestfile("Guestfiles.txt",ios::in |ios::out | ios::binary);

Guestfile.seekp((roomno - 1) * sizeof(Guest));

Guestfile.write(reinterpret_cast<const char *>(&guest),sizeof(Guest));

Guestfile.close();
}

it works perfectly. and i use this to read from it

void Guest::displayfile(int roomno, Guest data)
{
ifstream Guestfile("Guestfiles.txt",ios::in | ios::out | ios::binary);

Guestfile.seekg((roomno - 1) * sizeof(Guest));

Guestfile.read(reinterpret_cast<char *>(&data), sizeof(Guest));

cout <<data.getfname()<<setw(10) << data.getlname() <<setw(10)<<data.getID()<<setw(10)
<< data.getaddress()<<setw(10)<<data.getbooking_type()<<setw(10)<<data.getcontactno()<<setw(10)
<< data.getcategory();
Guestfile.close();
}

and it also works.

the data only gets corrupted when i use the same method 4 the base class

void Vehicle::createfile(int roomno,Vehicle v)
{
ofstream Vehiclefile("Vehiclefile.txt",ios::in |ios::out | ios::binary);

Vehiclefile.seekp((roomno - 1) * sizeof(Vehicle));

Vehiclefile.write(reinterpret_cast<const char *>(&v),sizeof(Vehicle));

Vehiclefile.close();
}
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:25 PM

Quote

i think i c what you are saying. but. y did this method work 4 the base class
You are playing with fire. What happens utterly depends on how everything is implemented in memory. If it worked for you, it's because you got lucky. Not because you wrote proper code.

Let's assume your code works for you. Your project is done. You hand it in. Your teacher compiles and runs the code. Will it work for him too? Only if he gets lucky too.

So please stop trying to use this approach of writing and reading memory directly, and properly serialize data member by member.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:26 PM

View PostSalem_c, on 24 November 2010 - 11:31 AM, said:

http://www.parashift...ialization.html
You can't just "write the bytes" of a class and expect it to work.

Even simple stuff like this in C
struct foo {
  int a;
  char c;
  double d;
} bar;
fwrite( &bar, sizeof(bar), 1, fp );


was only really guaranteed to work so long as you didn't recompile the program.

In C++, where classes can have hidden information (particularly when you start inheriting things), then simply "writing the bytes" means you write all that hidden information as well.

But unlike the C case, the data you write is only good (at best) for the life of the program. It is highly unlikely that you could just quit the program, re-run and then "load the bytes" into a clean object and expect it to work.


Why is that so? From time to time there have been threads based on assignments that used binary files essentially as databases, where each record is a class or struct object, using istream::write() to save entire records to the file, searching for records in the file and using istream::read() to load blocks of data into objects. It seemed to work (even after restarting the program) as long as the records consisted only of primitive data types (or arrays of same). Here's one example. From time to time I recall seeing other similar ones. The students didn't invent this approach. They clearly were instructed to do this. (Of course I don't claim that every teacher knows what he's doing.)

Is there something inherently wrong with doing that? Were those instructors completely wrong in suggesting this procedure?

I can see that on different machines a given structure may be stored differently. But is there any danger where the file is being written and read on the same machine?

This post has been edited by r.stiltskin: 24 November 2010 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#14 jamaican1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 10

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:39 PM

View PostOler1s, on 24 November 2010 - 08:25 PM, said:

Quote

i think i c what you are saying. but. y did this method work 4 the base class
You are playing with fire. What happens utterly depends on how everything is implemented in memory. If it worked for you, it's because you got lucky. Not because you wrote proper code.

Let's assume your code works for you. Your project is done. You hand it in. Your teacher compiles and runs the code. Will it work for him too? Only if he gets lucky too.

So please stop trying to use this approach of writing and reading memory directly, and properly serialize data member by member.


that is exactly how they do it in the book c++ how to programme by deitel and deitel (i just made a function of the same method that they use).

and i let the end user enter the information into the class object via cin >> set<whatever variable>();

and Class c(get<whatevervariable>());

if that makes a difference.

besides

i have a feeling that i am not describing what i am trying to do correctly

i might end up having to use sequential access files and store the data 4 each guest's vehicle in a separate file. but that would not be ideal.
Was This Post Helpful? 0
  • +
  • -

#15 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: get sizeof() to return size of a class

Posted 24 November 2010 - 09:55 PM

Quote

Is there something inherently wrong with doing that? Were those instructors completely wrong in suggesting this procedure?
The representation is not defined by the standard. Consequently, shuffle around data members, or different compiler options, or maybe your compiler itself isn't deterministic in how it compiles, and you're screwed. At best, you can make good gambles with POD types. Never a guarantee if you just write the class to memory.

Is this often done by programmers? Sure. Does it make it correct? No. People write bad code. Instructors teach wrong things. Water is wet.

Quote

i have a feeling that i am not describing what i am trying to do correctly
I completely understand what you are trying to do. You are trying to serialize data and write that stream to file. But you can't serialize by just writing the bytes to file.

It's not hard. It looks like you can write everything as text. Write each record to file by writing member by member. You can define a certain format for your text file (name on the first line, id on the next line, etc. repeat for each record).

When reading from the file, you just have to follow this format.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2