std::map<int,std::set<std::string> > Wrong? (Segmentation fault.)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Jansson

    std::map<int,std::set<std::string> > Wrong? (Segmentation fault.)

    Hello,

    I have the following code:

    std::map<int,st d::set<std::str ing> > k;
    k[0]="1234567890 ";
    k[1]="2345678901 ";
    //...
    std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > k)
    throw(std::runt ime_error)
    {
    std::map<int,st d::set<std::str ing> >::const_iterat or i;
    i=k.find(0);
    if(i==k.end())
    throw std::runtime_er ror("No zero in k.");
    return i->second;
    }

    Compilation of this code goes well, but I have the following problem while
    executing this in my implementation: "Segmentati on fault". I have
    pin-pointed the problem to be the last return statement. When I ignore
    using find(int) and instead loops over the map with the following code,
    everyting goes fine.

    std::set<std::s tring> strings;
    for(i=k.begin() ;i!=k.end();i++ )
    {
    if(k->first==0)
    strings=k->second;
    }
    return strings;

    Anybody know what is going on here?

    Regards,
    Peter Jansson

  • Ivan Vecerina

    #2
    Re: std::map&lt;int ,std::set&lt;st d::string&gt; &gt; Wrong? (Segmentation fault.)

    "Peter Jansson" <webmaster@jans son.net> wrote in message
    news:Pine.NEB.4 .61.05031408505 20.23649@ukato. freeshell.org.. .[color=blue]
    > Hello,
    >
    > I have the following code:
    >
    > std::map<int,st d::set<std::str ing> > k;
    > k[0]="1234567890 ";
    > k[1]="2345678901 ";[/color]
    The above two lines are incorrect.
    Maybe you meant:
    k[0].insert("123456 7890");
    k[1].insert("234567 8901");
    [color=blue]
    > //...
    > std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > k)[/color]

    NB: you probably want to pass the k parameter by const reference:
    std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > const&
    k)
    BTW: a typedef of two would probably make sense instead of writing
    map<int,set<str ing> > repeatedly...
    [color=blue]
    > throw(std::runt ime_error)
    > {
    > std::map<int,st d::set<std::str ing> >::const_iterat or i;
    > i=k.find(0);
    > if(i==k.end())
    > throw std::runtime_er ror("No zero in k.");
    > return i->second;
    > }[/color]

    The above code seems ok, and I don't see a problem in it.
    Maybe you could post a complete working sample?


    Regards,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


    Comment

    • Kristo

      #3
      Re: std::map&lt;int ,std::set&lt;st d::string&gt; &gt; Wrong? (Segmentation fault.)

      On 3/14/2005 4:33 AM, Ivan Vecerina wrote:[color=blue]
      > "Peter Jansson" <webmaster@jans son.net> wrote in message
      > news:Pine.NEB.4 .61.05031408505 20.23649@ukato. freeshell.org.. .
      >[color=green]
      >>Hello,
      >>
      >>I have the following code:
      >>
      >>std::map<int, std::set<std::s tring> > k;
      >>k[0]="1234567890 ";
      >>k[1]="2345678901 ";[/color]
      >
      > The above two lines are incorrect.
      > Maybe you meant:
      > k[0].insert("123456 7890");
      > k[1].insert("234567 8901");[/color]

      What's wrong with them? If an object with key x doesn't exist,
      operator[] will create a default object (in this case a string) to which
      a value (e.g., "1234567890 ") can be safely assigned.

      Kristo

      Comment

      • Jeff Schwab

        #4
        Re: std::map&lt;int ,std::set&lt;st d::string&gt; &gt; Wrong? (Segmentation fault.)

        Kristo wrote:[color=blue]
        > On 3/14/2005 4:33 AM, Ivan Vecerina wrote:
        >[color=green]
        >> "Peter Jansson" <webmaster@jans son.net> wrote in message
        >> news:Pine.NEB.4 .61.05031408505 20.23649@ukato. freeshell.org.. .
        >>[color=darkred]
        >>> Hello,
        >>>
        >>> I have the following code:
        >>>
        >>> std::map<int,st d::set<std::str ing> > k;
        >>> k[0]="1234567890 ";
        >>> k[1]="2345678901 ";[/color]
        >>
        >>
        >> The above two lines are incorrect.
        >> Maybe you meant:
        >> k[0].insert("123456 7890");
        >> k[1].insert("234567 8901");[/color]
        >
        >
        > What's wrong with them? If an object with key x doesn't exist,
        > operator[] will create a default object (in this case a string)[/color]

        No, in this case a std::set<std::s tring>. Look carefully at the
        declaration of k.
        [color=blue]
        > to which
        > a value (e.g., "1234567890 ") can be safely assigned.
        >
        > Kristo[/color]

        Comment

        • Peter Jansson

          #5
          Re: std::map&lt;int ,std::set&lt;st d::string&gt; &gt; Wrong? (Segmentation fault.)


          On Mon, 14 Mar 2005, Ivan Vecerina wrote:
          [color=blue]
          > The above two lines are incorrect.
          > Maybe you meant:
          > k[0].insert("123456 7890");
          > k[1].insert("234567 8901");[/color]

          Yes, that is what I meant.
          [color=blue][color=green]
          >> std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > k)
          >> throw(std::runt ime_error)
          >> {
          >> std::map<int,st d::set<std::str ing> >::const_iterat or i;
          >> i=k.find(0);
          >> if(i==k.end())
          >> throw std::runtime_er ror("No zero in k.");
          >> return i->second;
          >> }[/color]
          >
          > The above code seems ok, and I don't see a problem in it.
          > Maybe you could post a complete working sample?
          >[/color]

          Well, the non-working code is the myMethod method above (non-working since
          it fails while executing, it compiles fine). Below, is the code that
          works.

          std::set<std::s tring> myMethod_which_ works(std::map< int,std::set<st d::string> > k)
          throw(std::runt ime_error)
          {
          std::map<int,st d::set<std::str ing> >::const_iterat or i;
          std::set<std::s tring> strings;
          bool haveNotFoundZer o=true;
          for(i=k.begin() ;i!=k.end() && haveNotFoundZer o;k++)
          {
          if(i->first==0)
          {
          haveNotFoundZer o=false;
          strings=i->second;
          }
          }
          if(haveNotFound Zero)
          throw std::runtime_er ror("No zero in k.");
          return strings;
          }

          I thought the method find should be more transparent and perhaps even
          optimized for sorted maps but it did not work as already mentioned. I
          would be grateful for any suggestion/hint on what is wrong.

          Regards,
          Peter Jansson

          Comment

          • Ivan Vecerina

            #6
            Re: std::map&lt;int ,std::set&lt;st d::string&gt; &gt; Wrong? (Segmentation fault.)

            "Peter Jansson" <webmaster@jans son.net> wrote in message
            news:Pine.NEB.4 .61.05031518271 70.3337@norge.f reeshell.org...[color=blue]
            > On Mon, 14 Mar 2005, Ivan Vecerina wrote:[color=green]
            >> The above code seems ok, and I don't see a problem in it.
            >> Maybe you could post a complete working sample?
            >>[/color]
            >
            > Well, the non-working code is the myMethod method above (non-working since
            > it fails while executing, it compiles fine). Below, is the code that
            > works.[/color]
            Again, your usage of the map::find member function seemed correct,
            but a problem somewhere else in your code could not be excluded.
            And as requested above, *you* should post a minimum code sample
            that compiles and reproduces the problem you see.
            Something like:

            #include <iostream>
            #include <map>
            #include <set>
            #include <string>

            std::set<std::s tring>
            myMethod(std::m ap<int,std::set <std::string> > const& k)
            throw(std::runt ime_error)
            {
            std::map<int,st d::set<std::str ing> >::const_iterat or i = k.find(0);
            if(i==k.end())
            throw std::runtime_er ror("No zero in k.");
            return i->second;
            }

            int main()
            {
            std::map<int,st d::set<std::str ing> > k;
            k[0].insert("123456 7890");
            k[1].insert("234567 8901");

            std::set<std::s tring> set = myMethod(k); // does not throw
            std::cout << *set.begin(); // prints "1234567890 "
            }
            [color=blue]
            > I thought the method find should be more transparent and perhaps even
            > optimized for sorted maps but it did not work as already mentioned.[/color]
            map::find is transparent and optimized, and does work.
            You shouldn't persuade yourself otherwise before writing a complete
            minimum sample that compiles and illustrates the issue you observe.
            [color=blue]
            > I would be grateful for any suggestion/hint on what is wrong.[/color]
            The above code shall work, and does on the platform I use.
            If it fails on your system, this would be a bug in the implementation
            you use, and you should seek support from your vendor or a platform-
            specific forum (and use the code sample as a bug report).


            Ivan
            --
            http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


            Comment

            Working...