Hi, I have a function written in C that performs a base64 conversion of a string and returns a pointer. The C function allocates memory to the pointer using calloc(). The calling RPG program grabs the encoded value using %str() and then deallocates the pointer. The program works 90% of the time but occasionally it throws a MCH0601 (space offset outside current limit) error during the call to the C function. The error seems to occur only when the program is called multiple times within a long-running job. Ending and restarting the job fixes the problem, so perhaps the problem only exists within an activation group. Any suggestions as to the cause will be appreciated.
barbara_morris 120000DX5W490 Posts
Re: Returning pointer from C function to RPG2014-08-28T18:24:01ZThis is the accepted answer. This is the accepted answer.
If the returned string is not null-terminated, over time the heap storage might tend to contain non x'00' values. If you don't have the second parameter for %STR that tells it how far to look for the null-terminator, it will keep looking for the null-temrinator, and could eventually run into the physical end of the storage.
If my guess is right, it would be better to get the C function to either null-terminate the vlaue, or to also return the length so you can use that as the second parameter for %STR.