public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* global const variable not getting assigned properly
@ 2006-12-08  0:37 Chad Scholes
  2006-12-08 12:44 ` John Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Chad Scholes @ 2006-12-08  0:37 UTC (permalink / raw)
  To: gcc-help

I have the following global const defined at the top of a .cpp file:

const wchar_t * FILESPEC	= L"filespec";
const unsigned int LEN_PATH = 5  + wcslen(FILESPEC) ;


Then in a class method I reference LEN_PATH:

printf("The length is %d\n", LEN_PATH);

LEN_PATH is 0 (zero).  If I don't use the wide character version (change wchar_t to char and wcslen to strlen) then it works, I get LEN_PATH set to 13.  Also, if I just assign LEN_PATH = 5 or LEN_PATH = 5 + 8, then it also works.

Any idea why it is 0 when using wcslen?  And any idea why I'm not even getting an error to notify me of the problem?

Thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: global const variable not getting assigned properly
  2006-12-08  0:37 global const variable not getting assigned properly Chad Scholes
@ 2006-12-08 12:44 ` John Love-Jensen
  0 siblings, 0 replies; 5+ messages in thread
From: John Love-Jensen @ 2006-12-08 12:44 UTC (permalink / raw)
  To: Chad Scholes, MSX to GCC

Hi Chad,

> Any idea why it is 0 when using wcslen?  And any idea why I'm not even getting
> an error to notify me of the problem?

Works on my platform with my version of GCC with the BSD Standard C Library
(ISO/IEC 9899:1999 compliant).

I'm guessing its either your Standard C Library or your version of GCC or
your platform.

What version of Standard C Library are you using?

Who is the provider of your Standard C Library (e.g., GLibC)?

What version of GCC are you using?

What platform are you using?

Also, your code snippet and explanation of the behavior is nice, but could
you provide a short complete code example that demonstrates the problem.
Maybe its something simple like you used the incorrect header files (e.g.,
<wchar.h> instead of <cwchar>).

And could you provide the GCC command line that you used to compile your
short complete code example.

Thanks,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: global const variable not getting assigned properly
  2006-12-08 23:15 Chad Scholes
  2006-12-09 14:44 ` John (Eljay) Love-Jensen
@ 2006-12-11 12:43 ` John Love-Jensen
  1 sibling, 0 replies; 5+ messages in thread
From: John Love-Jensen @ 2006-12-11 12:43 UTC (permalink / raw)
  To: Chad Scholes, MSX to GCC

Hi Chad,

> Do you know why they are not getting initialized?

I presume because the .SO's global initializer is not being called.

> If I call dlopen instead will that help solve this problem?

Maybe.

> Is there something I can set to get those initialized before I use them?

Yes.

Instead of writing:
const int uno = 1;
const int dos = 2;
const int tres = 3;

Write this:

int GetUno()
{
  static int uno = 1;
  return uno;
}
int GetDos()
{
  static int dos = 1;
  return dos;
}
int GetTres()
{
  static int tres = 3;
  return tres;
}

Then use the accessor functions instead of globals.

In C/C++, there is no guarantee as to the order of construction betwixt
translation units.  This technique will guarantee order of construction
(provided you do not have a circular dependency).

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: global const variable not getting assigned properly
  2006-12-08 23:15 Chad Scholes
@ 2006-12-09 14:44 ` John (Eljay) Love-Jensen
  2006-12-11 12:43 ` John Love-Jensen
  1 sibling, 0 replies; 5+ messages in thread
From: John (Eljay) Love-Jensen @ 2006-12-09 14:44 UTC (permalink / raw)
  To: Chad Scholes, gcc-help

Hi Chad,

When you compiled the other shared library, did you use the same version of GCC (4.0.2 or 4.1.0) that you are using for your executable?

It sounds like a compiler version mismatch of some sort.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: global const variable not getting assigned properly
@ 2006-12-08 23:15 Chad Scholes
  2006-12-09 14:44 ` John (Eljay) Love-Jensen
  2006-12-11 12:43 ` John Love-Jensen
  0 siblings, 2 replies; 5+ messages in thread
From: Chad Scholes @ 2006-12-08 23:15 UTC (permalink / raw)
  To: eljay, gcc-help

So, I have found a little more information:

I have a Shared Library.  In that library is defined a number of static classes and const variables (they are global variables).  We need them to be static and/or const so they get loaded only once and everyone can reference them.

If I create an exe and reference any of those static classes or const (defined in the Shared Library) everything is fine.  However,  If I create a shared library that then calls any of the static classes or consts (defined in the other shared library) then none of the statics or consts are initialized.

I am linking the other library at Link time with the -lsoname parameter.  Do you know why they are not getting initialized?  If I call dlopen instead will that help solve this problem?  Is there something I can set to get those initialized before I use them?

I am using g++ version 4.0.2 on Suse 10.  I also get the same results compiling with  g++ (GCC) 4.1.0 (SUSE Linux).

Thanks!!!!


>>> John Love-Jensen <eljay@adobe.com> 12/08/06 5:44 AM >>>
Hi Chad,

> Any idea why it is 0 when using wcslen?  And any idea why I'm not even getting
> an error to notify me of the problem?

Works on my platform with my version of GCC with the BSD Standard C Library
(ISO/IEC 9899:1999 compliant).

I'm guessing its either your Standard C Library or your version of GCC or
your platform.

What version of Standard C Library are you using?

Who is the provider of your Standard C Library (e.g., GLibC)?

What version of GCC are you using?

What platform are you using?

Also, your code snippet and explanation of the behavior is nice, but could
you provide a short complete code example that demonstrates the problem.
Maybe its something simple like you used the incorrect header files (e.g.,
<wchar.h> instead of <cwchar>).

And could you provide the GCC command line that you used to compile your
short complete code example.

Thanks,
--Eljay


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-12-11 12:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-08  0:37 global const variable not getting assigned properly Chad Scholes
2006-12-08 12:44 ` John Love-Jensen
2006-12-08 23:15 Chad Scholes
2006-12-09 14:44 ` John (Eljay) Love-Jensen
2006-12-11 12:43 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).