473,225 Members | 1,379 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,225 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 1833
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.