473,769 Members | 5,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initializing public static const std::string member

Code compiles, but the string evaluates to NULL in debugger. Using gcc
3.1.1.
Did I not initialize the string properly or is this a compiler bug?

// some.h

#include <string>

namespace ns
{
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}

static const std::string mystring;

void Test();
};
} // namespace

// some.cpp

#include "some.h"

namespace ns
{
const std::string SomeClass::myst ring = "Somestring ";

void SomeClass::Test ()
{
// somestring evaluates to NULL and seg faults here!!!
cout << somestring + somestring;
}

} // namespace
Dec 17 '07 #1
9 5740
On 2007-12-17 17:42:55 -0500, Christopher <cp***@austin.r r.comsaid:
Code compiles, but the string evaluates to NULL in debugger. Using gcc
3.1.1.
Did I not initialize the string properly or is this a compiler bug?

// some.h

#include <string>

namespace ns
{
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}

static const std::string mystring;

void Test();
};
} // namespace

// some.cpp

#include "some.h"

namespace ns
{
const std::string SomeClass::myst ring = "Somestring ";

void SomeClass::Test ()
{
// somestring evaluates to NULL and seg faults here!!!
cout << somestring + somestring;
}

} // namespace
What is 'somestring' in the method SomeClass::Test ()?

Where is SomeClass::myst ring actually being used?

Perhaps it is better if you can produce a small actual program that
demonstrates the problem.

--

-kira

Dec 17 '07 #2
What is 'somestring' in the method SomeClass::Test ()?

Where is SomeClass::myst ring actually being used?

Typos - edit:

Code compiles, but the string evaluates to NULL in debugger. Using gcc
3.1.1.
Did I not initialize the string properly or is this a compiler bug?

// some.h

#include <string>

namespace ns
{
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}

static const std::string mystring;

void Test();
};

} // namespace

// some.cpp

#include "some.h"

namespace ns
{
const std::string SomeClass::myst ring = "Somestring ";

void SomeClass::Test ()
{
// somestring evaluates to NULL and seg faults here!!!
cout << mystring + mystring;
}
}

// main.cpp
#include "some.h"

int main()
{
ns::SomeClass tester;

ns::SomeClass.T est();

return 0;
}

Dec 17 '07 #3
On Dec 17, 5:04 pm, Christopher <cp...@austin.r r.comwrote:
What is 'somestring' in the method SomeClass::Test ()?
Where is SomeClass::myst ring actually being used?

Typos - edit:

Code compiles, but the string evaluates to NULL in debugger. Using gcc
3.1.1.
Did I not initialize the string properly or is this a compiler bug?

// some.h

#include <string>

namespace ns
{
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}

static const std::string mystring;

void Test();
};

} // namespace

// some.cpp

#include "some.h"

namespace ns
{
const std::string SomeClass::myst ring = "Somestring ";

void SomeClass::Test ()
{
// somestring evaluates to NULL and seg faults here!!!
cout << mystring + mystring;
}

}

// main.cpp
#include "some.h"

int main()
{
ns::SomeClass tester;
tester.Test();
>
return 0;

}

I hate having to stare at one screen and manually type things in
another.
Dec 17 '07 #4
Christopher wrote:
[..]
Code compiles, but the string evaluates to NULL in debugger. Using gcc
3.1.1.
Did I not initialize the string properly or is this a compiler bug?
What does it mean "the string evaluates to NULL in debugger"?
If you don't know whether the string has been initialised or not,
put the breakpoint in every std::string constructor so that you
know when one is actually constructed.
>
// some.h

#include <string>

namespace ns
{
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}

static const std::string mystring;

void Test();
};

} // namespace

// some.cpp

#include "some.h"

namespace ns
{
const std::string SomeClass::myst ring = "Somestring ";

void SomeClass::Test ()
{
// somestring evaluates to NULL and seg faults here!!!
I am not sure I understand that comment.
cout << mystring + mystring;
Not sure 'cout' is defined here. Did you include <iostream>?
}
}

// main.cpp
#include "some.h"

int main()
{
ns::SomeClass tester;

ns::SomeClass.T est();
'Test' is non-static member. You cannot call it without
an instance. Did you mean

tester.Test();

?
>
return 0;
}
Please post the _actual_ code that exhibits the error you are
trying to correct.

While it is possible that it's a bug in the compiler, there is
no way to conclude that if the code you post doesn't even compile.

For the testing purposes (when you're trying to figure out if it
is or isn't a compiler bug), you need to try different things,
like putting everything in the same translation unit.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 17 '07 #5

"Kira Yamato" <ki*****@earthl ink.netwrote in message
news:2007121720 322416807-kirakun@earthli nknet...
On 2007-12-17 19:05:03 -0500, Christopher <cp***@austin.r r.comsaid:
But of course, that was just some madeup code you typed out. Now do you
see why you should post actual code instead of madeup code? Your madeup
code may actually work because it does not reflect the actual code.
You are missing the point that the original question is not "Can anyone find
the problem for me", but rather, "Can someone help eliminate a possiblilty
for a problem?"

I couldn't possibly type in 50k lines, and it wasn't possible in this case
to figure out what _minimal_ was until the problem was known. It just
happens in this case that minimal would be:
A) some class that gets statically instantiated
B) through its constructor - instantiates another class which has static
members
*I think and am working on verifying that*

