C++: No output for Char* when on AIX. Worked on Linux


 
Thread Tools Search this Thread
Top Forums Programming C++: No output for Char* when on AIX. Worked on Linux
# 1  
Old 12-06-2010
C++: No output for Char* when on AIX. Worked on Linux

Hi All,

I have a script (attached) that was working fine on Linux. I compiled it there using
Code:
g++ CrncyFmt.cpp -o CrncyFmt.o

When I ran it there using eg.
Code:
CrncyFmt.o 2343.565 2

I get as expected:
Code:
CharOut  = "               2,343.57"

Now we have moved to our test box which is AIX and I have compiled it using
Code:
/usr/vacpp/bin/xlC_r -q64 CrncyFmt.cpp

(The command needs to be used in this format to work with Datastage which will be calling it)

But when I try to run it using eg
Code:
./a.out 2343.565 2

it returns only
Code:
CharOut  = ""

Can anyone please help me fix the code so that it works on AIX?

Many thanks!

Lee
PS AIX Version:
Code:
Fileset                                 Actual Level        Maintenance Level
-----------------------------------------------------------------------------
bos.rte                                 6.1.4.0             6.1.0.0

# 2  
Old 12-06-2010
I have no idea how you ever compiled this on linux:

Code:
crnc.cpp:64: error: 'atoi' was not declared in this scope

You need to #include <stdlib.h>

With luck this will fix the AIX error too. Undeclared external functions can break very badly on 64-bit platforms.
# 3  
Old 12-06-2010
Hi Corona,

Thanks for the quick response. It does actually compile on both Linux and AIX ... with or without the #include <stdlib.h>

I added the stdlib.h line but it still only outputs
Code:
CharOut  = ""

Lee

---------- Post updated at 03:56 PM ---------- Previous update was at 03:45 PM ----------

By the way, I should have noted also that placing
Code:
cout << "\nCharOut1  = \"" << CharOut << "\"\n\n"; //test print output

on line 239 before the final return DOES successfully give the right output. So something appears to be going wrong in the way the CrncyFmt function is being called by the main function or the way the CharOut is returned.

Lee
# 4  
Old 12-06-2010
Quote:
Originally Posted by Leedor
Hi Corona,

Thanks for the quick response. It does actually compile on both Linux and AIX ... with or without the #include <stdlib.h>
Not here it doesn't. In C++, undeclared externals are a flat-out error. At the very least it should be a warning, which you the programmer should treat as an error because using undeclared externals indeed mess up very badly on many platforms.
Quote:
So something appears to be going wrong in the way the CrncyFmt function is being called by the main function or the way the CharOut is returned.

Lee
Ahah... When your function returns, the object is no longer in memory, so the memory the string was stored in is no longer allocated and may be overwritten with garbage at any time. It's a sneaky form of returning a pointer to a local variable, disguised enough that the compiler can't warn you about it.

You should pass a buffer into the function to copy the string into instead.

Code:
void function(char *outstr, const char *instr1, const char *instr2)
{
        strcpy(outstr, "hello world");
}

int main(void)
{
        char buf[512];

        function(buf, str1, str2);

        printf("Output was %s\n", buf);
}

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-06-2010
Hi Corona,

Thanks again for both the note on undeclared externals (still not sure why I get no warning or error) and the solution.

Unfortunately I need my CrncyFmt function to return a Char*, not be void as it will eventually be run standalone.

Alternatively if it makes sense to make CrncyFmt a wrapper function with the original signature it had and put the majority of the code into a void function that would be fine too. Will that work?

Lee

---------- Post updated at 04:20 PM ---------- Previous update was at 04:15 PM ----------

Ok, so seeing your code, I decide to try the following as the final lines:
Code:
 strcpy(CharIn, CharOut);
        return CharIn;

Which works. Is it going to cause any issues to overwrite the input value like that?
# 6  
Old 12-06-2010
Yes. You're overwriting not just your input but all kinds of data beyond it you shouldn't be, corrupting main()'s argument array.

