socket in C++

Q

QQ

Hi I never used C++ before, so if the question is too basic,
please forgive me.

I am trying to implement a socket program in C++.
I know in C program when I receive a packet with some structure,
I need to parse it so as to evaluate every elements of the structure.
for example
struct A
{
int a;
int b;
}

so when I get the packet I need to parse it
memcpy(a,msg,sizeof(int));
....

So I wonder whether I need to do it in C++
or I can just claim

class C *exC = (class C*) msg

Thanks a lot!
 
L

Larry I Smith

QQ said:
Hi I never used C++ before, so if the question is too basic,
please forgive me.

I am trying to implement a socket program in C++.
I know in C program when I receive a packet with some structure,
I need to parse it so as to evaluate every elements of the structure.
for example
struct A
{
int a;
int b;
}

so when I get the packet I need to parse it
memcpy(a,msg,sizeof(int));

Yes, but don't forget to also copy 'b'.
...

So I wonder whether I need to do it in C++
or I can just claim

class C *exC = (class C*) msg
NO


Thanks a lot!

Larry
 
V

Victor Bazarov

QQ said:
Hi I never used C++ before, so if the question is too basic,
please forgive me.

I am trying to implement a socket program in C++.
I know in C program when I receive a packet with some structure,
I need to parse it so as to evaluate every elements of the structure.
for example
struct A
{
int a;
int b;
}

so when I get the packet I need to parse it
memcpy(a,msg,sizeof(int));
...

So I wonder whether I need to do it in C++
or I can just claim

class C *exC = (class C*) msg

'class' is superfluous here. And where did C come from? You just used
'A' a few lines back.

Anyway, it depends entirely on the structure (pun is not intended) of
the packet itself. Are you sure the memory pointed to by 'msg' has
the same byte layout as an A object? If so, I'd recommend

A a;
memcpy(&a, msg, sizeof(A));

to start.

V
 
L

Larry I Smith

Victor said:
'class' is superfluous here. And where did C come from? You just used
'A' a few lines back.

Anyway, it depends entirely on the structure (pun is not intended) of
the packet itself. Are you sure the memory pointed to by 'msg' has
the same byte layout as an A object? If so, I'd recommend

A a;
memcpy(&a, msg, sizeof(A));

to start.

V

Shouldn't 'A' be declared as:

extern "C" struct A { int a; int b; };

to ensure that it is a plain 'C' style struct
with no vtable, etc? Or is that the default if
one does not declare any member methods?

Regards,
Larry
 
V

Victor Bazarov

Larry said:
Shouldn't 'A' be declared as:

extern "C" struct A { int a; int b; };

to ensure that it is a plain 'C' style struct
with no vtable, etc? Or is that the default if
one does not declare any member methods?

struct A in its original form *is* a POD and *can* be used as the
destination in 'memcpy', so there is no need to dance around with
"C" or 'extern'. Besides, you're not really achieving that effect
with 'extern "C"', it does not suddenly make a non-POD structure POD.

Of course, if later somebody adds a constructor or private section
or a virtual function to it, POD-ness of it will disappear, but in
that case the type should be simply well-documented.

V
 
A

Andre Kostur

struct A in its original form *is* a POD and *can* be used as the
destination in 'memcpy', so there is no need to dance around with
"C" or 'extern'. Besides, you're not really achieving that effect
with 'extern "C"', it does not suddenly make a non-POD structure POD.

Of course, if later somebody adds a constructor or private section
or a virtual function to it, POD-ness of it will disappear, but in
that case the type should be simply well-documented.

And descending into platform-specific stuff:

- As previously noted, you'll need to ensure that both sides are using
the same structure padding
- Also, if you're passing around floats/doubles, they aren't necessarily
represented the same way on each side
- Also, you may have to worry about byte-ordering issues. (Hint: a 2-
byte short isn't the same byte order on a sparc vs. an intel CPU....)
- We're only talking about PODs here... if it's not a POD, then it's not
safe to do this at all.....
 
J

Jesper Madsen

"The truth of the matter is that you always know the right thing to do. The
hard part is doing it"
- General H. Norman Schwartzoff
Larry I Smith said:
Shouldn't 'A' be declared as:

extern "C" struct A { int a; int b; };

to ensure that it is a plain 'C' style struct
with no vtable, etc? Or is that the default if
one does not declare any member methods?
extern "C" does not mean compile as C, it just means no name mangling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top