However, that was not known at the time of the posting. So instead, I wanted
to narrow it down by verifying that the static initialization in that
example was done correctly (the second half of B). Then, if it was indeed
correct, search through the rest of the code for more possible problems (A
and the first half of B).

You have to make up code when you are dealing with property that is not your
own. Your employer would be quite angry if you posted actual code that was
getting used commercially. I tryed to post a minimal example for the minimal
set of factors that were known. When those factors are found to not be at
fault, then one can safley move on considering others. I apologize for the
typos and not being able to compile an example, but that's all I can
apologize for. If I had tryed to reproduce it, I would have miserably
failed, not knowing the cause. It could take weeks or more of trial and
error, and then of course I would have known my answer and not had a
question in the first place. I think the process of elimination is much more
productive. Once I didn't get any complaints about the code aside from the
typos, from Victor, I was confident that it should work and was able to dig
deeper looking for more possibilities, eliminatiing the possibility of
missing something in the simple scenario of initialising static members.


Dec 18 '07 #6
On Dec 17, 9:19 pm, "Christophe r Pisz" <some...@somewh ere.netwrote:
"Kira Yamato" <kira...@earthl ink.netwrote in message

news:2007121720 322416807-kirakun@earthli nknet...
On 2007-12-17 19:05:03 -0500, Christopher <cp...@austin.r r.comsaid:
But of course, that was just some madeup code you typed out. Now do you
see why you should post actual code instead of madeup code? Your madeup
code may actually work because it does not reflect the actual code.

You are missing the point that the original question is not "Can anyone find
the problem for me", but rather, "Can someone help eliminate a possiblilty
for a problem?"

I couldn't possibly type in 50k lines, and it wasn't possible in this case
to figure out what _minimal_ was until the problem was known. It just
happens in this case that minimal would be:
A) some class that gets statically instantiated
B) through its constructor - instantiates another class which has static
members
*I think and am working on verifying that*

However, that was not known at the time of the posting. So instead, I wanted
to narrow it down by verifying that the static initialization in that
example was done correctly (the second half of B). Then, if it was indeed
correct, search through the rest of the code for more possible problems (A
and the first half of B).

You have to make up code when you are dealing with property that is not your
own. Your employer would be quite angry if you posted actual code that was
getting used commercially. I tryed to post a minimal example for the minimal
set of factors that were known. When those factors are found to not be at
fault, then one can safley move on considering others. I apologize for the
typos and not being able to compile an example, but that's all I can
apologize for. If I had tryed to reproduce it, I would have miserably
failed, not knowing the cause. It could take weeks or more of trial and
error, and then of course I would have known my answer and not had a
question in the first place. I think the process of elimination is much more
productive. Once I didn't get any complaints about the code aside from the
typos, from Victor, I was confident that it should work and was able to dig
deeper looking for more possibilities, eliminatiing the possibility of
missing something in the simple scenario of initialising static members.

Nobody is asking you to Post copyrighted code, some code that
generates the problem would help.
For example the following code will not compile:

int main()
{
std::cout << "a short string";
}

Just as well, if i suggest to you that the following generates an
error:

a = b;

its impossible to know why the above failed if it did.

Try something as follows perhaps, its still rather strange that its we
that need to supply a working scenario (which in this case doesn't
generate that segfault). Note: the member mystring is static, it only
exists in exactly one translation unit. That fact is something you
need to consider should you decide to restate the problem.

// some.h
#ifndef SOME_H_
#define SOME_H_

#include <string>

namespace ns
{
class SomeClass
{
static const std::string mystring;
public:
SomeClass() {}
~SomeClass() {}

void Test();
};
}

#endif

// some.cpp
#include <iostream>
#include "some.h"
using namespace ns;

const std::string SomeClass::myst ring("Somestrin g");

void SomeClass::Test ()
{
std::cout << mystring + mystring;
std::cout << std::endl;
}

// main.cpp
#include "some.h"

int main()
{
ns::SomeClass tester;
tester.Test();
}

/*
SomestringSomes tring
*/

Dec 18 '07 #7
On 2007-12-17 21:19:50 -0500, "Christophe r Pisz" <so*****@somewh ere.netsaid:
>
However, that was not known at the time of the posting. So instead, I wanted
to narrow it down by verifying that the static initialization in that
example was done correctly (the second half of B). Then, if it was indeed
correct, search through the rest of the code for more possible problems (A
and the first half of B).
Ok. Then, no. Possibility eliminated. Search somewhere else in your
code for the problem.

One suggestion, before you execute the program in the debugger, try to
place a watchpoint for SomeClass::myst ring instead of a breakpoint. A
watchpoint monitors a memory location and breaks when it changes.

--

-kira

Dec 18 '07 #8

