ordering Constructor vs. Static declarations at startup

Well, I’ve got an intersting problem. Still using 6.1a (don’t know
if that matters) porting working code from Watcom (Q4).

I’ve got a class that is performing a sanitiy check on a static
array. The problem is the constructor is being executed before the
static array initialized from a constant array.

Like I said, the code was from Q4/Watcom and working there. Here
are some snipets

in the .cc

#define LOOKUP_REFERENCE(table,scope) {table, scope, table ## _SIZE, #scope}

LOOKUP_ACCESS_TABLE_T StringLookup::stringsInScope[] =
{ // the order of these must match SCOPE_T
LOOKUP_REFERENCE(PATIENT_ALARM_PARM_T_STRING, SCOPE_PATIENT_ALARM_PARM),
LOOKUP_REFERENCE(PATIENT_ALARM_STATE_T_STRING, SCOPE_PATIENT_ALARM_STATE),
LOOKUP_REFERENCE(EQUIP_ALARM_EQTYPE_T_STRING, SCOPE_EQUIP_ALARM_EQTYPE),
LOOKUP_REFERENCE(EQUIP_ALARM_ID_T_STRING, SCOPE_EQUIP_ALARM_ID),
LOOKUP_REFERENCE(EQUIP_ALARM_STATE_T_STRING, SCOPE_EQUIP_ALARM_STATE),
LOOKUP_REFERENCE(SCOPE_DS_ACTION_3_LINE_T_STRING, SCOPE_DS_ACTION_3_LINE),
LOOKUP_REFERENCE(NO_ACTIVITY_LOOKUP, SCOPE_NO_ACTIVITY)
};

and of course

