struct pointer not set - IBM AS400

This is a discussion on struct pointer not set - IBM AS400 ; I defined a struct and did a malloc to obtain storage for it. I then initialized the vars within the structure. I initially tested the code on AIX and it worked, but when I recreated the code on the AS/400, ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: struct pointer not set

  1. struct pointer not set

    I defined a struct and did a malloc to obtain storage for it. I then
    initialized the vars within the structure. I initially tested the code
    on AIX and it worked, but when I recreated the code on the AS/400, it
    fails and complains that the pointer (to the struct) was not set. I
    don't see how it would work on AIX and fail on the AS/400. Any help
    would be appreciated. The snippet of code is:

    typedef _Packed struct varrec {
    short ll;
    char vrec[MAXBUFFL-sizeof(short)];
    }varrec_t;

    varrec_t *vrecp;

    vrecp = NULL;

    vrecp = (varrec_t *)malloc(MAXBUFFL);

    if (vrecp == NULL) {
    printf("malloc for vrecp failed\n");
    exit(1);
    } /* end if */
    else
    printf("malloc for vrecp worked\n");

    i = strlen(REC1);
    (*vrecp).ll = i;

    /* at this point it fails */


  2. Re: struct pointer not set


    rtsujimoto_consultant@cusa.canon.com wrote:
    > I defined a struct and did a malloc to obtain storage for it. I then
    > initialized the vars within the structure. I initially tested the code
    > on AIX and it worked, but when I recreated the code on the AS/400, it
    > fails and complains that the pointer (to the struct) was not set. I
    > don't see how it would work on AIX and fail on the AS/400. Any help
    > would be appreciated. The snippet of code is:
    >
    > typedef _Packed struct varrec {
    > short ll;
    > char vrec[MAXBUFFL-sizeof(short)];
    > }varrec_t;
    >
    > varrec_t *vrecp;
    >
    > vrecp = NULL;
    >
    > vrecp = (varrec_t *)malloc(MAXBUFFL);
    >
    > if (vrecp == NULL) {
    > printf("malloc for vrecp failed\n");
    > exit(1);
    > } /* end if */
    > else
    > printf("malloc for vrecp worked\n");
    >
    > i = strlen(REC1);
    > (*vrecp).ll = i;
    >
    > /* at this point it fails */


    works for me. what error are you getting? On the AS400 you do have to
    watch out for pointer alignment errors. Pointers in structures have to
    be on 16 byte boundaries or else the code will bomb with an alignment
    error.

    -Steve

    void TestBombingCode( )
    {
    #define MAXBUFFL 512
    typedef _Packed struct varrec {
    short ll;
    char vrec[MAXBUFFL-sizeof(short)];
    }varrec_t;

    varrec_t *vrecp;
    short Lx ;

    vrecp = NULL;
    vrecp = (varrec_t *)malloc(MAXBUFFL);

    if (vrecp == NULL) {
    printf("malloc for vrecp failed\n");
    exit(1);
    } /* end if */
    else
    printf("malloc for vrecp worked\n");

    Lx = 25 ;
    (*vrecp).ll = Lx ;

    /* runs ok. */
    }


  3. Re: struct pointer not set


    Steve Richter wrote:
    > rtsujimoto_consultant@cusa.canon.com wrote:
    > > I defined a struct and did a malloc to obtain storage for it. I then
    > > initialized the vars within the structure. I initially tested the code
    > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > fails and complains that the pointer (to the struct) was not set. I
    > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > would be appreciated. The snippet of code is:
    > >
    > > typedef _Packed struct varrec {
    > > short ll;
    > > char vrec[MAXBUFFL-sizeof(short)];
    > > }varrec_t;
    > >
    > > varrec_t *vrecp;
    > >
    > > vrecp = NULL;
    > >
    > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > >
    > > if (vrecp == NULL) {
    > > printf("malloc for vrecp failed\n");
    > > exit(1);
    > > } /* end if */
    > > else
    > > printf("malloc for vrecp worked\n");
    > >
    > > i = strlen(REC1);
    > > (*vrecp).ll = i;
    > >
    > > /* at this point it fails */

    >
    > works for me. what error are you getting? On the AS400 you do have to
    > watch out for pointer alignment errors. Pointers in structures have to
    > be on 16 byte boundaries or else the code will bomb with an alignment
    > error.
    >
    > -Steve
    >
    > void TestBombingCode( )
    > {
    > #define MAXBUFFL 512
    > typedef _Packed struct varrec {
    > short ll;
    > char vrec[MAXBUFFL-sizeof(short)];
    > }varrec_t;
    >
    > varrec_t *vrecp;
    > short Lx ;
    >
    > vrecp = NULL;
    > vrecp = (varrec_t *)malloc(MAXBUFFL);
    >
    > if (vrecp == NULL) {
    > printf("malloc for vrecp failed\n");
    > exit(1);
    > } /* end if */
    > else
    > printf("malloc for vrecp worked\n");
    >
    > Lx = 25 ;
    > (*vrecp).ll = Lx ;
    >
    > /* runs ok. */
    > }


    I see the following when I do DSPJOBLOG:

    Pointer not set for location referenced.
    Application error. MCH3601 unmonitored by TSTVARWRIT at statement
    0000000012, instruction X'0000'.


    I'm going to copy your code and see if it works.


  4. Re: struct pointer not set


    rtsujimoto_consultant@cusa.canon.com wrote:
    > Steve Richter wrote:
    > > rtsujimoto_consultant@cusa.canon.com wrote:
    > > > I defined a struct and did a malloc to obtain storage for it. I then
    > > > initialized the vars within the structure. I initially tested the code
    > > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > > fails and complains that the pointer (to the struct) was not set. I
    > > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > > would be appreciated. The snippet of code is:
    > > >
    > > > typedef _Packed struct varrec {
    > > > short ll;
    > > > char vrec[MAXBUFFL-sizeof(short)];
    > > > }varrec_t;
    > > >
    > > > varrec_t *vrecp;
    > > >
    > > > vrecp = NULL;
    > > >
    > > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > > >
    > > > if (vrecp == NULL) {
    > > > printf("malloc for vrecp failed\n");
    > > > exit(1);
    > > > } /* end if */
    > > > else
    > > > printf("malloc for vrecp worked\n");
    > > >
    > > > i = strlen(REC1);
    > > > (*vrecp).ll = i;
    > > >
    > > > /* at this point it fails */

    > >
    > > works for me. what error are you getting? On the AS400 you do have to
    > > watch out for pointer alignment errors. Pointers in structures have to
    > > be on 16 byte boundaries or else the code will bomb with an alignment
    > > error.
    > >
    > > -Steve
    > >
    > > void TestBombingCode( )
    > > {
    > > #define MAXBUFFL 512
    > > typedef _Packed struct varrec {
    > > short ll;
    > > char vrec[MAXBUFFL-sizeof(short)];
    > > }varrec_t;
    > >
    > > varrec_t *vrecp;
    > > short Lx ;
    > >
    > > vrecp = NULL;
    > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > >
    > > if (vrecp == NULL) {
    > > printf("malloc for vrecp failed\n");
    > > exit(1);
    > > } /* end if */
    > > else
    > > printf("malloc for vrecp worked\n");
    > >
    > > Lx = 25 ;
    > > (*vrecp).ll = Lx ;
    > >
    > > /* runs ok. */
    > > }

    >
    > I see the following when I do DSPJOBLOG:
    >
    > Pointer not set for location referenced.
    > Application error. MCH3601 unmonitored by TSTVARWRIT at statement
    > 0000000012, instruction X'0000'.
    >
    >
    > I'm going to copy your code and see if it works.


    I copied your code and get the same error. The only difference is I
    have main() and you have TestBombingCode( ). Frustrating.


  5. Re: struct pointer not set


    rtsujimoto_consultant@cusa.canon.com wrote:
    > Steve Richter wrote:
    > > rtsujimoto_consultant@cusa.canon.com wrote:
    > > > I defined a struct and did a malloc to obtain storage for it. I then
    > > > initialized the vars within the structure. I initially tested the code
    > > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > > fails and complains that the pointer (to the struct) was not set. I
    > > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > > would be appreciated. The snippet of code is:
    > > >
    > > > typedef _Packed struct varrec {
    > > > short ll;
    > > > char vrec[MAXBUFFL-sizeof(short)];
    > > > }varrec_t;
    > > >
    > > > varrec_t *vrecp;
    > > >
    > > > vrecp = NULL;
    > > >
    > > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > > >
    > > > if (vrecp == NULL) {
    > > > printf("malloc for vrecp failed\n");
    > > > exit(1);
    > > > } /* end if */
    > > > else
    > > > printf("malloc for vrecp worked\n");
    > > >
    > > > i = strlen(REC1);
    > > > (*vrecp).ll = i;
    > > >
    > > > /* at this point it fails */

    > >
    > > works for me. what error are you getting? On the AS400 you do have to
    > > watch out for pointer alignment errors. Pointers in structures have to
    > > be on 16 byte boundaries or else the code will bomb with an alignment
    > > error.
    > >
    > > -Steve
    > >
    > > void TestBombingCode( )
    > > {
    > > #define MAXBUFFL 512
    > > typedef _Packed struct varrec {
    > > short ll;
    > > char vrec[MAXBUFFL-sizeof(short)];
    > > }varrec_t;
    > >
    > > varrec_t *vrecp;
    > > short Lx ;
    > >
    > > vrecp = NULL;
    > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > >
    > > if (vrecp == NULL) {
    > > printf("malloc for vrecp failed\n");
    > > exit(1);
    > > } /* end if */
    > > else
    > > printf("malloc for vrecp worked\n");
    > >
    > > Lx = 25 ;
    > > (*vrecp).ll = Lx ;
    > >
    > > /* runs ok. */
    > > }

    >
    > I see the following when I do DSPJOBLOG:
    >
    > Pointer not set for location referenced.
    > Application error. MCH3601 unmonitored by TSTVARWRIT at statement
    > 0000000012, instruction X'0000'.


    compile with debug set to *ALL:
    CRTCMOD MODULE(libname/TEST17D)
    SRCFILE(QRPGLESRC)
    SRCMBR(TEST17D)
    DBGVIEW(*ALL)

    then create the program:
    crtpgm test17d

    then start debug:
    strdbg test17d

    the interactive debugger shows the source code. Use F6 to add a
    breakpoint.

    run the program:
    call test17d

    use F10 to step thru the code.

    after the malloc you can display the struct:
    eval *vrecp

    your statement 12 does not look right for the line you said was
    failing. You can try to take the _packed off the struct, but that
    should not matter.

    ( If you think this is hard you should try to learn AIX )

    -Steve


  6. Re: struct pointer not set



    rtsujimoto_consultant@cusa.canon.com wrote:
    > rtsujimoto_consultant@cusa.canon.com wrote:
    >> Steve Richter wrote:
    >>> rtsujimoto_consultant@cusa.canon.com wrote:
    >>>> I defined a struct and did a malloc to obtain storage for it. I then
    >>>> initialized the vars within the structure. I initially tested the code
    >>>> on AIX and it worked, but when I recreated the code on the AS/400, it
    >>>> fails and complains that the pointer (to the struct) was not set. I
    >>>> don't see how it would work on AIX and fail on the AS/400. Any help
    >>>> would be appreciated. The snippet of code is:
    >>>>
    >>>> typedef _Packed struct varrec {
    >>>> short ll;
    >>>> char vrec[MAXBUFFL-sizeof(short)];
    >>>> }varrec_t;
    >>>>
    >>>> varrec_t *vrecp;
    >>>>
    >>>> vrecp = NULL;
    >>>>
    >>>> vrecp = (varrec_t *)malloc(MAXBUFFL);
    >>>>
    >>>> if (vrecp == NULL) {
    >>>> printf("malloc for vrecp failed\n");
    >>>> exit(1);
    >>>> } /* end if */
    >>>> else
    >>>> printf("malloc for vrecp worked\n");
    >>>>
    >>>> i = strlen(REC1);
    >>>> (*vrecp).ll = i;
    >>>>
    >>>> /* at this point it fails */
    >>> works for me. what error are you getting? On the AS400 you do have to
    >>> watch out for pointer alignment errors. Pointers in structures have to
    >>> be on 16 byte boundaries or else the code will bomb with an alignment
    >>> error.
    >>>
    >>> -Steve
    >>>
    >>> void TestBombingCode( )
    >>> {
    >>> #define MAXBUFFL 512
    >>> typedef _Packed struct varrec {
    >>> short ll;
    >>> char vrec[MAXBUFFL-sizeof(short)];
    >>> }varrec_t;
    >>>
    >>> varrec_t *vrecp;
    >>> short Lx ;
    >>>
    >>> vrecp = NULL;
    >>> vrecp = (varrec_t *)malloc(MAXBUFFL);
    >>>
    >>> if (vrecp == NULL) {
    >>> printf("malloc for vrecp failed\n");
    >>> exit(1);
    >>> } /* end if */
    >>> else
    >>> printf("malloc for vrecp worked\n");
    >>>
    >>> Lx = 25 ;
    >>> (*vrecp).ll = Lx ;
    >>>
    >>> /* runs ok. */
    >>> }

    >> I see the following when I do DSPJOBLOG:
    >>
    >> Pointer not set for location referenced.
    >> Application error. MCH3601 unmonitored by TSTVARWRIT at statement
    >> 0000000012, instruction X'0000'.
    >>
    >>
    >> I'm going to copy your code and see if it works.

    >
    > I copied your code and get the same error. The only difference is I
    > have main() and you have TestBombingCode( ). Frustrating.
    >


    It said it failed on statement 12. Looking at the compile listing, is
    that the (*vrecp).ll = i statement or the one after it?

    If you debug it with STRDBG and single-step through, what does "ev
    vrecp" show just before you execute the (*vrecp).ll = statement?

    What OS/400 release are you on? What were the parms you entered for the
    CRTCMOD and CRTPGM cmds?

    --Dave

  7. Re: struct pointer not set


    Steve Richter wrote:
    > rtsujimoto_consultant@cusa.canon.com wrote:
    > > Steve Richter wrote:
    > > > rtsujimoto_consultant@cusa.canon.com wrote:
    > > > > I defined a struct and did a malloc to obtain storage for it. I then
    > > > > initialized the vars within the structure. I initially tested the code
    > > > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > > > fails and complains that the pointer (to the struct) was not set. I
    > > > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > > > would be appreciated. The snippet of code is:
    > > > >
    > > > > typedef _Packed struct varrec {
    > > > > short ll;
    > > > > char vrec[MAXBUFFL-sizeof(short)];
    > > > > }varrec_t;
    > > > >
    > > > > varrec_t *vrecp;
    > > > >
    > > > > vrecp = NULL;
    > > > >
    > > > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > > > >
    > > > > if (vrecp == NULL) {
    > > > > printf("malloc for vrecp failed\n");
    > > > > exit(1);
    > > > > } /* end if */
    > > > > else
    > > > > printf("malloc for vrecp worked\n");
    > > > >
    > > > > i = strlen(REC1);
    > > > > (*vrecp).ll = i;
    > > > >
    > > > > /* at this point it fails */
    > > >
    > > > works for me. what error are you getting? On the AS400 you do have to
    > > > watch out for pointer alignment errors. Pointers in structures have to
    > > > be on 16 byte boundaries or else the code will bomb with an alignment
    > > > error.
    > > >
    > > > -Steve
    > > >
    > > > void TestBombingCode( )
    > > > {
    > > > #define MAXBUFFL 512
    > > > typedef _Packed struct varrec {
    > > > short ll;
    > > > char vrec[MAXBUFFL-sizeof(short)];
    > > > }varrec_t;
    > > >
    > > > varrec_t *vrecp;
    > > > short Lx ;
    > > >
    > > > vrecp = NULL;
    > > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > > >
    > > > if (vrecp == NULL) {
    > > > printf("malloc for vrecp failed\n");
    > > > exit(1);
    > > > } /* end if */
    > > > else
    > > > printf("malloc for vrecp worked\n");
    > > >
    > > > Lx = 25 ;
    > > > (*vrecp).ll = Lx ;
    > > >
    > > > /* runs ok. */
    > > > }

    > >
    > > I see the following when I do DSPJOBLOG:
    > >
    > > Pointer not set for location referenced.
    > > Application error. MCH3601 unmonitored by TSTVARWRIT at statement
    > > 0000000012, instruction X'0000'.

    >
    > compile with debug set to *ALL:
    > CRTCMOD MODULE(libname/TEST17D)
    > SRCFILE(QRPGLESRC)
    > SRCMBR(TEST17D)
    > DBGVIEW(*ALL)
    >
    > then create the program:
    > crtpgm test17d
    >
    > then start debug:
    > strdbg test17d
    >
    > the interactive debugger shows the source code. Use F6 to add a
    > breakpoint.
    >
    > run the program:
    > call test17d
    >
    > use F10 to step thru the code.
    >
    > after the malloc you can display the struct:
    > eval *vrecp
    >
    > your statement 12 does not look right for the line you said was
    > failing. You can try to take the _packed off the struct, but that
    > should not matter.
    >
    > ( If you think this is hard you should try to learn AIX )
    >
    > -Steve

    The code has been shuffled since the last posting, so I don't know
    which statement it's flagging, but I have put printf before and after
    the first attempt to modify the malloc'd storage. I fail in the memset.
    I'm off tomorrow, so I'll try to run debug on Thursday.


  8. Re: struct pointer not set

    rtsujimoto_consultant@cusa.canon.com wrote:
    > I defined a struct and did a malloc to obtain storage for it. I then
    > initialized the vars within the structure. I initially tested the code
    > on AIX and it worked, but when I recreated the code on the AS/400, it
    > fails and complains that the pointer (to the struct) was not set. I
    > don't see how it would work on AIX and fail on the AS/400. Any help
    > would be appreciated. The snippet of code is:
    >
    > typedef _Packed struct varrec {
    > short ll;
    > char vrec[MAXBUFFL-sizeof(short)];
    > }varrec_t;
    >
    > varrec_t *vrecp;
    >
    > vrecp = NULL;
    >
    > vrecp = (varrec_t *)malloc(MAXBUFFL);
    >
    > if (vrecp == NULL) {
    > printf("malloc for vrecp failed\n");
    > exit(1);
    > } /* end if */
    > else
    > printf("malloc for vrecp worked\n");
    >
    > i = strlen(REC1);
    > (*vrecp).ll = i;
    >
    > /* at this point it fails */
    >

    In my opinion you're allocating space as vrecp was an array of varrec_t
    types. If it's true vrecp[0].ll = should not fail.
    In these situation, to avoid malloc twice, I malloc a char* and assign
    this pointer to the struct pointer, e.g.:

    char *buf = NULL;
    varrec_t *vrecp;
    buf = malloc(sizeof(varrec_t));
    vrecp = (varrec_t *)buf;


    --
    Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'

  9. Re: struct pointer not set


    Dr.UgoGagliardelli wrote:
    > rtsujimoto_consultant@cusa.canon.com wrote:
    > > I defined a struct and did a malloc to obtain storage for it. I then
    > > initialized the vars within the structure. I initially tested the code
    > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > fails and complains that the pointer (to the struct) was not set. I
    > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > would be appreciated. The snippet of code is:
    > >
    > > typedef _Packed struct varrec {
    > > short ll;
    > > char vrec[MAXBUFFL-sizeof(short)];
    > > }varrec_t;
    > >
    > > varrec_t *vrecp;
    > >
    > > vrecp = NULL;
    > >
    > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > >
    > > if (vrecp == NULL) {
    > > printf("malloc for vrecp failed\n");
    > > exit(1);
    > > } /* end if */
    > > else
    > > printf("malloc for vrecp worked\n");
    > >
    > > i = strlen(REC1);
    > > (*vrecp).ll = i;
    > >
    > > /* at this point it fails */
    > >

    > In my opinion you're allocating space as vrecp was an array of varrec_t
    > types. If it's true vrecp[0].ll = should not fail.
    > In these situation, to avoid malloc twice, I malloc a char* and assign
    > this pointer to the struct pointer, e.g.:
    >
    > char *buf = NULL;
    > varrec_t *vrecp;
    > buf = malloc(sizeof(varrec_t));
    > vrecp = (varrec_t *)buf;
    >
    >
    > --
    > Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    > Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    > Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    > schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'


    I narrowed the problem to malloc - apparently it is not returning a
    valid pointer. I wrote a small program (snippet follows) and ran it on
    AIX and Windows successfully. On the AS/400, it fails with the error
    described above. Very strange. The implication is, I cannot recompile
    any working program that uses malloc, because it will fail. I'm going
    to open an issue with IBM.

    void main ( void )
    {
    char *cp = NULL;

    cp = (char *)malloc(100);
    memset(cp,'\0',100);
    memcpy(cp,"hello",5);
    printf("buffer=%s\n", cp);
    }


  10. Re: struct pointer not set


    rtsujimoto_consultant@cusa.canon.com wrote:
    > Dr.UgoGagliardelli wrote:
    > > rtsujimoto_consultant@cusa.canon.com wrote:
    > > > I defined a struct and did a malloc to obtain storage for it. I then
    > > > initialized the vars within the structure. I initially tested the code
    > > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > > fails and complains that the pointer (to the struct) was not set. I
    > > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > > would be appreciated. The snippet of code is:
    > > >
    > > > typedef _Packed struct varrec {
    > > > short ll;
    > > > char vrec[MAXBUFFL-sizeof(short)];
    > > > }varrec_t;
    > > >
    > > > varrec_t *vrecp;
    > > >
    > > > vrecp = NULL;
    > > >
    > > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > > >
    > > > if (vrecp == NULL) {
    > > > printf("malloc for vrecp failed\n");
    > > > exit(1);
    > > > } /* end if */
    > > > else
    > > > printf("malloc for vrecp worked\n");
    > > >
    > > > i = strlen(REC1);
    > > > (*vrecp).ll = i;
    > > >
    > > > /* at this point it fails */
    > > >

    > > In my opinion you're allocating space as vrecp was an array of varrec_t
    > > types. If it's true vrecp[0].ll = should not fail.
    > > In these situation, to avoid malloc twice, I malloc a char* and assign
    > > this pointer to the struct pointer, e.g.:
    > >
    > > char *buf = NULL;
    > > varrec_t *vrecp;
    > > buf = malloc(sizeof(varrec_t));
    > > vrecp = (varrec_t *)buf;
    > >
    > >
    > > --
    > > Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    > > Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    > > Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    > > schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'

    >
    > I narrowed the problem to malloc - apparently it is not returning a
    > valid pointer. I wrote a small program (snippet follows) and ran it on
    > AIX and Windows successfully. On the AS/400, it fails with the error
    > described above. Very strange. The implication is, I cannot recompile
    > any working program that uses malloc, because it will fail. I'm going
    > to open an issue with IBM.
    >
    > void main ( void )
    > {
    > char *cp = NULL;
    >
    > cp = (char *)malloc(100);
    > memset(cp,'\0',100);
    > memcpy(cp,"hello",5);
    > printf("buffer=%s\n", cp);
    > }


    When I tried to run strdbg, I'm getting: Command STRDBG not valid in
    this mode.
    My AS/400 admin is at a loss on how to get this work. Any ideas?


  11. Re: struct pointer not set

    rtsujimoto_consultant@cusa.canon.com wrote:

    > I narrowed the problem to malloc - apparently it is not returning a
    > valid pointer. I wrote a small program (snippet follows) and ran it on
    > AIX and Windows successfully. On the AS/400, it fails with the error
    > described above. Very strange. The implication is, I cannot recompile
    > any working program that uses malloc, because it will fail. I'm going
    > to open an issue with IBM.
    >
    > void main ( void )
    > {
    > char *cp = NULL;
    >
    > cp = (char *)malloc(100);
    > memset(cp,'\0',100);
    > memcpy(cp,"hello",5);
    > printf("buffer=%s\n", cp);
    > }
    >

    The above should work anyway, try to see in the compile listing for some
    warning that maybe would not prevent to create the program but cause
    such a runtime fault.
    malloc function sets errno if something goes wrong, try to see if any, e.g.:

    void main ( void )
    {
    char *cp = NULL;

    if ((cp = (char *)malloc(100)) != NULL) {
    memset(cp,'\0',100);
    memcpy(cp,"hello",5);
    printf("buffer=%s\n", cp);
    } else {
    perror( "Out of storage" );
    }
    }

    --
    Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'

  12. Re: struct pointer not set


    rtsujimoto_consultant@cusa.canon.com wrote:
    > rtsujimoto_consultant@cusa.canon.com wrote:
    > > Dr.UgoGagliardelli wrote:
    > > > rtsujimoto_consultant@cusa.canon.com wrote:
    > > > > I defined a struct and did a malloc to obtain storage for it. I then
    > > > > initialized the vars within the structure. I initially tested the code
    > > > > on AIX and it worked, but when I recreated the code on the AS/400, it
    > > > > fails and complains that the pointer (to the struct) was not set. I
    > > > > don't see how it would work on AIX and fail on the AS/400. Any help
    > > > > would be appreciated. The snippet of code is:
    > > > >
    > > > > typedef _Packed struct varrec {
    > > > > short ll;
    > > > > char vrec[MAXBUFFL-sizeof(short)];
    > > > > }varrec_t;
    > > > >
    > > > > varrec_t *vrecp;
    > > > >
    > > > > vrecp = NULL;
    > > > >
    > > > > vrecp = (varrec_t *)malloc(MAXBUFFL);
    > > > >
    > > > > if (vrecp == NULL) {
    > > > > printf("malloc for vrecp failed\n");
    > > > > exit(1);
    > > > > } /* end if */
    > > > > else
    > > > > printf("malloc for vrecp worked\n");
    > > > >
    > > > > i = strlen(REC1);
    > > > > (*vrecp).ll = i;
    > > > >
    > > > > /* at this point it fails */
    > > > >
    > > > In my opinion you're allocating space as vrecp was an array of varrec_t
    > > > types. If it's true vrecp[0].ll = should not fail.
    > > > In these situation, to avoid malloc twice, I malloc a char* and assign
    > > > this pointer to the struct pointer, e.g.:
    > > >
    > > > char *buf = NULL;
    > > > varrec_t *vrecp;
    > > > buf = malloc(sizeof(varrec_t));
    > > > vrecp = (varrec_t *)buf;
    > > >
    > > >
    > > > --
    > > > Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    > > > Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    > > > Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    > > > schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'

    > >
    > > I narrowed the problem to malloc - apparently it is not returning a
    > > valid pointer. I wrote a small program (snippet follows) and ran it on
    > > AIX and Windows successfully. On the AS/400, it fails with the error
    > > described above. Very strange. The implication is, I cannot recompile
    > > any working program that uses malloc, because it will fail. I'm going
    > > to open an issue with IBM.
    > >
    > > void main ( void )
    > > {
    > > char *cp = NULL;
    > >
    > > cp = (char *)malloc(100);
    > > memset(cp,'\0',100);
    > > memcpy(cp,"hello",5);
    > > printf("buffer=%s\n", cp);
    > > }

    >
    > When I tried to run strdbg, I'm getting: Command STRDBG not valid in
    > this mode.
    > My AS/400 admin is at a loss on how to get this work. Any ideas?

    Dr. Ugo, you're suggestions paid off! I was receiving warning messages
    during the compilation:
    MQMFTPV2/QCSRC(MALLOCTST), 6.21: CZM0304(10) No function prototype
    given
    for "malloc".

    MQMFTPV2/QCSRC(MALLOCTST), 8.7: CZM0304(10) No function prototype given

    for "memset".

    MQMFTPV2/QCSRC(MALLOCTST), 10.7: CZM0304(10) No function prototype
    given
    for "memcpy".
    I inserted includes for stdlib.h and string.h and the warnings went
    away, and the program worked properly. I assumed the messages were
    harmless (so much for that line of thinking).
    Thanks to all who replied. I'm going to start a new thread on getting
    strdbg to work.


  13. Re: struct pointer not set

    rtsujimoto_consultant@cusa.canon.com wrote:
    > rtsujimoto_consultant@cusa.canon.com wrote:
    >> rtsujimoto_consultant@cusa.canon.com wrote:
    >>> Dr.UgoGagliardelli wrote:
    >>>> rtsujimoto_consultant@cusa.canon.com wrote:
    >>>>> I defined a struct and did a malloc to obtain storage for it. I then
    >>>>> initialized the vars within the structure. I initially tested the code
    >>>>> on AIX and it worked, but when I recreated the code on the AS/400, it
    >>>>> fails and complains that the pointer (to the struct) was not set. I
    >>>>> don't see how it would work on AIX and fail on the AS/400. Any help
    >>>>> would be appreciated. The snippet of code is:
    >>>>>
    >>>>> typedef _Packed struct varrec {
    >>>>> short ll;
    >>>>> char vrec[MAXBUFFL-sizeof(short)];
    >>>>> }varrec_t;
    >>>>>
    >>>>> varrec_t *vrecp;
    >>>>>
    >>>>> vrecp = NULL;
    >>>>>
    >>>>> vrecp = (varrec_t *)malloc(MAXBUFFL);
    >>>>>
    >>>>> if (vrecp == NULL) {
    >>>>> printf("malloc for vrecp failed\n");
    >>>>> exit(1);
    >>>>> } /* end if */
    >>>>> else
    >>>>> printf("malloc for vrecp worked\n");
    >>>>>
    >>>>> i = strlen(REC1);
    >>>>> (*vrecp).ll = i;
    >>>>>
    >>>>> /* at this point it fails */
    >>>>>
    >>>> In my opinion you're allocating space as vrecp was an array of varrec_t
    >>>> types. If it's true vrecp[0].ll = should not fail.
    >>>> In these situation, to avoid malloc twice, I malloc a char* and assign
    >>>> this pointer to the struct pointer, e.g.:
    >>>>
    >>>> char *buf = NULL;
    >>>> varrec_t *vrecp;
    >>>> buf = malloc(sizeof(varrec_t));
    >>>> vrecp = (varrec_t *)buf;
    >>>>
    >>>>
    >>>> --
    >>>> Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    >>>> Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    >>>> Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    >>>> schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'
    >>> I narrowed the problem to malloc - apparently it is not returning a
    >>> valid pointer. I wrote a small program (snippet follows) and ran it on
    >>> AIX and Windows successfully. On the AS/400, it fails with the error
    >>> described above. Very strange. The implication is, I cannot recompile
    >>> any working program that uses malloc, because it will fail. I'm going
    >>> to open an issue with IBM.
    >>>
    >>> void main ( void )
    >>> {
    >>> char *cp = NULL;
    >>>
    >>> cp = (char *)malloc(100);
    >>> memset(cp,'\0',100);
    >>> memcpy(cp,"hello",5);
    >>> printf("buffer=%s\n", cp);
    >>> }

    >> When I tried to run strdbg, I'm getting: Command STRDBG not valid in
    >> this mode.
    >> My AS/400 admin is at a loss on how to get this work. Any ideas?

    > Dr. Ugo, you're suggestions paid off! I was receiving warning messages
    > during the compilation:
    > MQMFTPV2/QCSRC(MALLOCTST), 6.21: CZM0304(10) No function prototype
    > given
    > for "malloc".
    >
    > MQMFTPV2/QCSRC(MALLOCTST), 8.7: CZM0304(10) No function prototype given
    >
    > for "memset".
    >
    > MQMFTPV2/QCSRC(MALLOCTST), 10.7: CZM0304(10) No function prototype
    > given
    > for "memcpy".
    > I inserted includes for stdlib.h and string.h and the warnings went
    > away, and the program worked properly. I assumed the messages were
    > harmless (so much for that line of thinking).
    > Thanks to all who replied. I'm going to start a new thread on getting
    > strdbg to work.
    >

    Usually the message "Command XXXX not valid in this mode" is sent if
    your job is not in the mode for which the command should run.

    In the specific case STRDBG is valid only in *PROD mode, that is if you
    already entered the same command your job is in *DEBUG mode so STRDBG
    cannot be entered another time, unless you enter ENDDBG command.
    To see the valid mode do DSPCMD STRDBG it should show *PROD, if not,
    someone changed the command.
    --
    Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñe joAlcoolInside
    Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    Spamers iros a la mierda/Spamers allez vous faire foutre/Spammers loop
    schijten/Spammers macht Euch vom Acker/Spamerzy wypierdalac'

+ Reply to Thread