Sizeof Class object creates a problem.

Discussion in 'C' started by hrdksanghavi, Jun 22, 2011.

  1. hrdksanghavi

    hrdksanghavi New Member

    Joined:
    Jun 6, 2011
    Messages:
    6
    Likes Received:
    2
    Trophy Points:
    0
    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);
     
     
    }
    
    I don't know why the size is coming 8 instead of 6.
    Pls help me to come out of this.

    Hardik Sanghavi.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
    shabbir likes this.
  3. luptakticks

    luptakticks New Member

    Joined:
    Jul 1, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
    Last edited by a moderator: Jul 1, 2011
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice