problem with a static variable

J

Jean-Marc

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

--
 
E

Eric Sosman

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

Darrell Grainger

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

Jean-Marc

Darrell Grainger said:
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.
 
E

Eric Amick

Hello all ,

i have a strange behaviour with this code.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top