[Info-vax] calloc fails with access violation

Jose Cuevas jcuevas at mac.com
Wed Aug 26 13:03:14 EDT 2009


My last post was on a bit of hurry.

I do see what sapienz and others are telling me about Cobols Strings.
When I look at the sample code posted by Sapienz I see why I was not
seen a null problem. Since all my tests where using "01" variables in
Cobol, my tests did not reflected any differences. I added some things
to Sapienz's code to illustrate my early observations.(not an
argument, more of a personal learning process...)

$ type t1.cob
identification division.
       program-id. cobmain.

data division.
working-storage section.
01  top_structure.
        03  string_a    pic x(6) value all 'A'.
        03  string_b    pic x(6) value all 'B'.
        03  string_c    pic x(6) value all 'C'.
01 simple_var pic x(6) value all 'D'.

procedure division.
main section.
doit.
        call 'ccstub' using top_structure.
        call 'ccstubB' using by descriptor top_structure.
        call 'ccstub' using string_a.
        call 'ccstubB' using by descriptor string_a.
        call 'ccstub' using string_b.
        call 'ccstubB' using by descriptor string_b.

        call 'ccstub' using simple_var.
        call 'ccstubB' using by descriptor simple_var.

            stop run.


$ type t0.c
#include <stdio>
#include <string>
#include <stdlib>
#include <descrip.h>
int ccstub (char *str){
        printf ("Length=%d\n", strlen(str));
        printf("str=[%s]\n",str);
        printf("pointer=[%p]\n", str);
        int i = 0;
        for(i=strlen(str);i>=0; i--){
                if(str[i] == '\0'){
                        /*should never happen*/
                        printf("ch[%i]=[NULL][NULL]\n", i);
                }else{
                        printf("ch[%i]=[%i][%c]\n", i,str[i],str[i]);
                }
        }

        return 1;
}

int ccstubB (struct dsc$descriptor *str){
        printf("By Descriptor------------------\n");
        int sz = str->dsc$w_length;
        char *sc = calloc(sz + 1, sizeof(char));
        if(sc == NULL) return 0;
        memcpy(sc, str->dsc$a_pointer, sz);
        sc[sz] = '\0';

        printf ("Length=%d\n", sz);
        printf("str=[%s]\n",sc);
        printf("pointer=[%p]\n", str->dsc$a_pointer);

        int i = 0;
        for(i=strlen(sc);i>=0; i--){
                if(sc[i] == '\0'){
                        /*should never happen*/
                        printf("ch[%i]=[NULL][NULL]\n", i);
                }else{
                        printf("ch[%i]=[%i][%c]\n", i,sc[i],sc[i]);
                }
        }

        return 1;
}


The output:

$ run t1
Length=18
str=[AAAAAABBBBBBCCCCCC]
pointer=[20010]
ch[18]=[NULL][NULL]
ch[17]=[67][C]
ch[16]=[67][C]
ch[15]=[67][C]
ch[14]=[67][C]
ch[13]=[67][C]
ch[12]=[67][C]
ch[11]=[66][B]
ch[10]=[66][B]
ch[9]=[66][B]
ch[8]=[66][B]
ch[7]=[66][B]
ch[6]=[66][B]
ch[5]=[65][A]
ch[4]=[65][A]
ch[3]=[65][A]
ch[2]=[65][A]
ch[1]=[65][A]
ch[0]=[65][A]
By Descriptor------------------
Length=18
str=[AAAAAABBBBBBCCCCCC]
pointer=[20010]
ch[18]=[NULL][NULL]
ch[17]=[67][C]
ch[16]=[67][C]
ch[15]=[67][C]
ch[14]=[67][C]
ch[13]=[67][C]
ch[12]=[67][C]
ch[11]=[66][B]
ch[10]=[66][B]
ch[9]=[66][B]
ch[8]=[66][B]
ch[7]=[66][B]
ch[6]=[66][B]
ch[5]=[65][A]
ch[4]=[65][A]
ch[3]=[65][A]
ch[2]=[65][A]
ch[1]=[65][A]
ch[0]=[65][A]
Length=18
str=[AAAAAABBBBBBCCCCCC]
pointer=[20010]
ch[18]=[NULL][NULL]
ch[17]=[67][C]
ch[16]=[67][C]
ch[15]=[67][C]
ch[14]=[67][C]
ch[13]=[67][C]
ch[12]=[67][C]
ch[11]=[66][B]
ch[10]=[66][B]
ch[9]=[66][B]
ch[8]=[66][B]
ch[7]=[66][B]
ch[6]=[66][B]
ch[5]=[65][A]
ch[4]=[65][A]
ch[3]=[65][A]
ch[2]=[65][A]
ch[1]=[65][A]
ch[0]=[65][A]
By Descriptor------------------
Length=6
str=[AAAAAA]
pointer=[20010]
ch[6]=[NULL][NULL]
ch[5]=[65][A]
ch[4]=[65][A]
ch[3]=[65][A]
ch[2]=[65][A]
ch[1]=[65][A]
ch[0]=[65][A]
Length=12
str=[BBBBBBCCCCCC]
pointer=[20016]
ch[12]=[NULL][NULL]
ch[11]=[67][C]
ch[10]=[67][C]
ch[9]=[67][C]
ch[8]=[67][C]
ch[7]=[67][C]
ch[6]=[67][C]
ch[5]=[66][B]
ch[4]=[66][B]
ch[3]=[66][B]
ch[2]=[66][B]
ch[1]=[66][B]
ch[0]=[66][B]
By Descriptor------------------
Length=6
str=[BBBBBB]
pointer=[20016]
ch[6]=[NULL][NULL]
ch[5]=[66][B]
ch[4]=[66][B]
ch[3]=[66][B]
ch[2]=[66][B]
ch[1]=[66][B]
ch[0]=[66][B]
Length=6
str=[DDDDDD]
pointer=[20028]
ch[6]=[NULL][NULL]
ch[5]=[68][D]
ch[4]=[68][D]
ch[3]=[68][D]
ch[2]=[68][D]
ch[1]=[68][D]
ch[0]=[68][D]
By Descriptor------------------
Length=6
str=[DDDDDD]
pointer=[20028]
ch[6]=[NULL][NULL]
ch[5]=[68][D]
ch[4]=[68][D]
ch[3]=[68][D]
ch[2]=[68][D]
ch[1]=[68][D]
ch[0]=[68][D]


With the null issue at least settle for myself. I'll change my code
over to descriptors to see if it solves the problem.

PS I'll not be happy until I understand how the char * VS descriptor
thing is messing with my calloc. The things that do not add-up yet
are: i'm copying the contents of the cobol string to a c-string, I
inspect the contents of the strings and all seems to be in order, from
then on all I use is the string created in C. The address of the
descriptor is the same as that of the char *, the only difference I
see is that descriptor gives me a size, which is not an issue in my
case. The ACCESSVIOL is raised at different places and never with the
same record. (does tend to repeat somewhat with the same record but
not in a consistent way). We have another C library that we use in
Cobol with char * and never had problems with it for about 4 years
now.

I appreciate every ones help, will keep trying all of your suggestions.



More information about the Info-vax mailing list