6 Replies - 291 Views - Last Post: 05 December 2012 - 01:26 PM Rate Topic: -----

#1 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 197
  • Joined: 21-April 09

Stack dump produced when trying to use transform.

Posted 05 December 2012 - 07:11 AM

I am trying to use the transform algorithm to take a list of string and create a list of map<string,string>.

To do this I have this function:
map<string,string> split_line_into_fields(const string& line);

Am I using the transform algorithm incorrectly?

list<map<string,string> > m_msg;

transform(msg.begin(), msg.end(), m_msg.begin(), split_line_into_fields);

map<string,string> m = *m_msg.begin();

for (map<string,string>::iterator it = m.begin(); it != m.end(); ++it) {
    cout << "K = ''" << (*it).first  << "''" << endl
         << "V = ''" << (*it).second << "''" << endl
         << endl;
}


If I handle the loop manually I have no problems:
list<map<string,string> > m_msg;

for (list<string>::iterator it = msg.begin(); it != msg.end(); ++it) {
    m_msg.push_back(split_line_into_fields(*it));
}

map<string,string> m = *m_msg.begin();

for (map<string,string>::iterator it = m.begin(); it != m.end(); ++it) {
    cout << "K = ''" << (*it).first  << "''" << endl
         << "V = ''" << (*it).second << "''" << endl
         << endl;
}


(Note: I'm still in early development faze, so don't get all worried I'm just outputting a single element)

This post has been edited by code_m: 05 December 2012 - 07:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stack dump produced when trying to use transform.

#2 vividexstance  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: Stack dump produced when trying to use transform.

Posted 05 December 2012 - 10:06 AM

Correct me if I'm wrong, but because std::map holds std:pairs, you will probably need to do this manually and not with transform. If you were dealing with std::vector, it would be different.
Was This Post Helpful? 0
  • +
  • -

#3 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 197
  • Joined: 21-April 09

Re: Stack dump produced when trying to use transform.

Posted 05 December 2012 - 11:14 AM

The conversion is from string to map<string,string>. I'm putting the entire map into the list, not a single element of the map. The std::pair is not part of the equation here (that is handled in the function, and to that point is my entire for loop).

My only idea is that list.begin() isn't an insertion iterator?

Or maybe I need to allocate space before calling transform (though it doesn't make much sense to do so)?
Was This Post Helpful? 0
  • +
  • -

#4 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 197
  • Joined: 21-April 09

Re: Stack dump produced when trying to use transform.

Posted 05 December 2012 - 11:24 AM

Update: I do need to allocate space :whatsthat:

This works fine:

list<map<string,string> > m_msg(msg.size());

transform(msg.begin(), msg.end(), m_msg.begin(), split_line_into_fields);

map<string,string> m = *m_msg.begin();

for (map<string,string>::iterator it = m.begin(); it != m.end(); ++it) {
    cout << "K = ''" << (*it).first  << "''" << endl
         << "V = ''" << (*it).second << "''" << endl
         << endl;
}


I really feel it's kinda silly to allocate space since I'm using list, that's a lot of pointers that are going to be created twice. Might stick with the for-loop.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

Re: Stack dump produced when trying to use transform.

Posted 05 December 2012 - 11:48 AM

The container you're loading must be large enough to take the load.

This should work.
list<map<string,string> > test(const list<string> &msg) {
	list<map<string,string> > m_msg(msg.size());
	transform(msg.begin(), msg.end(), m_msg.begin(), split_line_into_fields);
	return m_msg;
}



Edit: But, um, you already figured that out. :P

What's this about pointers?

This post has been edited by baavgai: 05 December 2012 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 197
  • Joined: 21-April 09

Re: Stack dump produced when trying to use transform.

Posted 05 December 2012 - 12:14 PM

View Postbaavgai, on 05 December 2012 - 01:48 PM, said:

What's this about pointers?


When you initial the list with a given size, the default constructor will be called for each node creation. Since the function's return data size (in memory) is significantly larger than the default object the default object will be destroyed and the data pointer of the node will be updated to the address of where the function created the data. With a push_back you avoid all of that default object non-sense. If I were using an array or vector, sure the allocation is no big deal as it would occur even if I used push_back, but with a list I'm adding unneeded operations to a fairly simple conversion.
Was This Post Helpful? 1
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

Re: Stack dump produced when trying to use transform.

Posted 05 December 2012 - 01:26 PM

Got it, pointers under the covers.

Sounds like you're using the std::list as intended. Like most C++ programmers, I tend to reach for vector unless strongly motivated to use something else.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1