Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: file global strings not initialized ?



Permlink Replies: 8 - Last Post: May 24, 2017 9:25 AM Last Post By: Alex Belo Threads: [ Previous | Next ]
Asger Joergensen

Posts: 370
Registered: 11/18/08
file global strings not initialized ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 21, 2017 2:47 AM
Hi

I have a file containing a lot of small functions I use all the time
and I have these lines at the top:
static const String NL = L"\r\n";
static const String BS = L"\\";
static const wchar_t bs = L'\\';  // OK
static const wchar_t DecSeparator = FormatSettings.DecimalSeparator;
static const wchar_t* Zerros = L"00000000000000000000";  // OK
static const wchar_t* DotZerros = DecSeparator == L',' ? L",00000000000000000000" : L".00000000000000000000";
static const int MaxPrecision = 20;  // OK

I just noticed that four of these are not initialize, only the three wit OK
are initialized, i have these lines:
static const String NL = L"\r\n";
static const String BS = L"\\";

In other files where they do get initialised.

In another project where I use the same file everything does get initialised and in that project
I don't even use any of the functions that use those variables.
I thought it could be because I have the same lines in other files but DotZerros is not in any
other files and that isn't initialised.

What am I missing ?

Thanks in advance
Best regards
Asger
Alex Belo

Posts: 626
Registered: 10/8/06
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 21, 2017 9:00 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:

What am I missing ?

In what file (h, cpp) are these declarations?
How do you use (include) this file?

--
Alex
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 21, 2017 10:37 AM   in response to: Alex Belo in response to: Alex Belo
Hi Alex

Alex Belo wrote:

Asger Joergensen wrote:

What am I missing ?

In what file (h, cpp) are these declarations?

They are only in the cpp file.

How do you use (include) this file?
I add the cpp to the project and include it's h file.

In this project both Zerros and DotZerros are not used,
Zerros is initialised, but DotZerros is not

BS and bs are both in use in the file, but only bs are initialised.

Thanks for helping
Best regards
Asger

Alex Belo

Posts: 626
Registered: 10/8/06
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 22, 2017 8:18 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:

They are only in the cpp file.

I'm asking because people tend to place definitions in h-files and
include them in many compilation units; it leads to appearance of many
independent copies of vars.

AFAIUI your strings are global (in this cpp). Why do you use 'static'
instead of unnamed namespace? I.e.

namespace
{
const String NL = L"\r\n";
const String BS = L"
";
...
}

This is more clear method instead using of "oveloaded" 'static' keyword.

In this project both Zerros and DotZerros are not used,
Zerros is initialised, but DotZerros is not

Static vars can be initialized right before the first use. If var was
not yet in use it can be uninitialized.

I don't know why one unused var is initialized but anoter one is not.

BS and bs are both in use in the file, but only bs are initialised.

At what point? Before or after first use?

--
Alex
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 22, 2017 2:20 PM   in response to: Alex Belo in response to: Alex Belo
Hi Alex

Alex Belo wrote:

Asger Joergensen wrote:

AFAIUI your strings are global (in this cpp).

Correct.

Why do you use 'static'

I thought it made them only visible in that cpp file

instead of unnamed namespace? I.e.

didn't know about that one, autodedact. :)

namespace
{
const String NL = L"\r\n";
const String BS = L"
";
...
}

I just tried that, but it doesn't help, if I go to the function that uses
BS and Ctrl+Click the editor takes me to the right line at the top of the
file and the contents of BS is NULL. I discovered this problem because
the path produced by the function had no \ just a long line of letters.

i have now made a h and a cpp file where I:
h file
extern const String NL;
extern const String BS;
...
cpp file
const String NL = L"\r\n";
const String BS = L"
";
...

then I include the h file when ever needed and it seem to work fine

Static vars can be initialized right before the first use. If var was
not yet in use it can be uninitialized.

even though they are const ?

BS and bs are both in use in the file, but only bs are initialised.

At what point? Before or after first use?

both

Thanks for helping
Best regards
Asger
Alex Belo

Posts: 626
Registered: 10/8/06
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 23, 2017 9:12 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:

