Map Seg Fault

S

subaruwrx88011

I posted this question earlier and made some changes. Due to the topic
getting off topic I decided to post another one.

Here is the code.
#include <map>
#include <string>
#include <iostream>
using namespace std;

class CCSI_EnviromentClass
{
private:

//Constructor
CCSI_EnviromentClass();


struct StateType
{
int m_integer;
string m_string;
float m_float;

StateType() : m_integer(-1), m_float(1.0) {}
};

typedef std::map<std::string, StateType> ItemStateMap;

ItemStateMap m_itemMap;

static CCSI_EnviromentClass *ms_instance;

public:

//Destructor
~CCSI_EnviromentClass();

//Methods

static CCSI_EnviromentClass *instance();

int getItemStateInt(std::string item);
float getItemStateFloat(std::string item);
std::string getItemStateString(std::string item);

int setItemState(std::string item, std::string state);
int setItemState(std::string item, float state);
int setItemState(std::string item, int state);

void print();
};

//Initialize
CCSI_EnviromentClass *CCSI_EnviromentClass::ms_instance = NULL;

CCSI_EnviromentClass::CCSI_EnviromentClass()
{
//cout << "Instance of EnviromentClass Created" << endl;
}


CCSI_EnviromentClass::~CCSI_EnviromentClass()
{
ms_instance = NULL;
//cout << "Instance of EnviromentClass Destroyed" << endl;
}

CCSI_EnviromentClass *CCSI_EnviromentClass::instance()
{
if( ms_instance == NULL)
{
ms_instance = new CCSI_EnviromentClass();
}
return ms_instance;
}

std::string CCSI_EnviromentClass::getItemStateString(std::string item)
{
StateType state;

ItemStateMap::iterator map_iterator;

//Find the item name in the list
map_iterator = m_itemMap.find(item);

//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_string; //return string part of union
}
else
{
return NULL;
}

}

float CCSI_EnviromentClass::getItemStateFloat(std::string item)
{
StateType state;

ItemStateMap::iterator map_iterator;

//Find the item name in the list
map_iterator = m_itemMap.find(item);

//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_float; //return float part of union
}
else
{
return -1;
}
}

int CCSI_EnviromentClass::getItemStateInt(std::string item)
{
StateType state;

ItemStateMap::iterator map_iterator;

//Find the item name in the list
map_iterator = m_itemMap.find(item);

//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_integer; //return float part of union
}
else
{
return -1;
}
}

int CCSI_EnviromentClass::setItemState(std::string p_item, std::string
p_state)
{

StateType state;
state.m_string = p_state;

m_itemMap.insert(make_pair(p_item,state)); //THIS IS WHERE IT IS SEG
FAULTING
//m_itemMap[p_item] = state; //THIS IS WHERE IT IS SEG FAULTING
return 0;
}

int CCSI_EnviromentClass::setItemState(std::string p_item, float
p_state)
{
StateType state;
state.m_float = p_state;
m_itemMap.insert(make_pair(p_item,state));
//m_itemMap[p_item] = state;
return 0;
}

int CCSI_EnviromentClass::setItemState(std::string p_item, int p_state)
{
StateType state;
state.m_integer = p_state;
m_itemMap.insert(make_pair(p_item,state));
//m_itemMap[p_item] = state;
return 0;
}


void CCSI_EnviromentClass::print()
{
ItemStateMap::iterator mapIterator;

for(mapIterator = m_itemMap.begin(); mapIterator != m_itemMap.end();
mapIterator++)
{
cout << mapIterator->first << " = ";
mapIterator->second.print();
cout << endl;
}
}

Can anyone tell me why this is seg faulting? That will greatly be
appreciated.

Thanks subaru88011
 
V

Victor Bazarov

subaruwrx88011 said:
I posted this question earlier and made some changes. Due to the topic
getting off topic I decided to post another one.

.... And you miss again! Your code does not compile!
Here is the code.
#include <map>
#include <string>
#include <iostream>
using namespace std;

