473,231 Members | 1,788 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,231 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 5592
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.