"Kira Yamato" <ki*****@earthl ink.netwrote in message
news:2007121723 485975249-kirakun@earthli nknet...
On 2007-12-17 21:19:50 -0500, "Christophe r Pisz" <so*****@somewh ere.net>
said:
Ok. Then, no. Possibility eliminated. Search somewhere else in your
code for the problem.

One suggestion, before you execute the program in the debugger, try to
place a watchpoint for SomeClass::myst ring instead of a breakpoint. A
watchpoint monitors a memory location and breaks when it changes.
Good suggestion. I've kind of been ignoring that for awhile now and need to
learn how to do it in eclipse. I had it down in VS, but they stuck me in a
foreign envirnment. There are a few debugging sessions that would have went
alot easier if I had that available. Thanks for the helps. I know I might
sound argumentetive at times, but you can't convey tone of voice in text.
All the responses were helpful and don't go without my appreciatation. There
are a number of people I need to fed-ex a beer to in this ng.
Dec 18 '07 #9
On Dec 18, 3:19 am, "Christophe r Pisz" <some...@somewh ere.netwrote:
"Kira Yamato" <kira...@earthl ink.netwrote in message
news:2007121720 322416807-kirakun@earthli nknet...
On 2007-12-17 19:05:03 -0500, Christopher
<cp...@austin.r r.comsaid: But of course, that was just
some madeup code you typed out. Now do you see why you
should post actual code instead of madeup code? Your madeup
code may actually work because it does not reflect the
actual code.
You are missing the point that the original question is not
"Can anyone find the problem for me", but rather, "Can someone
help eliminate a possiblilty for a problem?"
I couldn't possibly type in 50k lines, and it wasn't possible
in this case to figure out what _minimal_ was until the
problem was known. It just happens in this case that minimal
would be:
A) some class that gets statically instantiated
B) through its constructor - instantiates another class which has static
members
*I think and am working on verifying that*
However, that was not known at the time of the posting. So
instead, I wanted to narrow it down by verifying that the
static initialization in that example was done correctly (the
second half of B). Then, if it was indeed correct, search
through the rest of the code for more possible problems (A and
the first half of B).
You're missing the point. Had you compiled and run the code you
posted, after correcting the obvious typos, you'd have found
that it works. You wouldn't need to post here for that. Your
question suggested that there was something that didn't work in
the code you posted. Since no one here can duplicate the error,
there's no way we can help you.
You have to make up code when you are dealing with property
that is not your own.
Of course. And no one wants you to post 50 000 lines of code
even if you could. You should, however, verify that the small
example you post displays the symptom you are asking about. If
not, you're just wasting your time and ours.

Often, of course, getting the small sample to display the
symptom will provide the insight as to where the error is coming
from, and you won't have to post. (I've experienced this once
or twice when preparing bug reports for g++. My original small
example would not display the bug, and in trying to find what
actually triggered it, incorporating different aspects of my
original code into the example, I've found that the "bug" was
actually an error in my own code.)
Your employer would be quite angry if you posted actual code
that was getting used commercially. I tryed to post a minimal
example for the minimal set of factors that were known. When
those factors are found to not be at fault, then one can
safley move on considering others. I apologize for the typos
and not being able to compile an example, but that's all I can
apologize for. If I had tryed to reproduce it, I would have
miserably failed, not knowing the cause. It could take weeks
or more of trial and error, and then of course I would have
known my answer and not had a question in the first place. I
think the process of elimination is much more productive. Once
I didn't get any complaints about the code aside from the
typos, from Victor, I was confident that it should work and
was able to dig deeper looking for more possibilities,
eliminatiing the possibility of missing something in the
simple scenario of initialising static members.
You'd have gotten that information much more quickly by trying
to compile your small example.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Dec 18 '07 #10

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

Similar topics

2
1932
by: zoe | last post by:
Is it possible to initialize a non-static const array class-member of a fundamental type? Something like: class Foo{}{ public: Foo(); private: const int myArray;
1
2546
by: Steven T. Hatton | last post by:
Is there a way to initialize an member array of user defined objects that is declared static const? For example: const vec3f I(1,0,0); const vec3f j(0,1,0); class corner_functor {
5
1713
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
9
10275
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not only seems to be a bad practice but also creates a bug on MacOSX panther (gcc 3.3).
2
10937
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
1
1984
by: gabriel.becedillas | last post by:
I have a lot of functions returning "const std::string&". Every time I wrap one of those I have to do it like this: class_.def("name", &someclass::bla, boost::python::return_value_policy<boost::python::copy_const_reference>() ); Is there a way to register a return value conversion for "const std::string&" so I can omit it every time I have to wrap functions returning "const std::string&" ? I wan't those strings to be copied to
6
15670
by: DaTurk | last post by:
I'm converting MC++ to C++/CLI and for some reason it's not cool with static const System::String^ How am I supposed to have static constant strings then?
2
1614
by: WinniePooh | last post by:
Hi there, I've the following code (here a snippet). ///////////////////////////////// // *.h file ///////////////////////////////// class ParameterTypeNames { public: static const std::string FLOAT; static const std::string INT; // ...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.