1 msgSuggestion required for appropriate implementation
11 msgAn article I alluded to
3 msgStrange floating point problems on SH4 with gcc...
5 msgg++/gcc : Floating point issue
1 msgCreating a Windows dll in Linux?
2 msgoutput to a directory which doesn't exit yet
1 msgGCC 3.4.4 compilation error for arm-linux target
1 msgproblems with precompiled header - vector and a...
1 msgbuiltin bounds checking how-to
1 msghelp installing gcc 4.0.3
2 msghelp spawning/executing a command line utility
1 msggcc symbols
3 msgstatic variables, the stack, the heap, and speed
1 msgg++ -- can unused constructors affect generated...

sending structure containing a pointer to anoth...
\ Abid Ghufran (23 Jul 2006)
. \ John \(Eljay\) Love-Jensen (23 Jul 2006)

4 msgARM Assembler
9 msgCygWin - GCC compiler error
1 msgGCC: Linking problem
1 msgFully qualified lib name.
2 msggcc -print-search-dirs
Subject:RE: sending structure containing a pointer to another structure over TCP socket
Group:Gcc-help
From:John \(Eljay\) Love-Jensen
Date:23 Jul 2006


Hi Abid,

Let's suppose you had this:

struct Second;

struct First
{
short value;
char* s;
Second* second;
};

struct Second
{
int a;
char b;
long c;
};

And let's assume that on your platform, char is 1 byte, short is 2 byte, int is 4 byte, and long is 8 byte. And let's assumet that pointers much be 4-byte aligned, and int must be 4-byte aligned, and long must be 8-byte aligned.

If you want to send First over the wire, and you want to include Second, what you should do is write a send routine, and a receive routine. I presume you use the hton and ntoh inside the low level data types Send and Recieve routines, which you'd have written.

void Send(int fd, First const& first)
{
SendInt16(fd, first.value);
SendCharArray(fd, size, first.s);
Send(fd, *(first.second));
};

void Send(int fd, Second const& second)
{
SendInt32(fd, second.a);
SendInt8(fd, second.b);
SendInt64(fd, second.c);
};

void Receive(int fd, First& first)
{
ReceiveInt32(fd, first.value);
ReceiveCharArray(fd, first.s);
first.second = new Second;
Receive(fd, *(first.second));
}

void Recieve(int fd, Second& second)
{
RecieveInt32(fd, second.a);
RecieveInt8(fd, second.b);
RecieveInt64(fd, second.c);
}

This is C++. You can use something similar for C (using pointers instead of references, and distinguished names). Don't forget to use ntoh (network Big Endian byte order to host byte order) and hton (host byte order to network Big Endian byte order) functions!

Alternatively, you can "flatten" your data structures into a textual representation, such as XML, and reconstitute it on the remote side. Some situations, textual format may be preferable over some canonical binary format.

Notice that we have avoided your pointer problem. We've also avoided sending along garbage intrastructure padding bytes and trailing structure alignment bytes, and we've avoided Big Endian / Little Endian problems, and we've avoided the issues caused by different platforms (OS and C/C++ compiler) having different sized fundamental data types.

Yet another alternative, is to work with IDL such as CORBA, or RPC, to use as a higher level language designed to address this exact problem domain. But that may be swatting a mosquito with a nuclear warhead, depending on your needs.

HTH,
--Eljay



© 2004-2008 readlist.com