problem with a static variable

Discussion in 'C Programming' started by Jean-Marc, Jun 15, 2004.

  1. Jean-Marc

    Jean-Marc Guest

    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

    --
     
    Jean-Marc, Jun 15, 2004
    #1
    1. Advertising

  2. Jean-Marc

    Eric Sosman Guest

    Jean-Marc wrote:
    > 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.


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

    --
     
    Eric Sosman, Jun 15, 2004
    #2
    1. Advertising

  3. On Tue, 15 Jun 2004, Jean-Marc wrote:

    > 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 "".


    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.

    > 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?


    From my reading of the standard, I'd say that the z/OS compiler is either
    broken or not ISO C compliant.

    > 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
    >
    > --
    >
    >
    >
    >


    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, Jun 15, 2004
    #3
  4. Jean-Marc

    Jean-Marc Guest

    "Darrell Grainger" <> a écrit dans le
    message de news:p...
    > On Tue, 15 Jun 2004, Jean-Marc wrote:
    >
    > > 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 "".

    >
    > I'd try changing the code to:
    >
    > static char szValue[MY_SIZE] = "z/OS is different";


    Nice idea, i'll try this.


    > 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.


    The compiler is said to be ISO compliant and I use the 'ANSI' directive.

    > > 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?

    >
    > From my reading of the standard, I'd say that the z/OS compiler is either
    > broken or not ISO C compliant.


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

    --

    Jean-Marc
     
    Jean-Marc, Jun 15, 2004
    #4
  5. Jean-Marc

    Eric Amick Guest

    On Tue, 15 Jun 2004 20:58:53 +0200, "Jean-Marc"
    <> wrote:

    >Hello all ,
    >
    >i have a strange behaviour with this code.
    >
    >File: func.h
    >--------------
    >#ifndef __FUNC_H__


    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
     
    Eric Amick, Jun 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kookey
    Replies:
    3
    Views:
    20,764
    Andrew Thompson
    Aug 20, 2005
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,841
    Christian Janßen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,426
    Denis Perelyubskiy
    Sep 5, 2003
  4. Sam
    Replies:
    4
    Views:
    475
    The Directive
    Jan 13, 2004
  5. Vikram Kalra
    Replies:
    23
    Views:
    878
    Nigel Wade
    Sep 7, 2007
Loading...

Share This Page