class CCSI_EnviromentClass
{
private:

//Constructor
CCSI_EnviromentClass();


struct StateType
{
int m_integer;
string m_string;
float m_float;

StateType() : m_integer(-1), m_float(1.0) {}
};

typedef std::map<std::string, StateType> ItemStateMap;

ItemStateMap m_itemMap;

static CCSI_EnviromentClass *ms_instance;

public:

//Destructor
~CCSI_EnviromentClass();

//Methods

static CCSI_EnviromentClass *instance();

int getItemStateInt(std::string item);
float getItemStateFloat(std::string item);
std::string getItemStateString(std::string item);

int setItemState(std::string item, std::string state);
int setItemState(std::string item, float state);
int setItemState(std::string item, int state);

void print();
};
[...]

void CCSI_EnviromentClass::print()
{
ItemStateMap::iterator mapIterator;

for(mapIterator = m_itemMap.begin(); mapIterator != m_itemMap.end();
mapIterator++)
{
cout << mapIterator->first << " = ";
mapIterator->second.print();

This does not compile. There is no member 'print' in 'StateType'.
cout << endl;
}
}

Can anyone tell me why this is seg faulting? That will greatly be
appreciated.

Can you post _real_ code?

V
 
S

subaruwrx88011

Are you trying to compile this?
I do have a print method in StateType. But it is irrevelant to what I
am asking. The problem is seg faulting in the setItemState methods.
My code compiles, I guarentee that.

Since you insist
Here is the StateType print method
Place it inside the struct StateType

void print()
{
if (m_integer != -1)
{
cout<< m_integer << " ";
}
else if(m_string.empty())
{
cout << m_string << " ";
}
else if(m_float != -1.0)
{
cout << m_float << " ";
}
}

and m_float is initialized to -1.0 not 1.0. That was a miss type on my
part.
 
R

roberts.noah

subaruwrx88011 said:
Are you trying to compile this?
I do have a print method in StateType. But it is irrevelant to what I
am asking. The problem is seg faulting in the setItemState methods.
My code compiles, I guarentee that.


Now that the class compiles what do we do with it? How can it be run
in a debugger if we don't have a set of statements that causes failure?

I get bored rather quickly so if you want an answer you kind of have to
help us help you.
 
V

Victor Bazarov

subaruwrx88011 said:
Are you trying to compile this?

Not only do I try to compile. I will actually run it if it compiles and
links successfully. I am fairly certain that it won't link since there
is no 'main' function. The ball is again in your court.

Let me spell it out for you: it is _impossible_ to determine the reason
for the undefined behaviour (segfault is just one manifestation of the UB)
without seeing _how_ the function where the alleged segfault happens is
being called. *Do you get this?*
I do have a print method in StateType. But it is irrevelant to what I
am asking. The problem is seg faulting in the setItemState methods.
My code compiles, I guarentee that.

I don't care what you guarantee. You're not providing enough information
to answer your question. If you want us to speculate on _all_ possible
reasons why it could segfault, then the margins of those postings are not
wide enough to contain our response.
Since you insist
[...]

I couldn't care less. If you need the answer, post the right information.
If you don't post, you don't need the answer. I'll grant you one more try
and after that welcome to my killfile.

V
 
S

subaruwrx88011

I can't post the whole application on here but ill do my best to give
an example.

int main()
{
CCSI_EnviromentClass *env;
string s;
int i;
float f;
string item1 = "Item1";
string item2 = "Item2";
string item3 = "Item3";
string online = "Online";
int state=1;
float freq=23.6;

env = CCSI_EnviromentClass::instance();

env->setItemState(item1,online);
env->setItemState(item2,state);
env->setItemState(item3,freq);

s = env->getItemStateString(item1);
i = env->getItemStateInt(item2);
f = env->getItemStateFloat(item3);

cout << item1 << " = " << s << endl;
cout << item2 << " = " << i << endl;
cout << item3 << " = " << f << endl;
}

I really do appreciate your guy's help
 
S

