<?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: very simple code(5 lines only), why coredump? in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877051#M703845</link>
    <description>Hello:&lt;BR /&gt;&lt;BR /&gt;Try this:&lt;BR /&gt; cp tt.cpp tt.c&lt;BR /&gt; cc -g tt.c&lt;BR /&gt;&lt;BR /&gt; When I run ./a.out, I get&lt;BR /&gt;1234&lt;BR /&gt;&lt;BR /&gt;As a guess I'd say s1 is allocated 5 bytes on the stack, s2 is right behind it with 8 bytes.  I'd be really surprised if the compiler didn't align s2 properly, but you never know.  s2 may be byte aligned and an int is bigger, so the dereference (p[0]) may cause a bus error.  (s2 needs to be on a word boundary.)&lt;BR /&gt;&lt;BR /&gt;The command line option -o tt.cpp is telling aCC you want to create an object file, not an executable.  Have you tried aCC -AA -g tt.cpp and running ./a.out ?  &lt;BR /&gt;&lt;BR /&gt;I see nothing wrong with the strcpy command, but you should always check the return value.  Also, this is a C program, why are you using the C++ compiler?&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;   Dave&lt;BR /&gt;</description>
    <pubDate>Mon, 27 Dec 2004 17:57:22 GMT</pubDate>
    <dc:creator>David Johns</dc:creator>
    <dc:date>2004-12-27T17:57:22Z</dc:date>
    <item>
      <title>very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877050#M703844</link>
      <description>&lt;BR /&gt;it's ok under WINDOWS &amp;amp; LINUX, but coredump under HP-UNIX, why?&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;int main(int argc, char *argv[])&lt;BR /&gt;{&lt;BR /&gt; char s1[5],s2[8];&lt;BR /&gt; strcpy(s1,"WKF"); //if remove this line, no coredump&lt;BR /&gt; int *p=(int*)s2;&lt;BR /&gt; p[0]=1234; //this line cause coredump&lt;BR /&gt; printf("%d\n",p[0]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;HP-UX hpltest B.11.11 U 9000/800 (tc)&lt;BR /&gt;&lt;BR /&gt;link with "aCC -g -o t t.cpp". why?&lt;BR /&gt;and how to avoid coredump without modify my code? thanks very much.&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Mon, 27 Dec 2004 10:17:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877050#M703844</guid>
      <dc:creator>proad</dc:creator>
      <dc:date>2004-12-27T10:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877051#M703845</link>
      <description>Hello:&lt;BR /&gt;&lt;BR /&gt;Try this:&lt;BR /&gt; cp tt.cpp tt.c&lt;BR /&gt; cc -g tt.c&lt;BR /&gt;&lt;BR /&gt; When I run ./a.out, I get&lt;BR /&gt;1234&lt;BR /&gt;&lt;BR /&gt;As a guess I'd say s1 is allocated 5 bytes on the stack, s2 is right behind it with 8 bytes.  I'd be really surprised if the compiler didn't align s2 properly, but you never know.  s2 may be byte aligned and an int is bigger, so the dereference (p[0]) may cause a bus error.  (s2 needs to be on a word boundary.)&lt;BR /&gt;&lt;BR /&gt;The command line option -o tt.cpp is telling aCC you want to create an object file, not an executable.  Have you tried aCC -AA -g tt.cpp and running ./a.out ?  &lt;BR /&gt;&lt;BR /&gt;I see nothing wrong with the strcpy command, but you should always check the return value.  Also, this is a C program, why are you using the C++ compiler?&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;   Dave&lt;BR /&gt;</description>
      <pubDate>Mon, 27 Dec 2004 17:57:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877051#M703845</guid>
      <dc:creator>David Johns</dc:creator>
      <dc:date>2004-12-27T17:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877052#M703846</link>
      <description>proad,&lt;BR /&gt;&lt;BR /&gt;&amp;gt; #include &lt;STDIO.H&gt;&lt;BR /&gt;&amp;gt; #include &lt;STRING.H&gt;&lt;BR /&gt;&amp;gt; int main(int argc, char *argv[])&lt;BR /&gt;&amp;gt; {&lt;BR /&gt;&amp;gt; char s1[5],s2[8];&lt;BR /&gt;&amp;gt; strcpy(s1,"WKF"); //if remove this line, no coredump&lt;BR /&gt;&amp;gt; int *p=(int*)s2;&lt;BR /&gt;&amp;gt; p[0]=1234; //this line cause coredump&lt;BR /&gt;&amp;gt; printf("%d\n",p[0]);&lt;BR /&gt;&amp;gt; }&lt;BR /&gt;&lt;BR /&gt;It dumps core because of incorrect alignment.&lt;BR /&gt;If you change the size of char s1[5] to s1[8],&lt;BR /&gt;the core dump will go away.&lt;BR /&gt;&lt;BR /&gt;- Biswajit&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Mon, 27 Dec 2004 20:04:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877052#M703846</guid>
      <dc:creator>Biswajit Tripathy</dc:creator>
      <dc:date>2004-12-27T20:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877053#M703847</link>
      <description>proad,&lt;BR /&gt;&lt;BR /&gt;Couple of more comments.&lt;BR /&gt;&lt;BR /&gt;You are writing a C code with some C++&lt;BR /&gt;syntax  and compiling in a C++ compiler.&lt;BR /&gt;You probably know that this is not really a&lt;BR /&gt;good idea in real life.&lt;BR /&gt;&lt;BR /&gt;Another thing is using char s2[] to store &lt;BR /&gt;integer. That's not a good idea either. &lt;BR /&gt;&lt;BR /&gt;- Biswajit&lt;BR /&gt;</description>
      <pubDate>Mon, 27 Dec 2004 20:11:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877053#M703847</guid>
      <dc:creator>Biswajit Tripathy</dc:creator>
      <dc:date>2004-12-27T20:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877054#M703848</link>
      <description>first, thanks very much for your answers.&lt;BR /&gt;&lt;BR /&gt;I wrote it in C++ because this code is just a demo of one of my another complex C++ program, which meet the same coredump. so do not compile it with "cc".&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I store a number in a char[] is to store  number in a big continuous memory in binary,in "sizeof(int)" bytes, the continuous memory also store other data.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I have tried to change the s1[] and s2[] size, but still coredump!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;int main(int argc, char *argv[])&lt;BR /&gt;{&lt;BR /&gt;   char s1[77],s2[88];&lt;BR /&gt;   strcpy(s1,"WKF"); //if remove this line, no coredump&lt;BR /&gt;   int *p=(int*)s2;&lt;BR /&gt;   p[0]=1234; //this line cause coredump&lt;BR /&gt;   printf("%d\n",p[0]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;thanks.&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Mon, 27 Dec 2004 22:34:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877054#M703848</guid>
      <dc:creator>proad</dc:creator>
      <dc:date>2004-12-27T22:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877055#M703849</link>
      <description>proad,&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I have tried to change the s1[] and s2[] size, but still coredump!&lt;BR /&gt;&lt;BR /&gt;I don't think you understood my reply; so let me be&lt;BR /&gt;more explicit. Make sure that the size of the array&lt;BR /&gt;s1[] is 8 byte alligned (i.e divisible by 8). In your new&lt;BR /&gt;code, you have used the s1[] size as 77. If you &lt;BR /&gt;change it to 80, then you will not encounter the bus&lt;BR /&gt;error.&lt;BR /&gt;&lt;BR /&gt;- Biswajit&lt;BR /&gt;</description>
      <pubDate>Mon, 27 Dec 2004 23:43:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877055#M703849</guid>
      <dc:creator>Biswajit Tripathy</dc:creator>
      <dc:date>2004-12-27T23:43:17Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877056#M703850</link>
      <description>thanks very very much,&lt;BR /&gt;Biswajit Tripathy's reply is very helpful.&lt;BR /&gt;thanks. :)</description>
      <pubDate>Tue, 28 Dec 2004 01:53:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877056#M703850</guid>
      <dc:creator>proad</dc:creator>
      <dc:date>2004-12-28T01:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877057#M703851</link>
      <description>proad,&lt;BR /&gt;before you close the thread, I just want you&lt;BR /&gt;to understand the problem with this solution.&lt;BR /&gt;The solution is working solely because you are&lt;BR /&gt;assigning a suitable size to s1 to make sure that&lt;BR /&gt;address of s2 is 8 byte aligned. If someone&lt;BR /&gt;inserts another char array s3 between s1 and s2,&lt;BR /&gt;the program might again start to fail. To write a&lt;BR /&gt;program with better coding standard, you probably&lt;BR /&gt;need to use int arrays instead of char arrays or &lt;BR /&gt;check the compiler manual to see if the &lt;BR /&gt;c-preprocessor "pragma" directive can help you &lt;BR /&gt;align the data in a more predictable way.&lt;BR /&gt;&lt;BR /&gt;- Biswajit&lt;BR /&gt;</description>
      <pubDate>Tue, 28 Dec 2004 02:33:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877057#M703851</guid>
      <dc:creator>Biswajit Tripathy</dc:creator>
      <dc:date>2004-12-28T02:33:20Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877058#M703852</link>
      <description>strcpy is also insecure programming practise.&lt;BR /&gt;use strncpy instead to prevent possible security hacks of your code in the future.</description>
      <pubDate>Tue, 28 Dec 2004 02:33:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877058#M703852</guid>
      <dc:creator>dirk dierickx</dc:creator>
      <dc:date>2004-12-28T02:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877059#M703853</link>
      <description>Biswajit Tripathy:&lt;BR /&gt;  it's a good idea for me to use int arrays instead of char arrays in my program.&lt;BR /&gt;  on the other hand, how to use "pragma" directive to align the data? would you please give me a simple example code?&lt;BR /&gt;  thanks.</description>
      <pubDate>Tue, 28 Dec 2004 03:23:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877059#M703853</guid>
      <dc:creator>proad</dc:creator>
      <dc:date>2004-12-28T03:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877060#M703854</link>
      <description>proad,&lt;BR /&gt;sorry, it's been a long time since I used pragma in&lt;BR /&gt;a user level code, so don't remember the exact&lt;BR /&gt;directives; but the following URL might help you:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.hp.com/en/B3901-90015/ch03.html" target="_blank"&gt;http://docs.hp.com/en/B3901-90015/ch03.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;- Biswajit&lt;BR /&gt;</description>
      <pubDate>Tue, 28 Dec 2004 03:38:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877060#M703854</guid>
      <dc:creator>Biswajit Tripathy</dc:creator>
      <dc:date>2004-12-28T03:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877061#M703855</link>
      <description>thanks very much,&lt;BR /&gt;Biswajit Tripathy's reply is always very helpful.&lt;BR /&gt;&lt;BR /&gt;so I can close the thread now,&lt;BR /&gt;but maybe somebody else want to add comments,&lt;BR /&gt;so I'll close it later.</description>
      <pubDate>Tue, 28 Dec 2004 03:51:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877061#M703855</guid>
      <dc:creator>proad</dc:creator>
      <dc:date>2004-12-28T03:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: very simple code(5 lines only), why coredump?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877062#M703856</link>
      <description>thanks to everybody!:)</description>
      <pubDate>Wed, 05 Jan 2005 07:29:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/very-simple-code-5-lines-only-why-coredump/m-p/4877062#M703856</guid>
      <dc:creator>proad</dc:creator>
      <dc:date>2005-01-05T07:29:30Z</dc:date>
    </item>
  </channel>
</rss>

