473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dynamic cast for streamed data

struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.

Dec 21 '05 #1
13 1911
Ninan wrote:
struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.


Why do you want to figure things? Send some prefix that uniquely identifies
the type of data being sent.

--
Salu2
Dec 21 '05 #2
Ninan wrote:
struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.


No. dynamic_cast uses RTTI to figure out what the type is. That
information is not transmitted across the network.

You shouldn't be using memcpy with classes with virtual functions
anyway. You'll likely run into problems. Prefer OO serialization
techniques:

http://www.parashift.com/c++-faq-lit...alization.html

Also check out the Boost serialization library:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M

Dec 21 '05 #3
That is one possibility and I definitly thought about it. The problem
with that approach is that it is more difficult to do in the system
that I am working with

Dec 21 '05 #4
Ninan wrote:
That is one possibility and I definitly thought about it. The problem
with that approach is that it is more difficult to do in the system
that I am working with


I doubt that something impossible will be easier that something possible.

--
Salu2
Dec 21 '05 #5

|| Prefer OO serialization techniques:

One of the things I dont quite understand at the 'lower level' is the
distinction between a serialized and an un-serialized object.
For serialization - my understanding is that it involves taking an
object and encoding it into an architecture independent form. Ex: a
byte stream. If I opt not to serialize the object, isn't it safe to
state that the un-serialized object will the transmitted across 'a
wire' _also_ in the form of a byte stream? In other words,
transmittial across the wire is in the form of a byte stream
independent of wether the object is serialized or un-serialized. That
said, I suspect the distinction now lies in the 'encoding'?

Perhaps I need to look at a pictorial, highlighting the bit patterns of
a raw/un-serialized object and a serialized object to garner a much
deeper appreaciation for serialization.

I was perusing example source that was used for serializing and
transmitting data across ethenet. Serialization of the data sure
solved the endian issues (ie no need to deal with all the endian
convesion mess) between the two platforms. What I'm not understanding
is how is the serialized object different from the un-serialized object
at the machine level.

Dec 22 '05 #6

ma740988 wrote:
|| Prefer OO serialization techniques:

One of the things I dont quite understand at the 'lower level' is the
distinction between a serialized and an un-serialized object.
For serialization - my understanding is that it involves taking an
object and encoding it into an architecture independent form. Ex: a
byte stream. If I opt not to serialize the object, isn't it safe to
state that the un-serialized object will the transmitted across 'a
wire' _also_ in the form of a byte stream? In other words,
transmittial across the wire is in the form of a byte stream
independent of wether the object is serialized or un-serialized.
How do you propose to transmit and reconstruct an object portably
without some serialization technique? Object layout is implementation
dependent (especially when inheritance and virtuality are involved),
and so, in general, representing a non-serialized object as a byte
stream is not a well-defined process. The FAQs on serialization define
serialization thusly:

"It lets you take an object or group of objects, put them on a disk or
send them through a wire or wireless transport mechanism, then later,
perhaps on another computer, reverse the process: resurrect the
original object(s). The basic mechanisms are to flatten object(s) into
a one-dimensional stream of bits, and to turn that stream of bits back
into the original object(s).

"Like the Transporter on Star Trek, it's all about taking something
complicated and turning it into a flat sequence of 1s and 0s, then
taking that sequence of 1s and 0s (possibly at another place, possibly
at another time) and reconstructing the original complicated
'something.'"

[snip] What I'm not understanding
is how is the serialized object different from the un-serialized object
at the machine level.


The serialized object has a known layout (one defined by the
programmer). The non-serialized object has, in general, an
implementation-defined layout (one defined by the compiler).

Cheers! --M

Dec 22 '05 #7

Ninan wrote:
struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.


No. Treating the bytes on the other side may cause an immediate crash,
HD format etcetera. This isn't unlikely - polymorphic classes often
contain
function pointers, function pointers may differ between instances
(different
load addresses) and a simple implementation of a dynamic_cast relies
on a hidden virtual function (e.g. __get_rtti_data() ). Now, what is
A::__get_rtti_data may be OS::FormatHD on another.

HTH,
Michiel Salters

Dec 22 '05 #8

M, appreaciate the response.

|| The non-serialized object has, in general, an
implementation-defined layout

even for a struct with POD members? ex:
struct foo { int idx; char jdx; unsigned char* ptr_x; float f; };

Dec 22 '05 #9
ma740988 wrote:
M, appreaciate the response.

|| The non-serialized object has, in general, an
implementation-defined layout

