[Info-vax] calloc fails with access violation

sapienzaf sapienza at noesys.com
Tue Aug 25 21:38:34 EDT 2009


On Aug 25, 4:23 pm, Jose Cuevas <jcue... at mac.com> wrote:
> That using char * as arguments in C when using "BY REFERENCE" in cobol
> does not make a difference at all. All strings returned the same size,
> and I was able to treat them a null-strings in the C side. That makes
> think that the runtime is doing the conversion for me.  I tested every
> standard library in C that I know depends on NULL and they work, I
> loop tru the pointer and it has a NULL.

Jose,

Until you are willing to accept that OpenVMS Cobol does not pass null-
terminated strings then I think there's very little we can do to help
you.  This is at least one fatal flaw in all your descriptions and
posted source code.

Here is a trivially simple proof.  Take the following sample programs
and build them on your system.  Note that the Cobol program explicity
defines a terminating zero (the ZEROBYTE data item) in order to
prevent access violations.

The main program is written in Cobol:

       identification division.
       program-id. cobmain.

       data division.
       working-storage section.
       01  top_structure.
           03  string_a    pic x(20) value spaces.
           03  string_b    pic x(20) value spaces.
           03  zerobyte    pic s9(4) comp value zero.

       procedure division.
       main-program section.
       do-it.
            call 'ccstub' using top_structure.
            call 'ccstub' using string_a.
            call 'ccstub' using string_b.

            stop run.

The subroutine is written in C:

#include <stdio>
#include <string>

int ccstub (char *str)
{
   printf ("Length=%d\n", strlen(str));

   return 1;
}

Build it using the following:

$ cob /ansi cobmain
$ cc ccstub
$ link cobmain, ccstub

Then run it and you will see the following results:

$ run cobmain
Length=40
Length=40
Length=20

Notice that when passing STRING_A (the second CALL), which is defined
as only 20 characters, the C subroutine reports its length as 40
characters.  This is because there is no zero termination of each
individual string data item.

Also notice that when passing the outer level structure
(TOP_STRUCTURE) there are no errors, nor does the C routine see it as
an array.

The run time library is not doing anything to the Cobol strings when
passed to a C subroutine.  Your experience in finding a zero byte at
the end of a single string data item is purely coincidental, and
cannot be guaranteed or relied upon in a production application.



More information about the Info-vax mailing list