C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Seg Fault Help

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
subaruwrx88011
Guest





PostPosted: Wed Feb 15, 2006 2:06 pm    Post subject: Seg Fault Help Reply with quote



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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ralf Fassel
Guest





PostPosted: Wed Feb 15, 2006 6:06 pm    Post subject: Re: Seg Fault Help Reply with quote



* "subaruwrx88011" <subaruwrx88011 (AT) gmail (DOT) com>
| 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

Use a debugger to find the cause of that segmentation fault.

Or provide a compilable example which reproduces the problem. The
code you show does not provide a compilable example, and if I turn it
into one, it runs w/o segfault.

R'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Paul Floyd
Guest





PostPosted: Thu Feb 16, 2006 2:06 am    Post subject: Re: Seg Fault Help Reply with quote



On 15 Feb 2006 08:26:45 -0500, subaruwrx88011 <subaruwrx88011 (AT) gmail (DOT) com> wrote:
Quote:
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 class with a pointer but without a copy assignment operator. Probably
not a good idea, and a fairly likely cause of your problem;

A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Andrea Griffini
Guest





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: Seg Fault Help Reply with quote

Paul Floyd ha scritto:

Quote:
A class with a pointer but without a copy assignment operator. Probably
not a good idea, and a fairly likely cause of your problem;

May be you're right in the specific (i think the example has been
"watered down" too much) but telling that a class with a pointer but
without a copy constructor is a likely source of problems seems to me
too much. Something nicer that's easy to remember is the rule of "the
three amigos" (as named in "C++ FAQs"): if your class has ANY of
destructor, assignment operator or copy constructor then probably it
needs indeed ALL three of them. This rule covers cases where the
indirect ownership is not based on a pointer (but for example on an
handle, or indirectly on registration) and avoids false positives on
classes that logically point out without owenership.
In the shown case there is no destructor, no copy constructor and no
assignment operator, so I don't know why you thought that there was
ownership of the string involved (indeed I think that the destructor
was lost in the simplification process before posting).
Surely in general at least a line of comment saying that no explicit
copy constructor is required because the default generated one is OK
would be nice, at least to inform a future reader that it wasn't
something that was simply forgot.

Andrea


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Allan W
Guest





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: Seg Fault Help Reply with quote

Quote:
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;
}

Paul Floyd wrote:
Quote:
A class with a pointer but without a copy assignment operator. Probably
not a good idea, and a fairly likely cause of your problem;

"Fairly likely," okay. But not definitely. The OP didn't explain what
is
being done with this pointer... But consider the fact that struct
StateType doesn't have a destructor, either. So there's no danger of
double-delete.

It could be that s only ever points to string literals, which should
NEVER
be deleted.

Any undefined behavior here? Note that there's nothing being
explicitly created or deleted from heap...

#include <iostream>
#include <map>
#include <ostream>
#include <string>

struct StateType {
int i;
float f;
char* s;
};
typedef std::map<std::string, StateType> ItemStateMap;

int main() {
ItemStateMap m_itemMap;
StateType state;
state.i = 1;
state.f = 123.4f;
state.s = "HelloWorld";
m_itemMap["FirstPosition"] = state;

state.i = 2;
state.f = 246.8f;
state.s = "GoodbyeWorld";
m_itemMap["LastPosition"] = state;

state = m_itemMap["FirstPosition"];
std::cout << "First Position: "
<< state.i << ' '
<< state.f << ' '
<< state.s << std::endl;

state = m_itemMap["LastPosition"];
std::cout << "Last Position: "
<< state.i << ' '
<< state.f << ' '
<< state.s << std::endl;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
eden
Guest





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: Seg Fault Help Reply with quote

subaruwrx88011 wrote:
Quote:
StateType state;
state.s = "HelloWorld";


This is the problem. You didn't allocate a memory for that pointer. You
can't just assign a value to it.

Why you don't use std::string also for the s? Like:

struct StateType
{
int i;
float f;
std::string s;
}

Goran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sat Feb 18, 2006 2:06 am    Post subject: Re: Seg Fault Help Reply with quote

In article <slrndv78dd.ot.root (AT) bisanne (DOT) netpratique.fr>, Paul Floyd
<root (AT) 127 (DOT) 0.0.1> writes
Quote:
On 15 Feb 2006 08:26:45 -0500, subaruwrx88011
subaruwrx88011 (AT) gmail (DOT) com> 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 class with a pointer but without a copy assignment operator.
Probably
not a good idea, and a fairly likely cause of your problem;

I doubt it. We need to distinguish between pointers that own a resource
(which need to handled with care and usually some form of smart-pointer
is part of the solution) and pointers that just reference a resource. As
the above struct lacks any member functions it would seem that s is
likely to be of the latter type. If it isn't because s is used to hang
on to a dynamically created array of char then there is a lot more wrong
with the C++ design which would have to cope with exceptions (that is th
reason that the C design solution is inappropriate in C++)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/
youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Mon Feb 20, 2006 11:06 am    Post subject: Re: Seg Fault Help Reply with quote

eden wrote:
Quote:
subaruwrx88011 wrote:
StateType state;
state.s = "HelloWorld";

This is the problem. You didn't allocate a memory for that
pointer. You can't just assign a value to it.

Sure he can, as long as the value is a pointer (and the compiler
won't allow any other type). In this case, the implicit
conversion char const[] to char const* is taking place, and
there is no problem.

What he can't do, of course, is dereference the pointer before
assigning something to it.

Quote:
Why you don't use std::string also for the s? Like:

struct StateType
{
int i;
float f;
std::string s;
}

While generally a good idea, there are exceptions. In cases
where all of the string values will be literals, for example, I
still use char const*. And when order of initialization issues
raise their head, using primitive types has definite advantages.
(Not knowing what he is doing with StateType, I cannot say
whether either of these issues affect this class -- and except
for the order of initialization issues, std::string is never
wrong, and usually a superior choice.)

At any rate, there is no problem in the code he actually posted.
There are two possible explinations for his problem:

-- He simplified too much. If the string in question isn't
really a string literal, but e.g. allocated on the heap, and
freed in the destructor, then the lack of an explicit copy
constructor would be a real problem.

-- He has a problem elsewhere, which only shows up at this
place. Errors which corrupt the heap have a nasty
propensity of not being immediatly apparent, but causing the
program to crash on some later, unrelated allocation or
free (and of course, creating a new element in a map
requires allocation on the part of the map).

The usual solution in a professional environment for the latter
problem is to run Purify on the program. As far as I know,
however, Purify has no "amateur" ou "student" licenses, and it's
normal license is priced outside the bounds of a student or an
amateur, so this option isn't normally available unless your
company is paying.

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
eden
Guest





PostPosted: Mon Feb 20, 2006 1:06 pm    Post subject: Re: Seg Fault Help Reply with quote

kanze wrote:
Quote:
eden wrote:
subaruwrx88011 wrote:
StateType state;
state.s = "HelloWorld";

This is the problem. You didn't allocate a memory for that
pointer. You can't just assign a value to it.

Sure he can, as long as the value is a pointer (and the compiler
won't allow any other type). In this case, the implicit
conversion char const[] to char const* is taking place, and
there is no problem.

What he can't do, of course, is dereference the pointer before
assigning something to it.


Well, I must admit I was too fast in my conclusion that he can't
assign string literals to the pointer. I was assuming that he is using
some char arrays on the stack that could be out of scope later when the
map is referenced, but of course, this is assuming too much.

goran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.