Segmentation fault

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

    Segmentation fault

    Hello,

    I've got the following piece of code:

    #include <string>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cmath>

    vector <vector <double data;
    for (long i=0;i<Count_Gri d_Easting-1;i++){
    data.push_back( vector <double>());
    for (long j=0;j<Count_Gri d_Northing-1;j++){
    data[i][j]=0;
    }
    }

    vector <vector <double data_occupied;
    for (long i=0;i<Count_Gri d_Easting-1;i++){
    data_occupied.p ush_back(vector <double>());
    for (long j=0;j<Count_Gri d_Northing-1;j++){
    data_occupied[i][j]=0;
    }
    }

    Every time I run it I get "Segmentati on Fault". What am I doing
    wrong, here, people???


    Thanks
    trxrse

  • Andre Kostur

    #2
    Re: Segmentation fault

    "trxrse" <Remus.SEPP@t-mobile.co.ukwro te in
    news:1170264252 .011112.10030@j 27g2000cwj.goog legroups.com:
    Hello,
    >
    I've got the following piece of code:
    >
    #include <string>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cmath>
    >
    vector <vector <double data;
    for (long i=0;i<Count_Gri d_Easting-1;i++){
    data.push_back( vector <double>());
    for (long j=0;j<Count_Gri d_Northing-1;j++){
    data[i][j]=0;
    data[i][j] is referring to an element in the vector that you just pushed
    onto data. That vector is still empty, thus you've accessed beyond the
    end of the container. You probably want to replace your loop over j
    with:

    data[i].resize(Count_G rid_Northing);

    }
    }
    >
    vector <vector <double data_occupied;
    for (long i=0;i<Count_Gri d_Easting-1;i++){
    data_occupied.p ush_back(vector <double>());
    for (long j=0;j<Count_Gri d_Northing-1;j++){
    data_occupied[i][j]=0;
    Same as above.
    }
    }
    >
    Every time I run it I get "Segmentati on Fault". What am I doing
    wrong, here, people???
    Accessing beyond the end of a container. Undefined Behaviour, which
    happens to be manifesting as a segfault.

    Comment

    • Rolf Magnus

      #3
      Re: Segmentation fault

      trxrse wrote:
      Hello,
      >
      I've got the following piece of code:
      >
      #include <string>
      #include <fstream>
      #include <cstdlib>
      #include <vector>
      #include <iostream>
      #include <sstream>
      #include <algorithm>
      #include <cmath>
      >
      vector <vector <double data;
      for (long i=0;i<Count_Gri d_Easting-1;i++){
      data.push_back( vector <double>());
      Simpler and quite a bit more efficient than repeated push_back() in the loop
      would be a:

      data.resize(Cou nt_Grid_Easting-1);

      before the loop.
      for (long j=0;j<Count_Gri d_Northing-1;j++){
      data[i][j]=0;
      Here is your problem. The inner vectors are all empty, i.e. have a size of
      0. In the outer loop, you did it right by using push_back(). Why not in the
      inner loop? The last line should be:

      data[i].push_back(0);

      Or again replace the for loop with a:

      data[i].resize(Count_G rid_Northing-1);

      which will also default-initialize all the elements.


      In total, you can replace the whole thing with:

      vector <vector <double data(Count_Grid _Easting-1,
      vector<double>( Count_Grid_Nort hing-1));

      Comment

      • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

        #4
        Re: Segmentation fault

        On 2007-01-31 18:24, trxrse wrote:
        Hello,
        >
        I've got the following piece of code:
        >
        #include <string>
        #include <fstream>
        #include <cstdlib>
        #include <vector>
        #include <iostream>
        #include <sstream>
        #include <algorithm>
        #include <cmath>
        >
        vector <vector <double data;
        for (long i=0;i<Count_Gri d_Easting-1;i++){
        data.push_back( vector <double>());
        Adds an empty vector to data.
        for (long j=0;j<Count_Gri d_Northing-1;j++){
        data[i][j]=0;
        Tries to access the j'th element in the empty vector.

        If you want to add a vector of size Count_Grid_East ing - 1 with all
        elements 0 you should use
        data.push_back( vector<double>( Count_Grid_East ing - 1, 0));
        instead.

        --
        Erik Wikström

        Comment

        • Jerry Coffin

          #5
          Re: Segmentation fault

          In article <1170264252.011 112.10030@j27g2 000cwj.googlegr oups.com>,
          Remus.SEPP@t-mobile.co.uk says...

          [ ... ]
          vector <vector <double data;
          for (long i=0;i<Count_Gri d_Easting-1;i++){
          data.push_back( vector <double>());
          for (long j=0;j<Count_Gri d_Northing-1;j++){
          data[i][j]=0;
          }
          }
          Perhaps something like this would work a bit better:

          vector<doublete mp(Count_Grid_N orthing-1);
          vector<vector<d oubledata(Count _Grid_Easting-1, temp);

          By default, the elements of temp will be default constructed, which
          means zero-filled in the case of arithmetic types. data is then
          constructed with each element a copy of temp, so we end up with a vector
          of vectors, all filled with zeros. The one shortcoming is that we're
          creating an extra vector (temp) just to initialize data. If that's a
          major problem, you could do something like this:

          vector<vector<d ouble data(Count_Grid _Easting-1);

          for (size_t i=0; i<data.size(); i++)
          data[i].resize(Count_G rid_Northing);

          --
          Later,
          Jerry.

          The universe is a figment of its own imagination.

          Comment

          • trxrse

            #6
            Re: Segmentation fault

            Thanks very much - you're really kind people. Indeed, this was the
            problem.

            Thanks
            trxrse


            On 1 Feb, 06:32, Jerry Coffin <jcof...@taeus. comwrote:
            In article <1170264252.011 112.10...@j27g2 000cwj.googlegr oups.com>,
            Remus.S...@t-mobile.co.uk says...
            >
            [ ... ]
            >
            vector <vector <double data;
            for (long i=0;i<Count_Gri d_Easting-1;i++){
            data.push_back( vector <double>());
            for (long j=0;j<Count_Gri d_Northing-1;j++){
            data[i][j]=0;
            }
            }
            >
            Perhaps something like this would work a bit better:
            >
            vector<doublete mp(Count_Grid_N orthing-1);
            vector<vector<d oubledata(Count _Grid_Easting-1, temp);
            >
            By default, the elements of temp will be default constructed, which
            means zero-filled in the case of arithmetic types. data is then
            constructed with each element a copy of temp, so we end up with a vector
            of vectors, all filled with zeros. The one shortcoming is that we're
            creating an extra vector (temp) just to initialize data. If that's a
            major problem, you could do something like this:
            >
            vector<vector<d ouble data(Count_Grid _Easting-1);
            >
            for (size_t i=0; i<data.size(); i++)
            data[i].resize(Count_G rid_Northing);
            >
            --
            Later,
            Jerry.
            >
            The universe is a figment of its own imagination.

            Comment

            • Default User

              #7
              Re: Segmentation fault - TPA

              trxrse wrote:
              Thanks very much - you're really kind people. Indeed, this was the
              problem.
              Please don't top-post. Your replies belong following or interspersed
              with properly trimmed quotes. See the majority of other posts in the
              newsgroup, or the group FAQ list:
              <http://www.parashift.c om/c++-faq-lite/how-to-post.html>

              Comment

              • Old Wolf

                #8
                Re: Segmentation fault - TPA

                On Feb 2, 7:33 am, "Default User" <defaultuse...@ yahoo.comwrote:
                trxrse wrote:
                Thanks very much - you're really kind people. Indeed, this was the
                problem.
                >
                Please don't top-post. Your replies belong following or interspersed
                with properly trimmed quotes. See the majority of other posts in the
                newsgroup, or the group FAQ list:
                <http://www.parashift.c om/c++-faq-lite/how-to-post.html>
                Top-posting was correct in this case. The OP's comments
                did not refer to any specific piece of the message he
                was replying to, and did not require any context to
                interpret.

                Comment

                • Kai-Uwe Bux

                  #9
                  Re: Segmentation fault - TPA

                  Old Wolf wrote:
                  On Feb 2, 7:33 am, "Default User" <defaultuse...@ yahoo.comwrote:
                  >trxrse wrote:
                  Thanks very much - you're really kind people. Indeed, this was the
                  problem.
                  >>
                  >Please don't top-post. Your replies belong following or interspersed
                  >with properly trimmed quotes. See the majority of other posts in the
                  >newsgroup, or the group FAQ list:
                  ><http://www.parashift.c om/c++-faq-lite/how-to-post.html>
                  >
                  Top-posting was correct in this case. The OP's comments
                  did not refer to any specific piece of the message he
                  was replying to, and did not require any context to
                  interpret.
                  Actually, the way I read it, the "this" in "Indeed, this was the problem"
                  refers to a specific piece of information, and the quoting style of the OP
                  turns the pronoun into a dangling reference.


                  Best

                  Kai-Uwe Bux

                  Comment

                  • Old Wolf

                    #10
                    Re: Segmentation fault - TPA

                    On Feb 5, 11:47 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
                    Old Wolf wrote:
                    On Feb 2, 7:33 am, "Default User" <defaultuse...@ yahoo.comwrote:
                    trxrse wrote:
                    Thanks very much - you're really kind people. Indeed, this was the
                    problem.
                    >
                    Please don't top-post. Your replies belong following or interspersed
                    with properly trimmed quotes. See the majority of other posts in the
                    newsgroup, or the group FAQ list:
                    <http://www.parashift.c om/c++-faq-lite/how-to-post.html>
                    >
                    Top-posting was correct in this case. The OP's comments
                    did not refer to any specific piece of the message he
                    was replying to, and did not require any context to
                    interpret.
                    >
                    Actually, the way I read it, the "this" in "Indeed, this was the problem"
                    refers to a specific piece of information, and the quoting style of the OP
                    turns the pronoun into a dangling reference.
                    Nevertheless, it is clear that the message is a thankyou
                    note, and 'this' refers to the problem that the OP had
                    and is now solved. It's not necessary to know what the
                    actual problem was, in order to understand the purpose
                    and content of the post.

                    Comment

                    • Default User

                      #11
                      Re: Segmentation fault - TPA

                      Old Wolf wrote:
                      On Feb 2, 7:33 am, "Default User" <defaultuse...@ yahoo.comwrote:
                      trxrse wrote:
                      Thanks very much - you're really kind people. Indeed, this was the
                      problem.
                      Please don't top-post. Your replies belong following or interspersed
                      with properly trimmed quotes. See the majority of other posts in the
                      newsgroup, or the group FAQ list:
                      <http://www.parashift.c om/c++-faq-lite/how-to-post.html>
                      >
                      Top-posting was correct in this case.
                      Impossible, as top-posting is never correct. If the quotes weren't
                      relevant to the reply, then they didn't belong at all. And of course,
                      you're wrong because the person was replying directly to something in
                      the quotes, i.e. the solution to the problem.

                      I try to put "- TPA" in the header when I post this message. If it
                      bothers you, consider filtering it.




                      Brian

                      Comment

                      Working...