PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

problem creating dynamic array of structure inside a static structure

Page 1 of 1

6 Replies - 3764 Views - Last Post: 19 December 2010 - 12:13 PM Rate Topic: -----

#1 gaurav479   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-December 10

PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 18 December 2010 - 01:53 AM

hello,
I am developing a client server application using windows sockets.
I am trying to send a dynamic array of structures inside a static array. when i send my message over a socket(i had used sizeof operator and tried manually assigning the size too), i get full buffer on the server side , but i m not able to access the dynamic structures portion.i am getting only static structure members on the other side. when i debug the client side i see the following:
1. after loading the structure (static structure containing dynamic structures), i see only static structure members and only first dynamic structure member using debugger when i use a breakpoint just at time of sending structure over socket.i am having following questions:
1.using debugger will i be able to see all the dynamic array members.
2.how to correctly create a dynamic array inside a static array.
3.how to send and receive dynamic arrays over sockets.
4. is there any other way to load the dynamic structures other than new operator.
i am freeing the memory after use too.

Is This A Good Question/Topic? 0
  • +

Replies To: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2160
  • View blog
  • Posts: 4,225
  • Joined: 30-May 10

Re: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 18 December 2010 - 02:58 AM

You can only send "Plain Old Data" (POD) over a socket connection.
And even then, you're at the mercy of endianess

If you've got something like
struct foo {
  int a;
  double b;
  char *c;
};


Then you have all sorts of problems which need to be addressed (much more than doing send(&foo,sizeof foo))
1. As mentioned already, the endianess of multi-byte data. But these are typically solved by using htonl() and similar functions.
2. That sizeof(int) may be different between the two machines (sizeof anything may be different. The network deals with octets (8 bits), but even something as simple as a char may not be 8 bits on some machines).
3. The amount of padding and alignment between the struct members varies from one compiler (and machine) to another. Even if your struct contains only POD, the receiver may have different padding/alignment to the sender.
4. Whilst IEE754 is very common, it is not the only floating point format. Sending doubles is even harder than sending integers.
5. Sending pointers is strictly not allowed. You have to make the code to do send(foo.c,strlen(foo.c)).

If you have a complicated structure containing pointers to pointers (say a linked list), then your code has to arrange to traverse through your pointers until you've visited each POD element, and sent that directly to the receiver. Similarly, the receiver then builds up the data structure one element at a time.

The search topic for reading more on this is "serialisation".
Was This Post Helpful? 3
  • +
  • -

#3 gaurav479   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-December 10

Re: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 18 December 2010 - 09:40 AM

thanks for valuable reply,
i first want to solve the problem of loading correctly a dynamic structure inside a static structure and be able to see it at the time of sending. that is waana be doubly sure that i had correctly loaded it and also waana see all the structure members at time of sending.right now using the debugger i cant see dynamic structure members also i feel when i set the structure members using a for loop like
for(i=0;i<=j;i++)
{
id[i].msgid.no-> j+1;
etc;
etc;
j++;
}

i m everytime loading in 1st structure only
but i had made i structures using new operator. the loop is setting the structure members as i m printing the id[i].msgid after setting it everytime. i m not able to see structure members using the debugger and that is not giving me confidence.i m using float and uint type of data structures.i dont think i should worry about padding at moment as client and server are intel machines using bigendian.i m studying serialization as suggested by you. thanks. kindly give a hint to so that i can see what i had set.

This post has been edited by JackOfAllTrades: 18 December 2010 - 09:48 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 18 December 2010 - 09:48 AM

You are probably going to need to show some more code here. Post your code IN CODE TAGS, as shown below:

:code:
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2160
  • View blog
  • Posts: 4,225
  • Joined: 30-May 10

Re: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 18 December 2010 - 10:40 AM

Pasting your actual structure would help.
Was This Post Helpful? 0
  • +
  • -

#6 gaurav479   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-December 10

Re: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 19 December 2010 - 01:01 AM

greetings,

thanks for your valuable comments.
my structure defn is like this:
struct SectorDataSectorLoad
{
__int16 SectorNo;
__int16 InitAzimuth;// and more
};
struct SectorLoad
{
__int16 MsgId;
__int16 Msglength;/// and more
struct Packet
{
SectorDataSectorLoad *psectorload;
}p;
}


i am creating dynamic structures somewhat like this
noofsec = numberofsector.GetCurSel();
osectorload.NoofSector = noofsec +1;
osectroload.p.psectorload = new SectorDataSectorLoad[noofsec +1];



i load the structures like this
if(counter<=noofsec)
{
osectorload.p.psectorload[counter].SectorNo = counter +1;

and similarly more

counter ++;
}



// i send my structure something like this
int size = (6 +((noofsec+1)*10));
if (m_pClientSock->Send((const char*)&osectorload,size,0)==SOCKET_ERROR)
{
wsprintf(m_szError,"failed to send";%d",m_pCleintSock->GetlastError());
}
// here when i use the deugger at the highlighted structure after loading i only see parent structure and only first dynamic structure and not all . 
can u clarify why i m not able to see what i am sending. i m using necessary codes for checking which i m delirately not writing down to avoid more confusion. thanks


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 19 December 2010 - 05:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2160
  • View blog
  • Posts: 4,225
  • Joined: 30-May 10

Re: PROBLEM: DYNAMIC ARRAY OF STRUCTURE INSIDE A STATIC STRUCTURE

Posted 19 December 2010 - 12:13 PM

> int size = (6 +((noofsec+1)*10));
This would only work if all the data were in a single large block of memory.

But it isn't, it contains pointers to other bits of memory.

For simplicity, assume that sender and receiver have the same memory organisation (endian, word size, alignment etc).

Then you would need to do something like
send( &osectorload.NoofSector, sizeof(sectorload.NoofSector) );
send( osectroload.p.psectorload, sizeof(SectorDataSectorLoad)*(sectorload.NoofSector) );


The receiver would then do
recv( &osectorload.NoofSector, sizeof(sectorload.NoofSector) );
osectroload.p.psectorload = new SectorDataSectorLoad[osectorload.NoofSector];
recv( osectroload.p.psectorload, sizeof(SectorDataSectorLoad)*(sectorload.NoofSector) );

Notice how the receiver is told how many sectorData elements there are, so it can allocate the right space before the data arrives.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1