<?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: What's wrong with C? in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595035#M725742</link>
    <description>Finally Pascal:&lt;BR /&gt;&lt;BR /&gt;What are you writing this function for anyhow?&lt;BR /&gt;&lt;BR /&gt;tmpnam() and tempnam() are ready made functions which already do this. If this was just a snippet to illustrate a problem with strings then ok but otherwise use the built-ins. Man tempnam for details. Note the comments about returning a static buffer if a NULL pointer is supplied which is really just like your problem.&lt;BR /&gt;&lt;BR /&gt;You should also look at the tmpfile function. It creates a file and them immediately unlinks it. There is no directory entry but you can do amnythink you like to the file until you close it. The file then disappears automatically.&lt;BR /&gt;Man tmpfile(3c) for details.&lt;BR /&gt;&lt;BR /&gt;Clay</description>
    <pubDate>Mon, 15 Oct 2001 16:26:52 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2001-10-15T16:26:52Z</dc:date>
    <item>
      <title>What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595030#M725737</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;I have a string function that is not working as I expect.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;char *makefname(fnum)&lt;BR /&gt;int fnum;&lt;BR /&gt;{&lt;BR /&gt;char fname[80];&lt;BR /&gt;int pid = 0;&lt;BR /&gt;char *fname_ptr;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;pid = getpid();&lt;BR /&gt;sprintf(fname,"/tmp/Tfile%d.%d.DAT",pid,fnum);&lt;BR /&gt;printf("fnum = %d fname = %s\n",fnum,fname);&lt;BR /&gt;fname_ptr = fname;&lt;BR /&gt;return(fname_ptr);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;char *fname1, *fname2, *fname3, *fname4;&lt;BR /&gt;fname1 = makefname(1);&lt;BR /&gt;fname2 = makefname(2);&lt;BR /&gt;fname3 = makefname(3);&lt;BR /&gt;fname4 = makefname(4);&lt;BR /&gt;printf("fname1 = %s\n",fname1);&lt;BR /&gt;printf("fname2 = %s\n",fname2);&lt;BR /&gt;printf("fname3 = %s\n",fname3);&lt;BR /&gt;printf("fname4 = %s\n",fname4);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;No matter what I do only the last value is displayed but everything is fine inside the makefname function. I feel like I am making a very simple mistake but I have been working on this for over two hours!!&lt;BR /&gt;&lt;BR /&gt;Thanks in advance.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Oct 2001 15:42:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595030#M725737</guid>
      <dc:creator>Greg White</dc:creator>
      <dc:date>2001-10-15T15:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595031#M725738</link>
      <description>Hi Pascal:&lt;BR /&gt;&lt;BR /&gt;The answer to your question is that it's doing just what you tell it. You actually have 2 serious problems and a few small ones. &lt;BR /&gt;Serious problem 1:) Your string function is returning a pointer to an auto declared variable. This code is actually working by accident. fname should be declared as a static char fname[80] - to preserve the data ourtside the function call. This will not fix your problem about only the last invocation counting but it will make your code SAFE.&lt;BR /&gt;&lt;BR /&gt;Serious Problem 2:) You are returning a POINTER; it points to the same area there fore only the last invocation counts. You need to either dynamically allocate the space each time or set up a round-robin buffer so that the buffer is only re-used after say 5 invocations when presumably you have copied the data to other variables.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Clay&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Oct 2001 15:50:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595031#M725738</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-10-15T15:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595032#M725739</link>
      <description>Get some memory - malloc&lt;BR /&gt;add the proper library &lt;STDLIB.H&gt;&lt;BR /&gt;copy fname to memory loc&lt;BR /&gt;&lt;BR /&gt;#&lt;BR /&gt;#include &lt;STDLIB.H&gt;&lt;BR /&gt;#&lt;BR /&gt;char *makefname(fnum)&lt;BR /&gt;int fnum;&lt;BR /&gt;{&lt;BR /&gt;char fname[80];&lt;BR /&gt;int pid = 0;&lt;BR /&gt;char *fname_ptr;&lt;BR /&gt;&lt;BR /&gt;#get some memory&lt;BR /&gt;fname_ptr = malloc(sizeof(fname));&lt;BR /&gt;pid = getpid();&lt;BR /&gt;sprintf(fname,"/tmp/Tfile%d.%d.DAT",pid,fnum);&lt;BR /&gt;printf("fnum = %d fname = %s\n",fnum,fname);&lt;BR /&gt;#save fname&lt;BR /&gt;strcpy(fname_ptr,fname);&lt;BR /&gt;return(fname_ptr);&lt;BR /&gt;}&lt;BR /&gt;&lt;/STDLIB.H&gt;&lt;/STDLIB.H&gt;</description>
      <pubDate>Mon, 15 Oct 2001 16:09:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595032#M725739</guid>
      <dc:creator>harry d brown jr</dc:creator>
      <dc:date>2001-10-15T16:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595033#M725740</link>
      <description>Okay Pascal:&lt;BR /&gt;&lt;BR /&gt;I've attached the round-robin version first; it will cycle through 8 calls before reusing the space. Minor quibble: Allocate temp files from /var/tmp'; /tmp should only be used by OS utils; /var/tmp fills up -&amp;gt; inconvenient; /tmp fills up -&amp;gt; BAD. &lt;BR /&gt;&lt;BR /&gt;Nore that you really don't need fname_ptr, the array address can be returned. Also, no need to call getpid more than once. Let a static varaiable store this to make your code more efficient. It is very importtant that both the character array and the current counter be declared as static.&lt;BR /&gt;&lt;BR /&gt;Clay</description>
      <pubDate>Mon, 15 Oct 2001 16:18:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595033#M725740</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-10-15T16:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595034#M725741</link>
      <description>Hi again:&lt;BR /&gt;&lt;BR /&gt;Here's the somewhat better solution:&lt;BR /&gt;&lt;BR /&gt;Malloc a chuck of memory each time but don't forget to free it when you are done.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Oct 2001 16:20:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595034#M725741</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-10-15T16:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595035#M725742</link>
      <description>Finally Pascal:&lt;BR /&gt;&lt;BR /&gt;What are you writing this function for anyhow?&lt;BR /&gt;&lt;BR /&gt;tmpnam() and tempnam() are ready made functions which already do this. If this was just a snippet to illustrate a problem with strings then ok but otherwise use the built-ins. Man tempnam for details. Note the comments about returning a static buffer if a NULL pointer is supplied which is really just like your problem.&lt;BR /&gt;&lt;BR /&gt;You should also look at the tmpfile function. It creates a file and them immediately unlinks it. There is no directory entry but you can do amnythink you like to the file until you close it. The file then disappears automatically.&lt;BR /&gt;Man tmpfile(3c) for details.&lt;BR /&gt;&lt;BR /&gt;Clay</description>
      <pubDate>Mon, 15 Oct 2001 16:26:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595035#M725742</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-10-15T16:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: What's wrong with C?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595036#M725743</link>
      <description>Thanks Clay and Harry. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Oct 2001 16:44:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/what-s-wrong-with-c/m-p/2595036#M725743</guid>
      <dc:creator>Greg White</dc:creator>
      <dc:date>2001-10-15T16:44:54Z</dc:date>
    </item>
  </channel>
</rss>

