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

problems understanding std::map

Hello!

I have following object relations:

Competition 1--* Category 1--* Course
1
|
|
*
Course

In competetition.h:
class Competition
{
public:

// type definitions
typedef std::string name_type;
typedef long date_type;
typedef long time_type;
typedef short storage_type;
typedef std::map<name_type, Course> Courses;
typedef std::map<name_type, Category> Categories;
...
// Constant definitions
static const storage_type BINARY_FILE = 1;
static const storage_type ASCII_FILE = 2;
static const storage_type DATABASE = 3;

...
void loadCourses(const name_type filename = "radat.lst",
const storage_type storage = ASCII_FILE);
...
private:
struct CompetitionImpl * m_CompRep;
...
}

I am trying to populate a Competition's Courses by reading a text
file.
The lodadCourses implementation in competition.C:

void CompetitionImpl::loadCoursesFromAscii(const name_type fileName) {

const char * fname = fileName.data();

// is usage of automatic variable correct here???
name_type name;
Course course;
Course::code_type code;
std::string line,codes;

std::ifstream infile(fname, std::ios::in);
PRE_(infile, std::ios_base::failure);
int state = 0;
std::string::size_type ind;
while(getline(infile, line)) {
switch (state) {
case 0:
name = line.substr(0, line.find(" "));
state = 1;
break;
case 1:
ind = 0;
course = Course(name);
for(codes = nextToken(line, ' ', &ind);
codes != "" && codes != "0";
codes = nextToken(line, ' ', &ind)) {
code = (short)atoi(codes.data());
course.addCheckPoint(code);
}
state = 2;
break;
case 2:
ind = 0;
for(codes = nextToken(line, ' ', &ind);
codes != "" && codes != "0";
codes = nextToken(line, ' ', &ind)) {
code = (short)atoi(codes.data());
course.addCheckPoint(code);
}
m_Courses[name] = course; // segfault from map here!
state = 0;
break;
default:
break;
}
//std::cout << line << "\nstate:" << state << std::endl;
}
//ifstream destructor takes care of closing file!

}
course.h:

class Course
{
public:

// type definitions
typedef std::string name_type;
typedef short code_type;
typedef std::vector<code_type> codes;
Course();

Course (name_type&);

Course& operator=(const Course&);
~Course();

bool operator==(const Course&) const;

name_type getName() const;
code_type getFirstCheckPoint() const;
code_type getCheckPoint() const;
void addCheckPoint(const code_type p_Code);

// test validity of object:
bool Invariant () const; // for debugging

private:
struct CourseImpl * m_CourseImpl;

};

course.C:
// Internal representation of Course class.

struct CourseImpl {

// typedefs
typedef Course::name_type name_type;
typedef Course::code_type code_type;
typedef Course::codes codes;

// members
name_type m_Name;
codes m_Codes;

// to test the validity of internal state:
bool Invariant() const; // express the valid state
void Post () const; // check the class invariant

CourseImpl(name_type&, codes&);
CourseImpl(codes&);

private:
CourseImpl (const CourseImpl&);
void operator = (const CourseImpl&);

};

// #define NDEBUG // default: debug is on
// while debugging, ignore exceptions and use assert
// Note. If NDEBUG is defined, all asserts are ignored.
//
// precondition and postcondition tests:
#ifndef NDEBUG // debug is on: test everything
#define PRE_(pre,E) assert (pre)
#define POST_() assert (this->Invariant ())
#else // for production code, only obligatory tests
#define PRE_(pre,Excpt) if(!(pre)) throw (Excpt)
#define POST_() /* empty */
#endif

// check correctness of internal state
inline bool CourseImpl::Invariant () const
{
if (this == 0) return false;
return true;
} // Invariant

// test the class invariant:
inline void CourseImpl::Post () const
{
POST_();
}

inline CourseImpl::CourseImpl(name_type& p_Name, codes& p_Codes):
m_Name(p_Name), m_Codes(p_Codes){

}

inline CourseImpl::CourseImpl(codes& p_Codes): m_Codes(p_Codes) {
}

/////////////////////////////////////////////
//
// Implementation of Course public interface
//
/////////////////////////////////////////////