If you really, really need to return a char * (why can't you pass a buffer into a standalone? everything else does, that's how it's supposed to work) you can return a pointer to a static local variable.

Code:
char *function(const char *wtf)
{
        char buf[512];
        strcpy(buf, wtf);
        return(buf);
}

...but this is discouraged because it will always return the same buffer.

Code:
char *a=function("abc");
char *b=function("def");

// both A and B now point to "def"

# 7  
Old 12-06-2010
Ok, I'll take your word for that, it sounds bad.

The reason I need to return a char, not void, is because it's run from within Datastage, which expects functions to have a return value. So for example a derivation for an output will be CrncyFmt("123.456","2"). There is no option to specify void functions.

I tried changing the signature to
Code:
char *CrncyFmt (const char* CharIn, const char* CharDecPrecNum)

but then compilation failed:
Code:
"CrncyFmt.cpp", line 63.24: 1540-0258 (S) A return value of type "char *" cannot be initialized with an expression of type "const char *".

Adding Const to the function return type and in the main function worked but then still returned an empty string from main. The environment is not fully functional yet so I can't test this from within Datastage, perhaps it would work.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculating Total Hours worked

Write a script using a Linux shell programming language to perform clock management for a small daycare. The program should manage all time in and out routines. At the end of the each day should give the Total hours worked that day. Example: Time-In 6:30am Lunch-Out 11 :25am... (1 Reply)
Discussion started by: sarapham409
1 Replies

2. UNIX for Beginners Questions & Answers

Sleep command did not worked

Hi All, We have a process which is running for last 2 years well and good in production. But suddenly yesterday there was issue we faced in the process. The actual process is what it does like below. 1. Receive the files in NAS directory(N/w attached storage). 2. Trigger the... (11 Replies)
Discussion started by: mad man
11 Replies

3. UNIX for Dummies Questions & Answers

Code not worked,as expected

Hi All, OS :- HPUX.. I have a following code, which I wrote to meet date and time condition. The below code refuse to work for some reason, I still don't have any idea, why ? the below code, should exit out, if the day happens to be sat and time greater the 04:00 pm. I am bit surprised, that... (2 Replies)
Discussion started by: alok.behria
2 Replies

4. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

5. Solaris

Anyone worked with ldmp2v?

Hi, has anyone worked with ldmp2v utility to convert a physical machine to a virtual. I am been trying to convert an ultra 45 (test machine) to an ldom on a T6320 blade server (with ldom manager 1.3) but have been unsuccessful. The firmware of the blade has been upgrade to the latest available.... (0 Replies)
Discussion started by: Mack1982
0 Replies

6. Shell Programming and Scripting

Different output in AIX and Linux OS

Hi All , Please help me in understanding why i am getting error for the below script in LINUX, but the same script will work perfectly in AIX unix. Script Name:try.ksh #!/bin/ksh echo "$0 \n" MYPWD=$(pwd $(dirname $0)) echo " $MYPWD \n" O/P in LINUX OS: ./try.ksh ./try.ksh:... (2 Replies)
Discussion started by: sudhir_barker
2 Replies

7. Shell Programming and Scripting

vi command -output garbage char in HP-UX

Hi all , I am new to HP-UX flavour of unix. i am issuing simple "vi" comand on the command prompt it is showing me some garbage character in command prompt itself ..unreadable format. I tried opening an existing file using the vi editor --and same thing ... (3 Replies)
Discussion started by: jambesh
3 Replies

8. Shell Programming and Scripting

test to see if useradd worked

I am trying to write a script that does a useradd -G <group> <user> but you cannot modify the user if he/she is logged on. How could I check to see if the user is on or if the user has been added to the group successfully? I plan on having the script sleep for a couple mins if the user is on then... (3 Replies)
Discussion started by: doublejz
3 Replies

9. UNIX for Dummies Questions & Answers

Variables being worked on inside of loops

I have a while read loop that reads values inside of a file and then performs an expr operation on each. Everything works fine, the way it's supposed to but, once the loop is finished, I can not access the variable that I used inside of the loop (but that variable was created outside of the... (7 Replies)
Discussion started by: yongho
7 Replies
Login or Register to Ask a Question