473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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::mystring = "Somestring";

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

} // namespace
Dec 17 '07 #1
9 5663
On 2007-12-17 17:42:55 -0500, Christopher <cp***@austin.rr.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::mystring = "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::mystring 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::mystring 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::mystring = "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.Test();

return 0;
}

Dec 17 '07 #3
On Dec 17, 5:04 pm, Christopher <cp...@austin.rr.comwrote:
What is 'somestring' in the method SomeClass::Test()?
Where is SomeClass::mystring 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::mystring = "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::mystring = "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.Test();
'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*****@earthlink.netwrote in message
news:2007121720322416807-kirakun@earthlinknet...
On 2007-12-17 19:05:03 -0500, Christopher <cp***@austin.rr.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, "Christopher Pisz" <some...@somewhere.netwrote:
"Kira Yamato" <kira...@earthlink.netwrote in message

news:2007121720322416807-kirakun@earthlinknet...
On 2007-12-17 19:05:03 -0500, Christopher <cp...@austin.rr.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::mystring("Somestring");

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

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

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

/*
SomestringSomestring
*/

Dec 18 '07 #7
On 2007-12-17 21:19:50 -0500, "Christopher Pisz" <so*****@somewhere.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::mystring instead of a breakpoint. A
watchpoint monitors a memory location and breaks when it changes.

--

-kira

Dec 18 '07 #8

"Kira Yamato" <ki*****@earthlink.netwrote in message
news:2007121723485975249-kirakun@earthlinknet...
On 2007-12-17 21:19:50 -0500, "Christopher Pisz" <so*****@somewhere.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::mystring 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, "Christopher Pisz" <some...@somewhere.netwrote:
"Kira Yamato" <kira...@earthlink.netwrote in message
news:2007121720322416807-kirakun@earthlinknet...
On 2007-12-17 19:05:03 -0500, Christopher
<cp...@austin.rr.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 objektorientierter Datenverarbeitung
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
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
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
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
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...
2
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...
1
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,...
6
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
by: WinniePooh | last post by:
Hi there, I've the following code (here a snippet). ///////////////////////////////// // *.h file ///////////////////////////////// class ParameterTypeNames { public: static const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.