× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On 9/15/2016 10:20 AM, tim wrote:
Im having issues with my first attemp with pointers. I have a service
program that uses strtok to split info into array elements. When i try
using it, the subsequent invocations replace prior ones with the latest
data. It looks like its pointing to the same memory location.

here is my code. i think i need to do something in my service program so
that the pointer is unique for each call to the service.
...
D tokens s 1024A varying dim(2000)
...
return %addr(tokens);

Your "tokens" variable is in automatic storage. That means that it only available to your procedure while the procedure is running. As soon as your procedure returns, the storage might be used by something else.

A procedure should NEVER return a pointer to a variable in automatic storage. There are no exceptions to this rule.

Another problem with your code:
strtok() expects the string to be null-terminated. Your parameter is not null-terminated, and there is no guarantee that it will be followed in storage by x'00', so you might get extra tokens that have nothing to do with the string that was passed to your procedure. One way to handle this is to make string2 65536 long, and code
string2 = string1 + x'00';
or better
string2 = %trim(string1) + x'00';

If you don't want to use Alan's approach, I would change the procedure so the caller passes in the "tokens" array. And then I would just return the number of tokens.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.