473,398 Members | 2,404 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,398 software developers and data experts.

Seg Fault Help

I am using a map that takes an string as the key and a structure that
is being stored.

struct StateType
{
int i;
float f;
char* s;
}

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

ItemStateMap m_itemMap;

StateType state;
state.s = "HelloWorld";

m_itemMap["FirstPosition"] = state;
This is a really watered down version of what I am doing but this is
exactly what its doing in a nutshell. I am getting a segmentation fault
on m_itemMap["FirstPosition"] = state. Can anyone tell me what I am
doing wrong? I tried to use
m_itemMap.insert("FirstPosition", state) but that wouldn't compile.

Thanks in advance
subaruwrx88011

Feb 14 '06 #1
14 1430
subaruwrx88011 wrote:
I am using a map that takes an string as the key and a structure that
is being stored.

struct StateType
{
int i;
float f;
char* s;
}

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

ItemStateMap m_itemMap;

StateType state;
state.s = "HelloWorld";

m_itemMap["FirstPosition"] = state;
This is a really watered down version of what I am doing but this is
exactly what its doing in a nutshell. I am getting a segmentation fault
on m_itemMap["FirstPosition"] = state. Can anyone tell me what I am
doing wrong? I tried to use
m_itemMap.insert("FirstPosition", state) but that wouldn't compile.


I'll tell you what you are doing wrong. You're not listening, and not
learning. POST COMPILABLE CODE.

We cannot tell you what is wrong with a "watered down version" if the
watered down version does not exhibit the problem.

At the moment the problem is the last two lines; they exist at file
scope, they need to go inside main, or another function.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #2
subaruwrx88011 wrote:
I am using a map that takes an string as the key and a structure that
is being stored.

struct StateType
{
int i;
float f;
char* s;
}
A semicolon seems to be missing here...

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

ItemStateMap m_itemMap;

StateType state;
state.s = "HelloWorld";
This is a VERY BAD IDEA(tm). Your member is a pointer to 'char', and you
are assigning a pointer to a const char to it.

m_itemMap["FirstPosition"] = state;
This is a really watered down version of what I am doing but this is
exactly what its doing in a nutshell.
Too much water. Post _real_ code. Your fragment as posted doesn't even
compile. Always post code that can be copied, pasted into a file, and
passed to a compiler without any changes.
I am getting a segmentation fault
on m_itemMap["FirstPosition"] = state. Can anyone tell me what I am
doing wrong? I tried to use
m_itemMap.insert("FirstPosition", state) but that wouldn't compile.


Of course it won't. 'insert' that you're trying to use takes one argument
-- a pair. RTFM.

V
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #3

"subaruwrx88011" <su************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I am using a map that takes an string as the key and a structure that
is being stored.

struct StateType
{
int i;
float f;
char* s;
Prefer to use std::string over a raw char array for s.
}

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

ItemStateMap m_itemMap;

StateType state;
state.s = "HelloWorld";
This points the member s pointer to a local const char. No memory is
allocated specifically for s, and if this constant goes out of scope, then s
points to invalid memory. Either allocated an array for s dynamically and
use strcpy to fill it, or use std::string.

m_itemMap["FirstPosition"] = state;
I'm not positive if this is how to assign to a map, but even if it is, it's
going to make a copy of the state object. And unless you define a copy
construcor for StateType, then it's only going to do a "shallow" copy, which
means that the member s will point to the same const char array that state's
s member points to. You need a valid copy constructor which allocates
memory for s and copies the data into it. (Again, it would be easier to use
std::string.)


This is a really watered down version of what I am doing but this is
exactly what its doing in a nutshell. I am getting a segmentation fault
on m_itemMap["FirstPosition"] = state. Can anyone tell me what I am
doing wrong? I tried to use
m_itemMap.insert("FirstPosition", state) but that wouldn't compile.


