problem with a static variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Marc

    #1

    problem with a static variable

    Hello all ,

    i have a strange behaviour with this code.

    File: func.h
    --------------
    #ifndef __FUNC_H__
    #define __FUNC_H__

    void init_value(void );
    char *get_value(void );

    #endif


    file: func.c
    --------------
    #include<stdio. h>
    #include<string .h>

    #define MY_SIZE 50

    static char szValue[MY_SIZE] = "";

    void init_value(void )
    {
    strcpy(szValue, "TEST TEST TEST");
    }

    char *get_value(void )
    {
    return szValue;
    }

    file: main.c
    --------------
    #include<stdlib .h>
    #include<stdio. h>
    #include "func.h"

    int main(void)
    {
    char *p = NULL;

    init_value();
    p = get_value();
    if(p == NULL)
    {
    printf("p is NULL here.\n");
    }
    else
    {
    printf("p : %s\n", p);
    /* here, under z/OS => *p= '\0' */
    }
    return EXIT_SUCCESS;
    }

    This code compiles without errors/warings using several compilers, under
    several OS:
    Windows, Unix (SUN & AIX) & z/OS

    The problem is that under z/OS, get_value() does not return "TEST TEST TEST"
    but "".

    REMARK: if i change the following
    static char szValue[MY_SIZE] by char szValue[MY_SIZE], then it works.

    I wonder if this code is correct, meaning, does it invoke an undefined
    behavior?
    And if yes, why ? I have carefully read the C norma, but i can not find any
    related topics.

    Thanks for the help,

    Jean-marc

    --



  • Eric Sosman

    #2
    Re: problem with a static variable

    Jean-Marc wrote:[color=blue]
    > Hello all ,
    >
    > i have a strange behaviour with this code.
    > [snipped; see up-thread]
    >
    > This code compiles without errors/warings using several compilers, under
    > several OS:
    > Windows, Unix (SUN & AIX) & z/OS
    >
    > The problem is that under z/OS, get_value() does not return "TEST TEST TEST"
    > but "".
    >
    > REMARK: if i change the following
    > static char szValue[MY_SIZE] by char szValue[MY_SIZE], then it works.
    >
    > I wonder if this code is correct, meaning, does it invoke an undefined
    > behavior?
    > And if yes, why ? I have carefully read the C norma, but i can not find any
    > related topics.[/color]

    Your code looks fine to me. You may have discovered
    a bug in the z/OS C implementation.

    --
    Eric.Sosman@sun .com

    Comment

    • Darrell Grainger

      #3
      Re: problem with a static variable

      On Tue, 15 Jun 2004, Jean-Marc wrote:
      [color=blue]
      > Hello all ,
      >
      > i have a strange behaviour with this code.
      >
      > File: func.h
      > --------------
      > #ifndef __FUNC_H__
      > #define __FUNC_H__
      >
      > void init_value(void );
      > char *get_value(void );
      >
      > #endif
      >
      >
      > file: func.c
      > --------------
      > #include<stdio. h>
      > #include<string .h>
      >
      > #define MY_SIZE 50
      >
      > static char szValue[MY_SIZE] = "";
      >
      > void init_value(void )
      > {
      > strcpy(szValue, "TEST TEST TEST");
      > }
      >
      > char *get_value(void )
      > {
      > return szValue;
      > }
      >
      > file: main.c
      > --------------
      > #include<stdlib .h>
      > #include<stdio. h>
      > #include "func.h"
      >
      > int main(void)
      > {
      > char *p = NULL;
      >
      > init_value();
      > p = get_value();
      > if(p == NULL)
      > {
      > printf("p is NULL here.\n");
      > }
      > else
      > {
      > printf("p : %s\n", p);
      > /* here, under z/OS => *p= '\0' */
      > }
      > return EXIT_SUCCESS;
      > }
      >
      > This code compiles without errors/warings using several compilers, under
      > several OS:
      > Windows, Unix (SUN & AIX) & z/OS
      >
      > The problem is that under z/OS, get_value() does not return "TEST TEST TEST"
      > but "".[/color]

      I'd try changing the code to:

      static char szValue[MY_SIZE] = "z/OS is different";

      If the z/OS compiler prints this message then you know the compiler is
      treating global static variables as constant.

      I'd also see if the compiler claims to be ISO compliant. Maybe it is not
      or maybe you need to add some switches to the compiler command to make it
      ISO C.
      [color=blue]
      > REMARK: if i change the following
      > static char szValue[MY_SIZE] by char szValue[MY_SIZE], then it works.
      >
      > I wonder if this code is correct, meaning, does it invoke an undefined
      > behavior?[/color]

      From my reading of the standard, I'd say that the z/OS compiler is either
      broken or not ISO C compliant.
      [color=blue]
      > And if yes, why ? I have carefully read the C norma, but i can not find any
      > related topics.
      >
      > Thanks for the help,
      >
      > Jean-marc
      >
      > --
      >
      >
      >
      >[/color]

      --
      Send e-mail to: darrell at cs dot toronto dot edu
      Don't send e-mail to vice.president@ whitehouse.gov

      Comment

      • Jean-Marc

        #4
        Re: problem with a static variable

        "Darrell Grainger" <darrell@NOMORE SPAMcs.utoronto .ca.com> a écrit dans le
        message de news:Pine.GSO.4 .58.04061515122 40.15042@drj.pf ...[color=blue]
        > On Tue, 15 Jun 2004, Jean-Marc wrote:
        >[color=green]
        > > Hello all ,
        > >
        > > i have a strange behaviour with this code.
        > >
        > > File: func.h
        > > --------------
        > > #ifndef __FUNC_H__
        > > #define __FUNC_H__
        > >
        > > void init_value(void );
        > > char *get_value(void );
        > >
        > > #endif
        > >
        > >
        > > file: func.c
        > > --------------
        > > #include<stdio. h>
        > > #include<string .h>
        > >
        > > #define MY_SIZE 50
        > >
        > > static char szValue[MY_SIZE] = "";
        > >
        > > void init_value(void )
        > > {
        > > strcpy(szValue, "TEST TEST TEST");
        > > }
        > >
        > > char *get_value(void )
        > > {
        > > return szValue;
        > > }
        > >
        > > file: main.c
        > > --------------
        > > #include<stdlib .h>
        > > #include<stdio. h>
        > > #include "func.h"
        > >
        > > int main(void)
        > > {
        > > char *p = NULL;
        > >
        > > init_value();
        > > p = get_value();
        > > if(p == NULL)
        > > {
        > > printf("p is NULL here.\n");
        > > }
        > > else
        > > {
        > > printf("p : %s\n", p);
        > > /* here, under z/OS => *p= '\0' */
        > > }
        > > return EXIT_SUCCESS;
        > > }
        > >
        > > This code compiles without errors/warings using several compilers, under
        > > several OS:
        > > Windows, Unix (SUN & AIX) & z/OS
        > >
        > > The problem is that under z/OS, get_value() does not return "TEST TEST[/color][/color]
        TEST"[color=blue][color=green]
        > > but "".[/color]
        >
        > I'd try changing the code to:
        >
        > static char szValue[MY_SIZE] = "z/OS is different";[/color]

        Nice idea, i'll try this.

        [color=blue]
        > If the z/OS compiler prints this message then you know the compiler is
        > treating global static variables as constant.
        >
        > I'd also see if the compiler claims to be ISO compliant. Maybe it is not
        > or maybe you need to add some switches to the compiler command to make it
        > ISO C.[/color]

        The compiler is said to be ISO compliant and I use the 'ANSI' directive.
        [color=blue][color=green]
        > > REMARK: if i change the following
        > > static char szValue[MY_SIZE] by char szValue[MY_SIZE], then it works.
        > >
        > > I wonder if this code is correct, meaning, does it invoke an undefined
        > > behavior?[/color]
        >
        > From my reading of the standard, I'd say that the z/OS compiler is either
        > broken or not ISO C compliant.[/color]

        I'll investigate on that way. Thanks for the help.

        --

        Jean-Marc


        Comment

        • Eric Amick

          #5
          Re: problem with a static variable

          On Tue, 15 Jun 2004 20:58:53 +0200, "Jean-Marc"
          <nospamjean_mar c_n2@yahoo.fr> wrote:
          [color=blue]
          >Hello all ,
          >
          >i have a strange behaviour with this code.
          >
          >File: func.h
          >--------------
          >#ifndef __FUNC_H__[/color]

          I doubt it has anything to do with your problem, but you should remember
          that all symbols starting with two underscores are reserved for the
          implementation' s use.

          --
          Eric Amick
          Columbia, MD

          Comment

          Working...