StringLookup::StringLookup()
{
for (int scope=0; scope < tableSize; scope++)
{
if (stringsInScope[scope].scope != scope)
{
printf(“scope name %s enum %d does not match index %d\n”,
stringsInScope[scope].scopeName,
stringsInScope[scope].scope, scope);
}
for (int i = 0; i < stringsInScope[scope].numElements; i++)
{
LOOKUP_T* lookup = stringsInScope[scope].lookupTable;
if (lookup_.enumId != i)
{
printf(“scope %s enum %d does not match index %d\n”,
stringsInScope[scope].scopeName, lookup.enumId, i);
}
}
}

Any wisdom on this one??

I’ve tried changing the scope of the member variable to a global
with no luck there. As well putting the definition into the class
definatition does not help as it creates a declaration loop inside
header files.

Any help would be greatfully appreciated!

Tom_

Disclaimer: not a c++ expert.

Idea: Did you try declaring the array static?

I’m puzzled about the constructor being called though. Shouldn’t it only
get called when you instantiate an object of that class type? Are you
declaring a global object of that type somewhere else?

Kris

“Thomas” <NtOoSmP_AsMt@swbell.net> wrote in message
news:ae36i5$mum$1@inn.qnx.com

Well, I’ve got an intersting problem. Still using 6.1a (don’t know
if that matters) porting working code from Watcom (Q4).

I’ve got a class that is performing a sanitiy check on a static
array. The problem is the constructor is being executed before the
static array initialized from a constant array.
4
Like I said, the code was from Q4/Watcom and working there. Here
are some snipets

in the .cc

#define LOOKUP_REFERENCE(table,scope) {table, scope, table ## _SIZE,
#scope}

LOOKUP_ACCESS_TABLE_T StringLookup::stringsInScope[] =
{ // the order of these must match SCOPE_T
LOOKUP_REFERENCE(PATIENT_ALARM_PARM_T_STRING, SCOPE_PATIENT_ALARM_PARM),
LOOKUP_REFERENCE(PATIENT_ALARM_STATE_T_STRING,
SCOPE_PATIENT_ALARM_STATE),
LOOKUP_REFERENCE(EQUIP_ALARM_EQTYPE_T_STRING, SCOPE_EQUIP_ALARM_EQTYPE),
LOOKUP_REFERENCE(EQUIP_ALARM_ID_T_STRING, SCOPE_EQUIP_ALARM_ID),
LOOKUP_REFERENCE(EQUIP_ALARM_STATE_T_STRING, SCOPE_EQUIP_ALARM_STATE),
LOOKUP_REFERENCE(SCOPE_DS_ACTION_3_LINE_T_STRING,
SCOPE_DS_ACTION_3_LINE),
LOOKUP_REFERENCE(NO_ACTIVITY_LOOKUP, SCOPE_NO_ACTIVITY)
};

and of course

StringLookup::StringLookup()
{
for (int scope=0; scope < tableSize; scope++)
{
if (stringsInScope[scope].scope != scope)
{
printf(“scope name %s enum %d does not match index %d\n”,
stringsInScope[scope].scopeName,
stringsInScope[scope].scope, scope);
}
for (int i = 0; i < stringsInScope[scope].numElements; i++)
{
LOOKUP_T* lookup = stringsInScope[scope].lookupTable;
if (lookup> .enumId != i)
{
printf(“scope %s enum %d does not match index %d\n”,
stringsInScope[scope].scopeName, lookup> .enumId, i);
}
}
}

Any wisdom on this one??

I’ve tried changing the scope of the member variable to a global
with no luck there. As well putting the definition into the class
definatition does not help as it creates a declaration loop inside
header files.

Any help would be greatfully appreciated!

Tom

Kris Warkentin <kewarken@qnx.com> wrote:

Disclaimer: not a c++ expert.

Idea: Did you try declaring the array static?

I’m puzzled about the constructor being called though.
Shouldn’t it only get called when you instantiate an object of
that class type? Are you declaring a global object of that type
somewhere else?

Yup, double checked. And yes, the class is instanciated with a
new from a constructor of another class that happens to be a
singleton :slight_smile:

ahhhhhhhhhh.

I figure their are two approaches, first we can try to rearrange
the files in the linking process to try to get the static in
question to be initialized before the singleton or move the some
of the initialization to an Init function instead of the
constructor.

Option B will probably be safer.

Ahh, the joys of porting legacy code…

thanks,
Tom


(gdb) print stringsInScope
$1 = {{lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0, scopeName = ‘\000’ <repeats 59 times>}}
(gdb) bt
#0 StringLookup (this=Internal: static symbol `StringLookup’ found in tskBase.cc psymtab but not in symtab.
StringLookup may be an inlined function, or may be a template function
(if a template, try specifying an instantiation: StringLookup).
) at stringLookup.cc:96
#1 0x8073f8e in TextStorage (this=0x80d6014) at textStorage.cc:47
#2 0x8074103 in GetInstance () at textStorage.cc:74
#3 0x8072f99 in SysStatWin (this=0x80cb454) at sysStatWin.cc:93
#4 0x8075169 in UiMgr (this=0x80c6fe0) at uiMgr.cc:45
#5 0x807a21f in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at uiMgr.cc:34
#6 0x807a26a in global constructors keyed to UiMgr::UserAttnInterval () at uiMgr.cc:34
#7 0x80a93d3 in __do_global_ctors_aux ()

“Thomas” <NtOoSmP_AsMt@swbell.net> wrote in message
news:ae50jh$52o$1@inn.qnx.com

Kris Warkentin <> kewarken@qnx.com> > wrote:
Disclaimer: not a c++ expert.

Idea: Did you try declaring the array static?

Glad I could help. Forgive my ignorance but I didn’t see the ‘static’
keyword in front of the declaration of stringsInScope[]. Is that done in
the class definition?

Kris

I’m puzzled about the constructor being called though.
Shouldn’t it only get called when you instantiate an object of
that class type? Are you declaring a global object of that type
somewhere else?

Yup, double checked. And yes, the class is instanciated with a
new from a constructor of another class that happens to be a
singleton > :slight_smile:

ahhhhhhhhhh.

I figure their are two approaches, first we can try to rearrange
the files in the linking process to try to get the static in
question to be initialized before the singleton or move the some
of the initialization to an Init function instead of the
constructor.

Option B will probably be safer.

Ahh, the joys of porting legacy code…

thanks,
Tom


(gdb) print stringsInScope
$1 = {{lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements =
0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}}
(gdb) bt
#0 StringLookup (this=Internal: static symbol `StringLookup’ found in
tskBase.cc psymtab but not in symtab.
StringLookup may be an inlined function, or may be a template function
(if a template, try specifying an instantiation: StringLookup).
) at stringLookup.cc:96
#1 0x8073f8e in TextStorage (this=0x80d6014) at textStorage.cc:47
#2 0x8074103 in GetInstance () at textStorage.cc:74
#3 0x8072f99 in SysStatWin (this=0x80cb454) at sysStatWin.cc:93
#4 0x8075169 in UiMgr (this=0x80c6fe0) at uiMgr.cc:45
#5 0x807a21f in __static_initialization_and_destruction_0
(__initialize_p=1, __priority=65535) at uiMgr.cc:34
#6 0x807a26a in global constructors keyed to UiMgr::UserAttnInterval ()
at uiMgr.cc:34
#7 0x80a93d3 in __do_global_ctors_aux ()

