Click to See Complete Forum and Search --> : Memory layout of classes & structures


anw
December 18th, 2002, 02:47 PM
Back in the old days of ANSI C programming, one could copy the data in one structure to another with code similar to the following:


struct myStruct
{
int var1;
char *var2;
};

struct myStruct s1;
struct myStruct s2;

memcpy(s2, s1, sizeof(struct myStruct));


On most machines, this was a lot more efficient than doing a member- by- member assignment.

Can one do this with C++ classes and structures? What about the vtable? Is it stored in the class/ struct?

And, if there is extra "stuff" in the class/ struct that prohibits one from doing this, how does one send and receive data blocks over a TCP/ IP connection, e. g.,

send(sock, myStruct, sizeof(myStruct), 0);


Thanks in advance,
Allen

TheCPUWizard
December 18th, 2002, 02:55 PM
NO, No, No, No.

In case I did not make myself clear. NO!

Ok, well.....maybe....

C++ classes (and a struct is nothing more or less than a class with the default protection at the opening { being public rather than private) ofen have "things" buried in them.

You run a great risk if you do this. Items such as vTbls's can easily be corrupted. If you need the performance (and that means you have MEASURED the program and determined that there is a quantifiable significant gain), then implement a copy construtor that works as a bulk copy. This way if (when?) it does break, it will only be in one place in the code.

You DO send information over TCP/IP exactly as you posted (except you should consider using a C++ cast rather than a C cast).

anw
December 18th, 2002, 03:00 PM
Thanks for the reply. I knew it wasn't good programming practice, but in C it always worked. Are you saying that the vtable, etc. could be stored in the data of the struct/ class?

Which would lead to a further question: if the structs & classes can contain vtables, how could you send them over TCP/ IP this way?

Thanks again for the reply,
Allen

TheCPUWizard
December 18th, 2002, 03:13 PM
I am not saying "could" I am saying "Will be" that is if they exist!

Same as when sending info across a boundary in "C", one must be careful.

Consider a struct which contains a pointer is sure to cause problems if you send that struct across a tcp/ip connection, and then try to use the pointer.

If you keep the struct to just primitive types (i.e. int, float, bool, double, char [and arrays of these]), you will be safe. Add anything else and you are begging for trouble.

If you want to "send objects" over a connection, look into DCOM or better yet. .NET

anw
December 18th, 2002, 03:16 PM
Gotcha. Thanks for the help.

Regards,
Allen

Paul McKenzie
December 18th, 2002, 04:25 PM
You can add to this whole mix classes derived from a virtual base class. You should never, never, never use them in any kind of 'C' copying, moving, or sorting (yes, qsort) function.

Regards,

Paul McKenzie

anw
December 18th, 2002, 04:34 PM
Ahhh! Good point. If you have a virtual base class, you automatically have a vtable.

Thanks,
Allen

stober
December 18th, 2002, 04:51 PM
I saw an example sort algorithm on msdn that used qsort to sort a CArray of class objects. I think someone on this forum posted a link to it a couple weeks ago.

I still use memcpy() to copy one struct to another in C++, but my structs do not contain any c++ stuff, like STL, class objects, etc. They are just plain C-style structures. Otherwise, I aggree with the other posters -- don't use memcpy() with class objects or structures that contain c++ objects.

TheCPUWizard
December 18th, 2002, 04:59 PM
I previously "PM"'d Paul regarding that non-standard, sorting alrgorythm. My opinion is people who want to abuse the C++ standard should either:

1) Program in a different language.

or

2) Go do it quietly and not complain when they have problems.

AnthonyMai
December 18th, 2002, 10:58 PM
For copying structures I tend to simply use the good old "=" instead of memcpy(), it's simpler and more readable, although the compiler usually translate it into a memcpy or an inlined memcpy().

Regardly the following comment:


I previously "PM"'d Paul regarding that non-standard, sorting alrgorythm. My opinion is people who want to abuse the C++ standard should either:

1) Program in a different language.
or
2) Go do it quietly and not complain when they have problems.

My response is the C++ standard is not a bible: You follow it and you are in heaven, you don't follow it and you are in h-e-l-l. Even if it is a bible, there are a variation of different bibles from different religions to choose from.

I view any programming language as just a tool to accomplish the task of letting a computing device to do things I designate it to do. I can program in any language or any mixture of language I want. What's wrong with it? If I can mix assembly into the C++ code, why can't I mix some C stuff into my C++ code?

Is C++ inheritantly better than other programming language? No. Is C++ inheritantly inferior than other languages? No. All things created are equal. Everything exists for a reason. The important thing is know when and where and in what environment to use each language, or the combination of languages.

Even the C++ itself is starting to split as part of the evolution. There is now this embedded C++, in comtrast to the standard C++. I am sure it won't take long before different flavors of C++ will emerge to fit the need of different specific environments.