I thought it made them only visible in that cpp file

Yes, this is correct, but unnamed namespace is "more C++" method.
Along with vars you can use it for all other things (local functions,
classes, etc.).

I just tried that, but it doesn't help, if I go to the function that
uses BS and Ctrl+Click the editor takes me to the right line at the
top of the file and the contents of BS is NULL. I discovered this
problem because the path produced by the function had no \ just a
long line of letters.

If you use these strings in different places try comparing addresses of
strings in these different places. Are they the same?

Also you can set breakpoint on static declaration and open CPU window
when execution will stop on it. somewhere in asm code you should see
call of String constructor (at least I think so :-)). Set BP on this
call, remove initial BP, and continue execution. If this BP will fire
again then something is wrong...

i have now made a h and a cpp file where I:
h file
extern const String NL;
extern const String BS;
...
cpp file
const String NL = L"\r\n";
const String BS = L"
";
...

then I include the h file when ever needed and it seem to work fine

OK, as expected. I still suspect that you had unwanted copies of
strings in your initial code. Aforementioned experiment with addresses
could confirm or disprove my hypothesis.

Static vars can be initialized right before the first use. If var
was not yet in use it can be uninitialized.

even though they are const ?

why not? String is class (not plain constant data) so compiler must
insert a call of constructor somewhere.

BS and bs are both in use in the file, but only bs are
initialised.

At what point? Before or after first use?

both

Hmm...
If addresses are the same then it looks as bug.
What is your version?
Upload minimal example in attachments, I'll try it in CB2007.

--
Alex
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 23, 2017 3:33 PM   in response to: Alex Belo in response to: Alex Belo
Hi Alex

Alex Belo wrote:

Asger Joergensen wrote:

I thought it made them only visible in that cpp file

Yes, this is correct, but unnamed namespace is "more C++" method.
Along with vars you can use it for all other things (local functions,
classes, etc.).

But I really don't get the logic, in putting a nameless namespace around
a bunch of vars, to make them available only to the code, that comes after
in the same file.

If you use these strings in different places try comparing addresses of
strings in these different places. Are they the same?

I cant seem to find a way to do that, because I cant set a breakpoint
where the vars are initialised, but I tried adding an extra variable:

namespace{
const String TestStr = L"xxxxx";
const String NL = L"\r\n";
const String BS = L"
";
const String empty = L"";
}

and using it in a function that does get called, it is also NOT initialised
and there are no other copies.

I also tried using namespace asj{.. the same name as the namespace in witch
the vars are used, but that didn't make a difference.

even though they are const ?

why not? String is class (not plain constant data) so compiler must
insert a call of constructor somewhere.

does that mean that a const string in a function isn't const either ?
e.g.
String DoStringThing(const String str);

What is your version?

Berlin

Upload minimal example in attachments, I'll try it in CB2007.

I can't make it happen in other projects.:( :)

Thanks for explaining
Best regards
Asger
Alex Belo

Posts: 626
Registered: 10/8/06
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 24, 2017 9:25 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:

But I really don't get the logic, in putting a nameless namespace
around a bunch of vars, to make them available only to the code, that
comes after in the same file.

There are some disscussions on this theme like
https://stackoverflow.com/questions/357404/why-are-unnamed-namespaces-used-and-what-are-their-benefits
https://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions

In short it can be useful in too general cases:

- when you use C-style code and need to create interface between local
functions;
- when you need to provide some local helpers like predicates
(comparer, for example) for stl algorithms. In C++ 11 it is better to
use lambda-functions but in "classic" C++ anonimous namespace is very
helpful for this.

does that mean that a const string in a function isn't const either ?
e.g.
String DoStringThing(const String str);

str will be const inside DoStringThing but before passing str into
function it must be created somewhere with constructor.

I can't make it happen in other projects.

Sometimes only dances with Shaman's tambourine can help. :-)

One of shamanic method is recreation of the project from scratch...

--
Alex
Alex Belo

Posts: 626
Registered: 10/8/06
Re: file global strings not initialized ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 21, 2017 9:10 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:

What am I missing ?

Could they be optimized as unused?

--
Alex
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02