help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bizarre backtrace when trying to initialize std::map with std::pair


From: red floyd
Subject: Re: bizarre backtrace when trying to initialize std::map with std::pair array
Date: Wed, 27 Jun 2007 12:09:54 -0700
User-agent: Thunderbird 2.0.0.4 (Windows/20070604)

Benjamin Collins wrote:
I'm trying to initialize a std::map with an array of std::pair, but it
causes a sigsegv.  I'm using GCC 3.4.6 on RHEL 4.4 x86_64.

My gdb backtrace, a snippet of stl_map.h, and my program are included
below.  For better formatted display, see http://paste.collinslabs.com/?show=30.
Notice in particular the values of the parameters to the map
constructor and the insert_unique() function in frames #3 and #2,
respectively.  Why is __first in the map constructor one thing, and
__first in the insert_unique function another?

   1.
      /* map.cpp */
   2.
      #include <iostream>
   3.
      #include <map>
   4.
      #include <string>
   5.
      #include <utility>
   6.
      #include <vector>
   7.

   8.
      namespace {
   9.
        void f() {}
  10.
        void g() {}
  11.

  12.
        typedef std::pair<std::string, void (*)()> mypair_t;
  13.

  14.
        mypair_t pairs[] =
  15.
          {
  16.
            mypair_t("f", &f),
  17.
            mypair_t("g", &g)
  18.
          };
  19.
      }
  20.

  21.
      int main()
  22.
      {
  23.
        std::map<std::string, void (*)()> mymap(pairs, pairs
+sizeof(pairs));
  24.
      }
  25.

  26.
You error is on 23. sizeof(pairs) is much larger than the number of entries, so you're running off the end of pairs[]. You either need to use sizeof(pairs)/sizeof(mypair_t), or since it's a run-time value:

template<typename T, int N>
inline int array_size(const T(&)[N])
{
    return N;
}

int main()
{
    std::map<std::string, void(*)()> mymap(pairs, array_size(pairs));

}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]