subaruwrx88011

Woh, hold on man. I told you before that I am new to this c++ thing and
this google group thing. Cut me some slack man. In no way am I being
negative or sarcastic. I appreciate you trying to help and im doing my
best to give you what you need.
 
V

Victor Bazarov

subaruwrx88011 said:
I can't post the whole application on here but ill do my best to give
an example.

int main()
{
CCSI_EnviromentClass *env;
string s;
int i;
float f;
string item1 = "Item1";
string item2 = "Item2";
string item3 = "Item3";
string online = "Online";
int state=1;
float freq=23.6;

env = CCSI_EnviromentClass::instance();

env->setItemState(item1,online);
env->setItemState(item2,state);
env->setItemState(item3,freq);

s = env->getItemStateString(item1);
i = env->getItemStateInt(item2);
f = env->getItemStateFloat(item3);

cout << item1 << " = " << s << endl;
cout << item2 << " = " << i << endl;
cout << item3 << " = " << f << endl;
}

I really do appreciate your guy's help

The code as posted, combined with 'CCSI_EnvironmentClass' definition and
your 'print' member addition, compiles and runs _just_fine_. Test it
yourself.

Now, if your "whole application" does crash, and you can't post its code
(which is understandable), you're on your own. I can only tell you to
learn to use a debugger and run your application under one, and then let
it crash, and then see what led to the crash, analyse the values of all
the objects involved, try to understand the flow that leads to the error,
and so on. IOW, welcome to the land of _real_world_programming_.

V
 
V

Victor Bazarov

subaruwrx88011 said:
Woh, hold on man. I told you before that I am new to this c++ thing and
this google group thing. Cut me some slack man. In no way am I being
negative or sarcastic. I appreciate you trying to help and im doing my
best to give you what you need.

Believe me, you have all slack you possibly can handle. If you're new,
have you read the FAQ? If not, how come? If you did, why didn't you
follow the recommendations of the section 5? There is plenty of slack,
you see, you just need to start picking it up.

V
 
S

subaruwrx88011

Well...
I thankyour for looking into it. I haven't used a debugger and we don't
have access to one here so....guess ill keep trucking along.
but have a good day man and thanks.
 
D

Default User

subaruwrx88011 said:
Well...
I thankyour for looking into it. I haven't used a debugger and we
don't have access to one here so....guess ill keep trucking along.
but have a good day man and thanks.

While you're getting pummelled, notice how most of the regulars have
quotes and such in their replies? That makes it much easier to follow
what going on. See the information below.



Brian
 
S

subaruwrx88011

Default said:
While you're getting pummelled, notice how most of the regulars have
quotes and such in their replies? That makes it much easier to follow
what going on. See the information below.



Brian


Thanks for the tip. Appreciate it. I will get the hang of this soon.
 
H

Howard

subaruwrx88011 said:
I posted this question earlier and made some changes. Due to the topic
getting off topic I decided to post another one.
int CCSI_EnviromentClass::setItemState(std::string p_item, std::string
p_state)
{

StateType state;
state.m_string = p_state;

m_itemMap.insert(make_pair(p_item,state)); //THIS IS WHERE IT IS SEG
FAULTING
//m_itemMap[p_item] = state; //THIS IS WHERE IT IS SEG FAULTING
return 0;
}


I may be mistaken, but...

[asking others here]

isn't there still a problem (even using std::string instead of char*) when
adding the object to the map? Doesn't it involve making a copy of the
StateType object? And because of the std::string member, doesn't that mean
that a copy-constructor for copying the string itself is going to be needed?
(Or does the default copying of the struct handle strings properly already?)

To the OP: if you don't have a debugger, how do you know where it's
crashing? Also, you _really_ need to use a debugger when developing. Don't
you at least have access to gdb, (if not a complete development environment
(editor+compiler+debugger))?

-Howard
 
B

Ben Pope

subaruwrx88011 said:
Woh, hold on man. I told you before that I am new to this c++ thing and
this google group thing. Cut me some slack man. In no way am I being
negative or sarcastic. I appreciate you trying to help and im doing my
best to give you what you need.

