Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Sizeof Class object creates a problem. (http://www.go4expert.com/forums/sizeof-class-object-creates-t26116/)

hrdksanghavi 22Jun2011 11:36

Sizeof Class object creates a problem.
 
Hello,

I have created a class structure and use the object to store value from buffer.

Code:

Class Data1
{
public:
    long Srno;
    short BookType;
    short Status;
};
 
Class Data2
{
public:
    long Srno;
    short BookType;
};
 
int main(int argc, char** argv)
{
    Data1 objdata1;
    Data2 objdata2;
 
    short SizeData1 = 0;
    short SizeData2 = 0;
 
    SizeData1 = sizeof(objdata1);
    SizeData2 = sizeof(objdata2);
 
    memcpy(&objdata1,buffer,SizeData1);
    memcpy(&objdata2,buffer,SizeData2);
 
 
}

Quote:

OutPut
=====
SizeData1 = 8
SizeData2 = 8

But data not coppied properly as the SizeData2 should be 6 instead of 8.
I don't know why the size is coming 8 instead of 6.
Pls help me to come out of this.

Hardik Sanghavi.

xpi0t0s 23Jun2011 13:17

Re: Sizeof Class object creates a problem.
 
The code doesn't compile as given; it's class not Class, and buffer isn't defined, and the program doesn't display any output. So this isn't the code that gave you the unexpected result and it would be easier if you posted the code that did.

I also get 8 for both in Visual Studio 2010. Probably the compiler allocates memory in chunks of 4 bytes, padding where it can, so long(4)+short(2)+short(2)=8 bytes, and long(4)+short(2)+unused(2) also=8 bytes. I suspect your compiler (and mine) will only allocate memory in chunks of 4 bytes so that everything is aligned to 4-byte boundaries.

luptakticks 1Jul2011 12:29

Re: Sizeof Class object creates a problem.
 
Hello, I am currently a student at University of Advancing Technology and after compiling this by changing the Class to class (as mentioned above) and adding the proper heading (noted below) I ran into no memory errors. The only errors I ran into was that 'buffer' was not declared. Here is what I ran:
Code:

#include <iostream>  //  Input/output library
using namespace std;


class Data1
{
public:
    long Srno;
    short BookType;
    short Status;
};
 
class Data2
{
public:
    long Srno;
    short BookType;
};
 
int main(int argc, char** argv)
{
    Data1 objdata1;
    Data2 objdata2;
 
    short SizeData1 = 0;
    short SizeData2 = 0;
 
    SizeData1 = sizeof(objdata1);
    SizeData2 = sizeof(objdata2);
 
    memcpy(&objdata1,buffer,SizeData1);
    memcpy(&objdata2,buffer,SizeData2);
 
 
}

and the errors were:

error C2065: 'buffer' : undeclared identifier
error C2065: 'buffer' : undeclared identifier

Hope this helps.

xpi0t0s 1Jul2011 21:55

Re: Sizeof Class object creates a problem.
 
The reason you get "buffer: undeclared identifier" is...er...that buffer is an undeclared identifier. Simple, eh? The plain English reading of most errors points you in the right direction.

Solution: define buffer and that should solve the problem. Oh, and you'll need to put something in it of course, for memcpy to copy stuff from.


All times are GMT +5.5. The time now is 11:42.