Re: packing a C structure



On 10/26/07, haairam@xxxxxxxxx <haairam@xxxxxxxxx> wrote:
hi,,

iam new to perl..i would like to pack a C structure to send it via
socket....

this is my C structure

typedef struct buffer_t {
uint32_t a;
char b[16];
uint32_t c;
uint32_t d;
char e[6];
char f[8];
} buffer_t;


can u provide a guideline for the format in the Pack command that i
need to use...
snip

Short answer:

You cant do this, find a better way to send the data (I would suggest
XML, YAML, or JSON).

Long answer:

You may be able to do this, but only for a given C compiler and OS
combination. ANSI C does not specify how structures are laid out in
memory, only how the language access it. Some architectures use all
sorts of padding to make accessing elements faster (this is why you
have to say sizeof(struct foo) to get the size of the structure
instead of just calculating it from the elements). You are also not
guaranteed what order the elements will be in. The elements
themselves may not even be of a given size since char, int, and long
only have minimum size definitions. Yet another hurdle is big endian
vs little endian memory layout. So, the upshot is you need to find
out exactly how the structure is laid out in memory. After you know
that, you can choose the proper pack string, but be prepared for it to
change. It is better to send the data in a platform neutral manner
and reconstitute it on the other end. XML is popular for some reason,
but there are many different data transport formats.
.



Relevant Pages

  • Re: Segmentation fault
    ... Here you ask for a pointer to char. ... to a random position in memory. ... There's nothing else than a string the user could enter;-) ... to the use of scanf(). ...
    (comp.lang.c)
  • Re: Memory Access Error
    ... First, scanfexpects a char pointer ... of 'inputstring') you still would have the problem that 'inputstring' ... you a char pointer but not also some memory it would be pointing to. ...
    (comp.os.linux.development.apps)
  • Re: Memory leak
    ... that worries me is the potential for memory overwrites. ... Is it possible XML string is larger than m_XMLMessage buffer will ... > //Returns tag value for a given tag name from XML event ... > char* GetTagValueFromXMLmessage ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Passing Pointers
    ... And now you know why you didn't want to allocate that 100 char array above: ... Note the memory you're deleting was allocated within Input. ... so name now points to a dynamically allocated array of chars. ... >void Output ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Passing Pointers
    ... And now you know why you didn't want to allocate that 100 char array above: ... Note the memory you're deleting was allocated within Input. ... so name now points to a dynamically allocated array of chars. ... >void Output ...
    (comp.lang.cpp)