Operating System - HP-UX
1756638 Members
2425 Online
108849 Solutions
New Discussion

Trying to port old code to new system

 
SOLVED
Go to solution
Carl Sacco
New Member

Trying to port old code to new system

I'm moving c code from one system to another.
Old system: 9000/800 T520 HPUX 11.0 using verB.11.01.06 HP C++ ansi C compiler.
New system: 9000/800 v2600 HPUX 11.0 using CANSI B.11.01.10 C/ANSI C bundle
The follow snipit is an exampole of where I am having trouble. It runs great on the old machine but not on the new. I have complied it in 32 and 64 bit with the same results. Any help is appreciated.

Thanks!!

char *srbitfmt();

main()
{
char workarea[7];

sprintf(workarea, "%6.6s", srbitfmt());
printf("test result <%s> \n", workarea);

}
char *srbitfmt()
{
char workarea[7];

(void) sprintf(workarea, "%c%c%c%c%c%c", '1', '1', '1', '1', '1', '1');
printf("test <%s>\n", workarea);

return(workarea);
}
1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Trying to port old code to new system

Hi Carl,

This one is easy though it's very easy to overlook. The correct answer is that your old version is working by accident.

Your *char function srbitfmt() is returning garbage because the workarea array is auto declared (and thus comes from the stack) rather than static. If you simply change the declaration within the function to
static char workarea[7];
all will be well. When you exit the function the area pointed to by the auto declared variable became undefined. Auto declaration within main is fine because you are still in scope.

Hope this makes things clear, Clay
If it ain't broke, I can fix that.