(Sorry, I'm not up on maps. But I hope my other thoughts are helpful.)

-Howard

Feb 14 '06 #4
TB
subaruwrx88011 sade:
I am using a map that takes an string as the key and a structure that
is being stored.

struct StateType
{
int i;
float f;
char* s;
}
Missing semicolon.

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

ItemStateMap m_itemMap;

StateType state;
state.s = "HelloWorld";

m_itemMap["FirstPosition"] = state;

Why not provide a complete compilable code, instead of something
partial. Does this work?

#include <string>
#include <map>

struct StateType {
char const * d_cptr;
};

int main(int argc, char** argv)
{
std::map<std::string, StateType> m;
StateType state = {"HelloWorld"};
m["FirstPosition"] = state;
return 0;
}
This is a really watered down version of what I am doing but this is
exactly what its doing in a nutshell. I am getting a segmentation fault
on m_itemMap["FirstPosition"] = state. Can anyone tell me what I am
doing wrong? I tried to use
m_itemMap.insert("FirstPosition", state) but that wouldn't compile.


m_itemMap.insert( std::make_pair("FirstPosition", state) );

--
TB @ SWEDEN
Feb 14 '06 #5
Ben Pope wrote:
I'll tell you what you are doing wrong. You're not listening, and not
learning. POST COMPILABLE CODE.


If I've bundled you in with somebody else asking an unnervingly similar
question, and you are not, in fact CQ, then I apologise.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #6
Ok sorry guys I am really new at c++ and how this forum works.

Here is the real code.

class CCSI_EnviromentClass
{
private:

//Constructor
CCSI_EnviromentClass();
typedef struct StateType
{
int i;
char* s;
float f;
StateType() : i(-1), f(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(char* item);
float getItemStateFloat(char* item);
char* getItemStateString(char* item);

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

void print();
};

char* CCSI_EnviromentClass::getItemStateString(char* 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.s; //return string part of union
}
else
{
return NULL;
}
}

float CCSI_EnviromentClass::getItemStateFloat(char* 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.f; //return float part of union
}
else
{
return -1;
}
}

int CCSI_EnviromentClass::getItemStateInt(char* 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.i; //return float part of union
}
else
{
return -1;
}
}

int CCSI_EnviromentClass::setItemState(std::string p_item, char*
p_state)
{
StateType state;
state.s = p_state;
m_itemMap[p_item] = state;
return 0;
}

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

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

Hope this is better.

Feb 14 '06 #7
Howard wrote:
"subaruwrx88011" <su************@gmail.com> wrote [..]
StateType state;
state.s = "HelloWorld";

This points the member s pointer to a local const char.


No. It points the member 's' to a string literal.
No memory is
allocated specifically for s, and if this constant goes out of scope, then s
points to invalid memory.
A string literal _never_ goes "out of scope".
Either allocated an array for s dynamically and
use strcpy to fill it, or use std::string.


V
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #8
subaruwrx88011 wrote:
Ok sorry guys I am really new at c++ and how this forum works.

Here is the real code.

class CCSI_EnviromentClass
{
private:

//Constructor
CCSI_EnviromentClass();
typedef struct StateType
{
int i;
char* s;
float f;
StateType() : i(-1), f(1.0) {}
So, you initialise all except 's'. 's' is left _uninitialised_. If
a pointer is uninitialised, it contains _garbage_, it points _nowhere_.
Initialise it to 0, at least then you can _verify_ whether it has some
"real" value or "nothing".
};

typedef std::map<std::string, StateType> ItemStateMap;
ItemStateMap m_itemMap;
static CCSI_EnviromentClass *ms_instance;

public:
//Destructor
~CCSI_EnviromentClass();
The constructor is private, the destructor is public. Is there any
particular reason?

//Methods
static CCSI_EnviromentClass *instance();
int getItemStateInt(char* item);
float getItemStateFloat(char* item);
char* getItemStateString(char* item);
OK... The map is based on 'std::string'. You're passing bare pointers
around. Is there a particular reason?
int setItemState(std::string item, char* state);
int setItemState(std::string item, float state);
int setItemState(std::string item, int state);

void print();
};

char* CCSI_EnviromentClass::getItemStateString(char* 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.s; //return string part of union
It is better to do

return (*map_iterator).second.s;

here to avoid another search that would inevitably performed when you
use the indexing operator.
}
else
{
return NULL;
}
}

