Seg Fault帮助 [英] Seg Fault Help
问题描述
我正在使用一个以字符串作为键的地图和一个存储
的结构。
struct StateType
{
int i;
浮动f;
char * s;
}
typedef std :: map< std :: string,StateType> ItemStateMap;
ItemStateMap m_itemMap;
StateType状态;
state.s =" HelloWorld" ;;
m_itemMap [" FirstPosition"] = state;
这是我正在做的一个真正淡化的版本,但这是
正是它的确如此。我在m_itemMap [" FirstPosition"] = state上遇到了分段错误
。谁能告诉我我是什么?b $ b做错了什么?我试图使用
m_itemMap.insert(FirstPosition,状态),但这不会编译。
提前谢谢
subaruwrx88011
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
推荐答案
subaruwrx88011写道:
subaruwrx88011 wrote:
我使用的是一个带字符串的地图作为存储
的关键和结构。
struct StateType
{
int i;
浮动f;
char * s;
}
typedef std :: map< std :: string,StateType> ItemStateMap;
ItemStateMap m_itemMap;
StateType状态;
state.s =" HelloWorld";
m_itemMap [" FirstPosition" ;] =状态;
这是我正在做的一个真正淡化的版本,但这简直就是它的作用。我在m_itemMap [" FirstPosition"] = state上遇到了分段错误。谁能告诉我我做错了什么?我试图使用
m_itemMap.insert(" FirstPosition",state)但是不会编译。
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.
我会告诉你什么你做错了。你不是在听,而不是学习b $ b。可编辑后代码。
我们无法告诉你淡化版本有什么问题。如果
淡化版本没有出现问题。
目前问题是最后两行;它们存在于文件
范围内,它们需要进入主要或其他功能。
Ben Pope
-
我不只是一个数字。对于很多人来说,我被称为字符串...
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...
subaruwrx88011写道:
subaruwrx88011 wrote:
我使用的字符串为存储的关键和结构。
struct StateType
{
int i;
浮动f;
char * s ;
}
这里似乎缺少一个分号......
typedef std :: map< std :: string,StateType> ItemStateMap;
ItemStateMap m_itemMap;
StateType状态;
state.s =" HelloWorld" ;;
这是一个非常糟糕的想法(tm)。你的成员是一个指向''char''的指针,你
正在为它指定一个指向const char的指针。
m_itemMap [" FirstPosition"] = state ;
这是我正在做的一个真正淡化的版本,但这简直就是它的作用。
水太多了。发布_real_代码。您发布的片段甚至不会编译。总是发布可以复制,粘贴到文件中的代码,并且
传递给编译器而不做任何更改。
我在m_itemMap上遇到分段错误
[" FirstPosition"] =州。谁能告诉我我做错了什么?我试图使用
m_itemMap.insert(" FirstPosition",state)但是不会编译。
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.
当然不会。你试图使用的''insert''需要一个参数
- 一对。 RTFM。
V
-
请在邮寄回复时从我的地址中删除资金
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
" subaruwrx88011" <苏************ @ gmail.com>在消息中写道
news:11 ********************* @ g43g2000cwa.googlegro ups.com ...
"subaruwrx88011" <su************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
我正在使用一个以字符串作为键的地图和一个存储
的结构。
struct StateType
{
int i;
float f;
char * s;
更喜欢在s的原始字符数组上使用std :: string。
}
typedef std :: map< std :: string,StateType> ItemStateMap;
ItemStateMap m_itemMap;
StateType状态;
state.s =" HelloWorld" ;;
这指向成员的指向本地const char的指针。没有内存是专门为s分配的
,如果这个常量超出范围,那么
指向无效内存。要么动态地为s分配一个数组,要么使用strcpy来填充它,或者使用std :: string。
m_itemMap [" FirstPosition"] = state;
如果这是如何分配给地图的话,我不是肯定的,但即使是这样,也是如此,这是'b $ b状态对象的副本。除非你为StateType定义一个副本
construcor,否则它只会做一个浅的复制,其中
表示成员s将指向同样的const char数组,该数组表示'
s成员指向的。您需要一个有效的复制构造函数,为s分配
内存并将数据复制到其中。 (同样,使用
std :: string会更容易。)
这是我正在做的一个真正淡化的版本,但这是
确切地说它的作用简直就是这样。我在m_itemMap [" FirstPosition"] = state上遇到了分段错误。谁能告诉我我做错了什么?我试图使用
m_itemMap.insert(" FirstPosition",state)但是无法编译。
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.
(抱歉,我是不要在地图上。但我希望我的其他想法都有帮助。)
-Howard
(Sorry, I''m not up on maps. But I hope my other thoughts are helpful.)
-Howard
这篇关于Seg Fault帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!