even for a struct with POD members? ex:
struct foo { int idx; char jdx; unsigned char* ptr_x; float f; };


No, POD-only C-style structs might work, but that's why I said "in
general." Note, however, that your pointer's pointee would not likely
be properly shipped across the wire with a memcpy-type approach.

Cheers! --M

Dec 22 '05 #10


ma740988 wrote On 12/22/05 10:58,:
M, appreaciate the response.

|| The non-serialized object has, in general, an
implementation-defined layout

even for a struct with POD members? ex:
struct foo { int idx; char jdx; unsigned char* ptr_x; float f; };


Yes. The amount of padding (if any) between struct
elements is implementation-defined. Many machines will
put a gap between jdx and ptr_x; some will pad with one
byte and some with three. Some will add four bytes of
padding after f. In principle, an implementation can
use arbitrary amounts of padding after any struct element;
only market forces discourage whimsical abuses.

--
Er*********@sun.com

Dec 22 '05 #11
In article <do**********@news1brm.Central.Sun.COM>,
Eric Sosman <er*********@sun.com> wrote:
ma740988 wrote On 12/22/05 10:58,:
M, appreaciate the response.

|| The non-serialized object has, in general, an
implementation-defined layout

even for a struct with POD members? ex:
struct foo { int idx; char jdx; unsigned char* ptr_x; float f; };


Yes. The amount of padding (if any) between struct
elements is implementation-defined. Many machines will
put a gap between jdx and ptr_x; some will pad with one
byte and some with three. Some will add four bytes of
padding after f. In principle, an implementation can
use arbitrary amounts of padding after any struct element;
only market forces discourage whimsical abuses.


Another problem is byte order for any data type where sizeof > 1, e.g.
int and float.

And if the struct contains pointers, you'll just transmit the address,
not the object that it points to. That address will be useless to the
receiver.

--
Barry Margolin, ba****@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Dec 22 '05 #12

|| No, POD-only C-style structs might work, but that's why I said "in
general."
Ok!! got it.

Dec 23 '05 #13

"ma740988" <ma******@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I was perusing example source that was used for serializing and
transmitting data across ethenet. Serialization of the data sure
solved the endian issues (ie no need to deal with all the endian
convesion mess) between the two platforms.
If you have a network connection that sends a stream of bytes from one
place to another, you need to encode whatever you want to send as a stream
of bytes. You then write code on the sending side to convert the
information, however it is stored internally, into that particular stream of
bytes. Then you write code on the receiving side to convert that stream of
bytes into data, however it is stored internally.
What I'm not understanding
is how is the serialized object different from the un-serialized object
at the machine level.


Suppose you are thinking about two apples. Somehow, that is stored
inside your head in a native format that makes sense to you. In that form,
it may not make any sense to anyone else. To communicate it to me, you
serialize it. You convert it into a sequence of sounds that can be
communicated to another person. They deserialize it when they receive it,
reconstructing the notion of "two apples" inside their own head.

You start with the available for of communication. In the case of the
two people in my example, it's mouth, air, ears. What can that channel
communicate? Only a sequence of sounds. So we need rules to encode concepts
like "two apples" into sequences of sounds and vice versa.

Same thing in your case. You have a channel, and it can transmit
sequences of bytes. So you write code to convert whatever you want to
communicate into a precisely defined sequence of bytes, and on the other
side, from bytes.

DS
Dec 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
1
by: Tim Pascoe | last post by:
I am using the Dynamic Cross-Tab code supplied in an article from SQL Server Magazine (http://www.winnetmag.com/SQLServer/Article/ArticleID/15608/15608.html). I modified the script to generate a...
0
by: Chris Hoare | last post by:
I am writing a prototype data collection application; teh data for this project is being streamed in from a TCP port. Example of the XM...
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
6
by: Bill Bob | last post by:
I have an accounting database which contains data from various years. The frontend is a VB.Net program. At the year end, the program creates new voucher and transaction tables and creates new...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
1
by: Pacific Fox | last post by:
Hi all, I have a SQL statement that allows paging and dynamic sorting of the columns, but what I can't figure out without making the SQL a dynamic string and executing it, or duplicating the SQL...
6
by: Fred Mangusta | last post by:
Hi, I have an object, Model, that creates another object, Trainer. Trainer, given some data, fills some structures with processed data. My problem is the following: I need to pass those...
3
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
Note: My apologies for repeating this post from last week, but my nospam alias and profile account were incorrect. I think I have fixed this, so hopefully this post will trigger MS into a response...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.