473,394 Members | 1,965 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,394 software developers and data experts.

Map Seg Fault

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::stri ng 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::strin g 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

Feb 14 '06 #1
17 1654
subaruwrx88011 wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #2
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.

Feb 14 '06 #3

subaruwrx88011 wrote:
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.

Feb 14 '06 #4
subaruwrx88011 wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #5
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

Feb 14 '06 #6
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.

Feb 14 '06 #7
subaruwrx88011 wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #8
subaruwrx88011 wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #9
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.

Feb 14 '06 #10
subaruwrx88011 wrote:
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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Feb 14 '06 #11

Default User wrote:
subaruwrx88011 wrote:
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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

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

Feb 14 '06 #12

"subaruwrx88011" <su************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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


Feb 14 '06 #13
subaruwrx88011 wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #14
Howard wrote:
[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
Feb 14 '06 #15

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Zu****************@newsread1.mlpsca01.us.to.v erio.net...
Howard wrote:
[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
Feb 14 '06 #16
Howard wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #17
In article <11*********************@o13g2000cwo.googlegroups. com>,
"subaruwrx88011" <su************@gmail.com> wrote:
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::stri ng 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.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 15 '06 #18

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

Similar topics

3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
3
by: Moshe Kravchik | last post by:
Hi! We have a Web Service written in ATL Server and a client written in Java using Axis. When something goes wrong on the server side, it returns an HRESULT of the error which is translated into...
0
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
0
by: Equinex | last post by:
Hi, I am trying to call a Web Service using Php and Soap. Why does the following Php 5 code return? try { //$ExchangeLoginResult = $ExchangeClient->Login(array("request" =>
3
by: =?Utf-8?B?TWFucHJlZXQgU3VzaGls?= | last post by:
I am having a Webservice within which i am throwing SOAP Exceptions and therefore whenever something wrong happens a SOAP fault comes up in the response - see below: <?xml version="1.0"...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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...

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.