Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

static variables

Posted on 1998-08-17
10
Medium Priority
?
283 Views
Last Modified: 2011-09-20
This question might have to do with some knowledge of AIX4.2.x based C++ compiler(xlC_r). I am new to this environment. here it goes:

i've a C++ class file with a certain static variable(say "astat". this goes into a shared library and this library is linked into an executable.
now when I run this executable(the file that intializes/defines this static var is part of only the shared library), i find 2 addresses for the static varible "astat" when accessed from 2 different functions.  why would  this happen ??

if you are familiar with AIX's makeC++SharedLib_r/xlC_r to create shared libs and executables respectively, is there a link line option to supress these multiple copies ?
0
Comment
Question by:rajaram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 2

Expert Comment

by:mlev
ID: 1170569
The definition/initialization isn't in a header file, right?
0
 

Author Comment

by:rajaram
ID: 1170570
here it goes:

file:  hdr.h
       -----

class A {
  private:
    static sp var1;      // sp - a struct definition.
  public:
    ..
    ..
    stativ void initVars();
    static void someOperation();
    ..
    ..
};
________________________________________________________________

file  impl.cpp
      --------

sp A::var1;

void A::initVars() {
   // initialize the static varible
};

void A::someOperation() {
   // this function is accessed by various routines to
   // use/manipulate the stativ var
};

.
.
____________________________________________________________


0
 
LVL 3

Expert Comment

by:xyu
ID: 1170571
Did You tell to the compiler & linker that class A is expoted/imported any way ???
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:xyu
ID: 1170572
I see additional problem in Your code ... You have to specify initializer to You static variable... nobody garanted order of initialization of statics
0
 

Author Comment

by:rajaram
ID: 1170573
1st comment :
i have not used any options which will tell the compiler/linker to export/import these files. frankly i dont know if I have to
do it or how to export/import files ?

2nd comment:

please see the .cpp file. the first line (sp A::var1;) should
do. atleast this works on SOLARIS/HPUX/WIN-NT.
do i've to equate it to NULL or something ??
0
 
LVL 3

Expert Comment

by:xyu
ID: 1170574
2 nd comment... its good practice to specify initializer (constructor) for static variable (by stabdard it have to be zerroed, but i'v seen alredy 2 cases where it wasn't 0) :)

look at documentation on Your compiler for keywords export/import, _export/_import ... it may help You... I don't have compiler here so i can't check it ... but export/import is standard keyword... so check it.. and shoe me the results if any... i'll help You then  
0
 

Author Comment

by:rajaram
ID: 1170575
i'll hunting for the compiler/linker documentation. once i get
them i'll try and let you know.
thanks.
0
 
LVL 3

Expert Comment

by:xyu
ID: 1170576
the suggestion is the next try to do like that

in hdr.h

#ifndef HDR_DECLSPEC
  #define HDR_DECLSPEC _export
#endif

class HDR_DECLSPEC A {
  private:
    static sp var1;      // sp - a struct definition.
  public:
    ..
    ..
    stativ void initVars();
    static void someOperation();
    ..
    ..
};

and before using in in the main (non-shared part of the program)
do
#define HDR_DECLSPEC _import

it may help
0
 

Author Comment

by:rajaram
ID: 1170577
WHEN does one require to export/import files ?
btw, this is an encouraging suggestion. thanks!

Coming from a SOLARIS background, i've never understood the
logic/reason behind export/import of files(if possible please
answer the "WHEN" part).

great suggestion!!
0
 
LVL 3

Accepted Solution

by:
xyu earned 360 total points
ID: 1170578
I make it as answer... so i could get some points :)

ok..

when You specifying _export You are not exporting the file You just instruct compiler to generate class code that way that it can berecognized by linker as exportable for generation of export tables of shared module... the same way when You specifying _import You instruct the compiler not to jenerate any implementation of class methods and VFT and class statics but prepere information for linket to import it ...

so in the shared module source it should look like that
class _export Foo {...}

and in main module (that using the shared one):
class _import Foo {...}

so example i gave to You before shows one of the ideas to provide this difference...

try it on Your compiler (it may work)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question