<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: casting from character array to Structure. in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225227#M894922</link>
    <description>Your example program also crashes on PA-RISC with a +DD64 compiler mode.  I can prevent the crash by putting the buffer array in a union with an otherwise unused struct sbuf.  That causes the union, (and the array), to be aligned as necessary for the struct.&lt;BR /&gt;&lt;BR /&gt;main(argc, argv)&lt;BR /&gt;int argc;&lt;BR /&gt;char **argv;&lt;BR /&gt;{&lt;BR /&gt;int m;&lt;BR /&gt;struct x&lt;BR /&gt;{&lt;BR /&gt;long long l;&lt;BR /&gt;int i;&lt;BR /&gt;union {&lt;BR /&gt;char buffer[sizeof(struct sample)];&lt;BR /&gt;struct sbuf align;&lt;BR /&gt;} u;&lt;BR /&gt;int r;&lt;BR /&gt;}k;&lt;BR /&gt;char *ptr=k.u.buffer;&lt;BR /&gt;func((struct sbuf *)ptr);&lt;BR /&gt;}</description>
    <pubDate>Fri, 26 Mar 2004 14:26:57 GMT</pubDate>
    <dc:creator>Mike Stroyan</dc:creator>
    <dc:date>2004-03-26T14:26:57Z</dc:date>
    <item>
      <title>casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225224#M894919</link>
      <description>Hi, &lt;BR /&gt;&lt;BR /&gt;I have an instance where a character array is casted to structure, X. &lt;BR /&gt;ie char arr[50*sizeof(struct X)];&lt;BR /&gt;   char *l=arr;&lt;BR /&gt;   func((X *)l);&lt;BR /&gt;&lt;BR /&gt;{sizeof(struct X) is 1168 }&lt;BR /&gt;The structure X, has stat64 as one of its field. Since the most strict alignment in stat64 is 8bytes long, I guess the address of 'l' should also be aligned at 8 multiples. &lt;BR /&gt;I am getting SIGBUS &amp;amp; core dump in func(), when X-&amp;gt;stat field is assigned to stat structure value returned by stat64 call. &lt;BR /&gt;How can I ensure the character array address aligns at 8 multiples? &lt;BR /&gt;Or is the problem something else?&lt;BR /&gt;&lt;BR /&gt;In gdb, when I load core, I get address of 'l' only aligned at 4 whenever it dumps core.</description>
      <pubDate>Mon, 22 Mar 2004 05:40:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225224#M894919</guid>
      <dc:creator>Aditya Rekha</dc:creator>
      <dc:date>2004-03-22T05:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225225#M894920</link>
      <description>I must ask the obvious question.  Why don't you use the true data type to get the correct alignment?&lt;BR /&gt;&lt;BR /&gt;struct X arr[50];&lt;BR /&gt;func(arr);&lt;BR /&gt;</description>
      <pubDate>Tue, 23 Mar 2004 12:54:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225225#M894920</guid>
      <dc:creator>Mike Stroyan</dc:creator>
      <dc:date>2004-03-23T12:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225226#M894921</link>
      <description>Hi Mike, &lt;BR /&gt;&lt;BR /&gt;There seem to be two reasons why this is done. &lt;BR /&gt;&lt;BR /&gt;1. Our component sends this list of structures to pipe along with a 8byte pipe header. So here arr to arr+8 is a pipe header.&lt;BR /&gt;&lt;BR /&gt;2. The struct X, has following fields.&lt;BR /&gt;struct X&lt;BR /&gt;{&lt;BR /&gt;X *next;&lt;BR /&gt;short filename;&lt;BR /&gt;short actionname;&lt;BR /&gt;unsigned char physicaltype;&lt;BR /&gt;short logical type;&lt;BR /&gt;int errnum;&lt;BR /&gt;struct stat64 stat;&lt;BR /&gt;........&lt;BR /&gt;char text[1024];&lt;BR /&gt;}&lt;BR /&gt;Here the text field concatenates filename, actionname, logicaltype, link and filelink strings. In most of the practical cases, the total length of all these fields far less than 1024. So we move pointer to the nearest 4multiple of the length of the structure we used.  And the next structure starts from that point. This way we practically fit more than 50 such structures in the same space.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I missed mentioning one important point in my previous message. I face this problem only with IPF executable. This problem occurs whenever we try to assign any structure with some padded values to a casted type.A sample program which demonstrates the problem is &lt;BR /&gt;&lt;BR /&gt;#include&lt;STDIO.H&gt;&lt;BR /&gt;/* Here 4bytes in between st_dev and st_size are automatically padded, as offset of long long has to start at multiples of 8 */&lt;BR /&gt;typedef struct sample&lt;BR /&gt;{ int st_dev;&lt;BR /&gt;  long long  st_size;&lt;BR /&gt;  long st_ctime;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;typedef struct sbuf {&lt;BR /&gt; struct  sample s;&lt;BR /&gt;} ;&lt;BR /&gt;&lt;BR /&gt;main(argc, argv)&lt;BR /&gt;int argc;&lt;BR /&gt;char **argv;&lt;BR /&gt;{&lt;BR /&gt;int m;&lt;BR /&gt;struct x&lt;BR /&gt;{&lt;BR /&gt;   long long l;&lt;BR /&gt;   int i;&lt;BR /&gt;   char buffer[sizeof(struct sample)];&lt;BR /&gt;   int r;&lt;BR /&gt;}k;&lt;BR /&gt;char *ptr=k.buffer;&lt;BR /&gt;func((struct sbuf *)ptr);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;int func(struct sbuf *buffer)&lt;BR /&gt;{&lt;BR /&gt;struct sample st;&lt;BR /&gt;int result;&lt;BR /&gt;memset(buffer,0,sizeof(struct sample));&lt;BR /&gt;st.st_dev=12;&lt;BR /&gt;st.st_size=12342424;&lt;BR /&gt;st.st_ctime=234;&lt;BR /&gt;buffer-&amp;gt;s=st;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Similar is the case with stat64 structure.It is not tightly packed&amp;amp; has some padding in between. &lt;BR /&gt;&lt;BR /&gt;Why is this problem only with IA binary? &lt;BR /&gt;When we look at stat structure, it looks as if special care is taken to avoid automatic padding. But stat64 structure doesn't seem to have taken such care. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Fri, 26 Mar 2004 07:18:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225226#M894921</guid>
      <dc:creator>Aditya Rekha</dc:creator>
      <dc:date>2004-03-26T07:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225227#M894922</link>
      <description>Your example program also crashes on PA-RISC with a +DD64 compiler mode.  I can prevent the crash by putting the buffer array in a union with an otherwise unused struct sbuf.  That causes the union, (and the array), to be aligned as necessary for the struct.&lt;BR /&gt;&lt;BR /&gt;main(argc, argv)&lt;BR /&gt;int argc;&lt;BR /&gt;char **argv;&lt;BR /&gt;{&lt;BR /&gt;int m;&lt;BR /&gt;struct x&lt;BR /&gt;{&lt;BR /&gt;long long l;&lt;BR /&gt;int i;&lt;BR /&gt;union {&lt;BR /&gt;char buffer[sizeof(struct sample)];&lt;BR /&gt;struct sbuf align;&lt;BR /&gt;} u;&lt;BR /&gt;int r;&lt;BR /&gt;}k;&lt;BR /&gt;char *ptr=k.u.buffer;&lt;BR /&gt;func((struct sbuf *)ptr);&lt;BR /&gt;}</description>
      <pubDate>Fri, 26 Mar 2004 14:26:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225227#M894922</guid>
      <dc:creator>Mike Stroyan</dc:creator>
      <dc:date>2004-03-26T14:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225228#M894923</link>
      <description>Thank you. That's a nice solution for alignment. Leaving the sample program aside, in the actual source,the 1st eight bytes of character array are used for pipe header msg and the rest are the list of structures. So, a bit sceptical if this will work out. And implementing union requires change at all other places where the character array is referenced. Can we have other alternatives? &lt;BR /&gt;&lt;BR /&gt;There's one correction in the question I posted. The same program dumps core even when the address is aligned correctly at 8 multiples :-(  . Now I feel the problem is due to automatic padding in stat64 structure     rather than alignment of structure X.&lt;BR /&gt; &lt;BR /&gt;Instead of directly assigning stat64 structure, if I use memcpy the problem goes away. But is it a good practice to use memcpy as it doesn't check for alignment or does it? I am not sure. &lt;BR /&gt;Instead of &lt;BR /&gt; l-&amp;gt;stat = stat_buf;/* stat_buf is the stat64 structure obtained from stat64 call */&lt;BR /&gt; using&lt;BR /&gt;memcpy( &amp;amp;(l-&amp;gt;stat),&amp;amp;stat_buf,sizeof (struct stat64 )); is able to solve the problem.&lt;BR /&gt;&lt;BR /&gt;What's your opinion on using memcpy?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 28 Mar 2004 03:15:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225228#M894923</guid>
      <dc:creator>Aditya Rekha</dc:creator>
      <dc:date>2004-03-28T03:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225229#M894924</link>
      <description>memcpy() should be a safe operation as long as you declare a variable for the destination. That way, the source address can fall on a byte-boundary but the destintaion (since it is a declared variable) will always fall on a word boundary. This is basically the technique used by databases when floating point values might fall on odd boundaries within the database record. Memcpy (or something very similar) is used to move variables on a column by column basis.&lt;BR /&gt;</description>
      <pubDate>Sun, 28 Mar 2004 15:42:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225229#M894924</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2004-03-28T15:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: casting from character array to Structure.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225230#M894925</link>
      <description>memcpy doesn't have any alignment requirements.  (It does require the source and destination don't overlap.)&lt;BR /&gt;&lt;BR /&gt;  You should be fine using memcpy to pack the data if it will only be used for a write to a pipe.  You will need to be carefull of alignment requirements if you once again use that data as a struct instead of a byte array.  You will need to copy into an aligned address when unpacking data from reads of that pipe.&lt;BR /&gt;</description>
      <pubDate>Mon, 29 Mar 2004 10:59:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/casting-from-character-array-to-structure/m-p/3225230#M894925</guid>
      <dc:creator>Mike Stroyan</dc:creator>
      <dc:date>2004-03-29T10:59:41Z</dc:date>
    </item>
  </channel>
</rss>

