473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

--

Nov 14 '05 #1
4 1530
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.

--
Er*********@sun.com

Nov 14 '05 #2
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 vi************@whitehouse.gov
Nov 14 '05 #3
"Darrell Grainger" <da*****@NOMORESPAMcs.utoronto.ca.com> a écrit dans le
message de news:Pi*******************************@drj.pf...
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
Nov 14 '05 #4
On Tue, 15 Jun 2004 20:58:53 +0200, "Jean-Marc"
<no****************@yahoo.fr> 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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
5
by: chandra sekhar | last post by:
Hi All, I am trying to allocate memory for member variable in a static method belongs to same class.. the allocation is o.k and when try to access the member variable I find that it is a null...
4
by: Bangalore | last post by:
Hi all, I am finding quite difficulty in understanding the behaviour of the following program. Base class is singleton, so it should allow the creation of only one object. Eventhough it is...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: hemant.singh | last post by:
I am stuck with strange JS issue My product gives user to show some images(which track mouseover) on page by embedding script like <script src=http://domain.com/GetDynamicJS?domagic=1>...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
2
by: Jeff | last post by:
Hey ..NET 2.0 I'm developing an application which will perform some webservice calls and I believe having those calls in a separate thread may help the app run smoother No user are waiting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.