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.
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.
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.
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.