Course::Course(Course::name_type& p_Name) {
codes c = codes(0);
m_CourseImpl = new CourseImpl(p_Name, c);
}

//Default constructor needed by map!

Course::Course() {
codes c = codes(0);
m_CourseImpl = new CourseImpl(c);
}

// Test state of Course instance
bool Course::Invariant () const {
return m_CourseImpl!= 0 && m_CourseImpl->Invariant ();
}
Course& Course::operator=(const Course& rhs) {
POST_();
if(&rhs != this) {
m_CourseImpl->m_Codes = rhs.m_CourseImpl->m_Codes;
m_CourseImpl->m_Name = rhs.m_CourseImpl->m_Name;
}
return *this;
}

Course::~Course() {
delete m_CourseImpl;

}
bool Course::operator==(const Course& rhs) const {
POST_();
return m_CourseImpl->m_Codes == rhs.m_CourseImpl->m_Codes;
}

Course::name_type Course::getName() const {
return m_CourseImpl->m_Name;
}

Course::code_type Course::getFirstCheckPoint() const {

//PRE_(m_CourseImpl->m_Codes.size() > 0, std::out_of_range);
if (m_CourseImpl->m_Codes.size() > 0)
return m_CourseImpl->m_Codes[0];
else
return short(0);
}

void Course::addCheckPoint(Course::code_type p_Code) {
m_CourseImpl->m_Codes.push_back(p_Code);
}

When running the main program:
int main() {

Competition comp;
comp.loadCourses();
...

I get a segfault and from gdb I can get following backtrace:
0x00428ce1 in std::string::~string() ()
at /usr/include/c++/3.3.1/bits/stl_algobase.h:402
402 copy(const _Tp* __first, const _Tp* __last, _Tp*
__result)
(gdb)
(gdb) bt
#0 0x00428ce1 in std::string::~string() ()
at /usr/include/c++/3.3.1/bits/stl_algobase.h:402
#1 0x00412851 in CourseImpl::~CourseImpl() (this=0xa047010) at
course.C:99
#2 0x00405b8f in Course::~Course() (this=0x22ea80) at course.C:99
#3 0x004316be in std::map<std::string, Course,
std::less<std::string>, std::allocator<std::pair<std::string const,
Course> > >::operator[](std::string const&)
(this=0xa043b80, __k=@0x22ed00)
at /usr/include/c++/3.3.1/bits/stl_map.h:319
#4 0x0040254b in CompetitionImpl::loadCoursesFromAscii(std::string) (
this=0xa043b70, fileName=0x22ed90) at competition.C:153
#5 0x00403738 in Competition::loadCourses(std::string, short)
(this=0x22eef8,
p_Name=0x22eed8, p_Type=2) at competition.C:317
#6 0x0040122c in main () at main.C:8

Run on cygwin and compiled with g++ 3.3.1 (cygming special)

I suspect using automatic variables is not the right way to set
objects into a std:map, or is the problem in Course class and its
implementation? Can anyone with more experience using C++/stl explain
what is the right way of doing this?
Any help and references to further reading appreciated.

--
Antti
Jul 22 '05 #1
1 1843
On 26 Jan 2004 02:57:51 -0800 in comp.lang.c++, ag*****@hotmail.com
(Antti Granqvist) was alleged to have written:
m_Courses[name] = course; // segfault from map here! I suspect using automatic variables is not the right way to set
objects into a std:map, or is the problem in Course class and its
implementation?
Yes, using automatic variables is perfectly ok. Of course std::map will
make its own copies of them to store, so it is crucial that the copy
constructor and assignment operator of class Course works properly.
In that regard, the line
struct CourseImpl * m_CourseImpl;


worries me. Using such a risque technique means you must watch your
allocated memory very carefully indeed in order to avoid memory leaks
and/or multiple copies trying to share the same m_CourseImpl.
Where is your copy constructor?

That input parsing logic with all the state switching is mind boggling.
Quite likely it's wrong and what you are trying to insert in the map is
not what you think. As a trace I'd add

cerr << "Inserting m_Courses[" << name << "] =" << course << '\n';

with suitable operator<< defined for Course.

Also possible that a stray write elsewhere trashed the map earlier, etc.

Jul 22 '05 #2

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

Similar topics

3
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); ...
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to...
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.