You've been asked several times to post a "complete, compilable example".

That means we can copy-paste it directly into one (1) file, compile it
and see what you are telling us you see. If it's a compile error, fine.
Compilable doesn't mean it must compile successfully, merely that we
are able to replicate your problem.

Every single time you've been asked to do that, you miss some part of
the code out, and we are unable to replicate your problem.

If it means that you have to spend time chopping your program up, before
posting it here, then so be it. You need to spend that time so that we
are able to replicate your problem as easily as possible.

We like helping, really, we do. We get satisfaction in knowing that
somebody is now wiser. What we do not enjoy is going round in circles,
asking you to post complete code, being told that it's complete when it
is not.

Don't forget, we do this for free, in our own time. It is you that has
the problem, and it is in your best interest to make the problem as
clear as possible. We see the same basic problems 10s of times a day,
is it no wonder we get fed up sometimes?

When you want to ask a question in a newsgroup, go find the rules (use
google groups and search the group for "FAQ"), put in some time reading
and understanding the FAQ. Follow the rules, and your visit here (or
any other newsgroup) will be good. Disobey the rules, and expect to be
beaten to death with a large pole!

Welcome to newsgroups.

Ben Pope
 
V

Victor Bazarov

Howard said:
[asking others here]

isn't there still a problem (even using std::string instead of char*) when
adding the object to the map? Doesn't it involve making a copy of the
StateType object? And because of the std::string member, doesn't that mean
that a copy-constructor for copying the string itself is going to be needed?
(Or does the default copying of the struct handle strings properly already?)
[..]

A struct that has a member of type 'std::string' need not have its own
copy constructor, the compiler-generated one should suffice.

V
 
H

Howard

Victor Bazarov said:
Howard said:
[asking others here]

isn't there still a problem (even using std::string instead of char*)
when adding the object to the map? Doesn't it involve making a copy of
the StateType object? And because of the std::string member, doesn't
that mean that a copy-constructor for copying the string itself is going
to be needed? (Or does the default copying of the struct handle strings
properly already?)
[..]

A struct that has a member of type 'std::string' need not have its own
copy constructor, the compiler-generated one should suffice.

Ok. Thanks, Victor.

Regards,
-Howard
 
B

Ben Pope

Howard said:
I may be mistaken, but...

[asking others here]

isn't there still a problem (even using std::string instead of char*) when
adding the object to the map? Doesn't it involve making a copy of the
StateType object? And because of the std::string member, doesn't that mean
that a copy-constructor for copying the string itself is going to be needed?
(Or does the default copying of the struct handle strings properly already?)

The default copy constructor calls the copy constructor of each
component. It's a shallow copy in that respect. It does not do a
memcpy. The problem comes when you have members that do not have the
correct copy semantics, such as pointers to objects that are supposed to
be owned by the object.

If every object implements the correct copy semantics, you never have to
write your own copy constructor (how cool is that?).

The only time you need your own copy constructor is when you are copying
objects that own other resources (such as a file). In this case though,
it often means that when it makes little sense to copy a member object,
it also makes little sense to copy the object owning that member, in
which case you make the copy constructor and assignment operator
private, and do not implement them.

Ben Pope
 
D

Daniel T.

"subaruwrx88011 said:
I posted this question earlier and made some changes. Due to the topic
getting off topic I decided to post another one.

[some code snipped]
std::string CCSI_EnviromentClass::getItemStateString(std::string item)
{
StateType state;

ItemStateMap::iterator map_iterator;

//Find the item name in the list
map_iterator = m_itemMap.find(item);

//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_string; //return string part of union
}
else
{
return NULL;
}

}

[other code snipped]
Can anyone tell me why this is seg faulting? That will greatly be
appreciated.

I don't know if it ever happens in your program, but if map_iterator
ever equals m_itemMap.end() that could be your problem. Try returning ""
instead of NULL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top