Kris Warkentin <kewarken@qnx.com> wrote:

“Thomas” <> NtOoSmP_AsMt@swbell.net> > wrote in message
news:ae50jh$52o$> 1@inn.qnx.com> …
Kris Warkentin <> kewarken@qnx.com> > wrote:
Disclaimer: not a c++ expert.

Idea: Did you try declaring the array static?

Glad I could help. Forgive my ignorance but I didn’t see the ‘static’
keyword in front of the declaration of stringsInScope[]. Is that done in
the class definition?

Just to tripple check I added it in, and same result. sigh…

Well, back to it.

Thanks for helping, if you have any more ideas I will gladly listen.

Tom

There is a way (which I don’t remember) to specify the priority that global
scope and static local scope constructors are executed.

Often I’ll use:
cerr << message
in a constructor. That only works because cerr is constructed at a high
priority.

“Thomas” <NtOoSmP_AsMt@swbell.net> wrote in message
news:ae50jh$52o$1@inn.qnx.com

Kris Warkentin <> kewarken@qnx.com> > wrote:
Disclaimer: not a c++ expert.

Idea: Did you try declaring the array static?

I’m puzzled about the constructor being called though.
Shouldn’t it only get called when you instantiate an object of
that class type? Are you declaring a global object of that type
somewhere else?

Yup, double checked. And yes, the class is instanciated with a
new from a constructor of another class that happens to be a
singleton > :slight_smile:

ahhhhhhhhhh.

I figure their are two approaches, first we can try to rearrange
the files in the linking process to try to get the static in
question to be initialized before the singleton or move the some
of the initialization to an Init function instead of the
constructor.

Option B will probably be safer.

Ahh, the joys of porting legacy code…

thanks,
Tom


(gdb) print stringsInScope
$1 = {{lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements =
0, scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}, {
lookupTable = 0x0, scope = SCOPE_PATIENT_ALARM_PARM, numElements = 0,
scopeName = ‘\000’ <repeats 59 times>}}
(gdb) bt
#0 StringLookup (this=Internal: static symbol `StringLookup’ found in
tskBase.cc psymtab but not in symtab.
StringLookup may be an inlined function, or may be a template function
(if a template, try specifying an instantiation: StringLookup).
) at stringLookup.cc:96
#1 0x8073f8e in TextStorage (this=0x80d6014) at textStorage.cc:47
#2 0x8074103 in GetInstance () at textStorage.cc:74
#3 0x8072f99 in SysStatWin (this=0x80cb454) at sysStatWin.cc:93
#4 0x8075169 in UiMgr (this=0x80c6fe0) at uiMgr.cc:45
#5 0x807a21f in __static_initialization_and_destruction_0
(__initialize_p=1, __priority=65535) at uiMgr.cc:34
#6 0x807a26a in global constructors keyed to UiMgr::UserAttnInterval ()
at uiMgr.cc:34
#7 0x80a93d3 in __do_global_ctors_aux ()