float CCSI_EnviromentClass::getItemStateFloat(char* 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.f; //return float part of union
Same note as above
}
else
{
return -1;
}
}

int CCSI_EnviromentClass::getItemStateInt(char* 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.i; //return float part of union
And here, too.
}
else
{
return -1;
}
}

int CCSI_EnviromentClass::setItemState(std::string p_item, char*
p_state)
{
StateType state;
state.s = p_state;
m_itemMap[p_item] = state;
return 0;
}

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

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

Hope this is better.


Actually, just a tiny bit better. But of course, since you never show
_who_ uses those functions (especially the suspect 'setItemState') and
_how_ they are used (what is passed to them), there is no way to tell,
again. The code is real alright. However, it's _incomplete_.

V
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #9

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:oT****************@newsread1.mlpsca01.us.to.v erio.net...
Howard wrote:
"subaruwrx88011" <su************@gmail.com> wrote [..]
StateType state;
state.s = "HelloWorld";

This points the member s pointer to a local const char.


No. It points the member 's' to a string literal.
No memory is
allocated specifically for s, and if this constant goes out of scope,
then s points to invalid memory.


A string literal _never_ goes "out of scope".


Ah, quite so. So is this a valid way to assign to a char*? I had thought
it was a valid initializer, but not a valid assignment.

-Howard

Feb 14 '06 #10
Howard wrote:

"subaruwrx88011" <su************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...

state.s = "HelloWorld";


This points the member s pointer to a local const char. No memory is
allocated specifically for s, and if this constant goes out of scope,
then s points to invalid memory.


This is not correct. The chars inside "" form a string literal, which
is not local at all. It will exist for the lifetime of the program.
There are good reasons for not doint that (Victor pointed out the main
one) but yours isn't correct.

Brian
Feb 14 '06 #11
Howard wrote:
[..] So is this a valid way to assign to a char*? I had thought
it was a valid initializer, but not a valid assignment.


Well, I don't remember. You're probably correct. Some compilers let it
through, however.

V
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #12
Ok i changed it to use std::string. Now in my application in need to
check to see if that string was stored. I was using
if(state.s != NULL), but that doesn't work now since state.s is
std::string. How do I go about checking this?

Feb 14 '06 #13

subaruwrx88011 wrote:
Ok i changed it to use std::string. Now in my application in need to
check to see if that string was stored. I was using
if(state.s != NULL), but that doesn't work now since state.s is
std::string. How do I go about checking this?


You may need a variable to tell you such is the case. Or you could use
std::string* and do the same thing. Or, if "" is not a valid value,
you could check that state.s.size() > 0.

Feb 14 '06 #14
Victor Bazarov wrote:
Howard wrote:
[..] So is this a valid way to assign to a char*? I had thought
it was a valid initializer, but not a valid assignment.


Well, I don't remember. You're probably correct. Some compilers let
it through, however.


It's valid, but deprecated (and a very BAD idea).

From the standard:

4.2 Array-to-pointer conversion

2 A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type "pointer to char"; a wide string literal
can be converted to an rvalue of type "pointer to wchar_t". In either
case, the result is a pointer to the first element of the array. This
conversion is considered only when there is an explicit appropriate
pointer target type, and not when there is a general need to convert
from an lvalue to an rvalue. [Note: this conversion is deprecated. See
Annex D. ]

Annex D:

D.4 Implicit conversion from const strings
1 The implicit conversion from const to non-const qualification for
string literals (4.2) is deprecated.

Brian
Feb 14 '06 #15

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

Similar topics

16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
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...
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...
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! ...
0
by: Nol | last post by:
Hi all, My webservice throws an exception, which is translated into a soap Fault in the soap message body. See below for the actual message format as it is send by the server. On the (dotNet)...
4
by: Nol de Wit | last post by:
(this is a re-post of a message I've posted last friday, but now from my MSDN subscription account, hoping MSDN support will pick this up...) Hi all, My webservice throws an exception, which...
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...
14
by: Matt | last post by:
Hello. I'm after some general advice. At the moment i have some code that should run a FOR loop a set number of times. Each time the structure stamps is read in as follows: for( i = 0 ; i <...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.