468,503 Members | 2,003 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

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 4708
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by zoe | last post: by
1 post views Thread by Steven T. Hatton | last post: by
5 posts views Thread by Minti | last post: by
9 posts views Thread by Mathieu Malaterre | last post: by
1 post views Thread by gabriel.becedillas | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.