Simple C++ program crash - why?

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

    Simple C++ program crash - why?

    Hello all,
    I'm a self-taught java dev trying to teach (yet again) teach myself the
    ins-n-outs of C++. I'm working on the problem from


    I haven't gotten far in figuring out the reason why my program crashes.
    I've tried on three different platforms (cygwin, opensolaris and linux)
    but can't squeeze a decent stack trace out of gdb (yes, using g++ -g -O0).

    It happens after cout (i.e the last thing my program does), and some
    stack traces I've got indicate a problem with ~Address - does this
    indicate some sort of destructor for a stack like there are in classes?

    Heres the source:
    #import <iostream>
    struct Address {
    std::string city;
    int zip;
    };

    struct Student {
    int id;
    bool isGrad;
    Address addr;
    };
    int numOfGrads (Student std[], int nstd);
    int main(int nargs, char *args[]) {
    Student students[1];
    students[0].id = 0;
    students[0].isGrad = true;
    students[0].addr.city = "Geelong";
    students[0].addr.zip = 3200;
    students[1].id = 1;
    students[1].isGrad = false;
    students[1].addr.city = "Werribee";
    students[1].addr.zip = 7331;
    int gr = numOfGrads(stud ents, 1);
    std::cout << "Number of students is: " << gr << std::endl;
    return 0;
    }
    int numOfGrads(Stud ent std[], int nstd) {
    int num = 0;
    int x;
    for (x=0; x<nstd; x++) {
    if (std[x].isGrad) {
    num++;
    }
    }
    return num;
    }
  • Jerry Coffin

    #2
    Re: Simple C++ program crash - why?

    In article <00e5bc3f$0$770 $c3e8da3@news.a straweb.com>,
    matt@mcbridemat t.dhs.org says...

    [ ... ]
    int main(int nargs, char *args[]) {
    Student students[1];
    This defines an array of _one_ and only one student.
    students[0].id = 0;
    [ ... ]
    students[1].id = 1;
    Here you're accessing not just one, but _two_ students in the array. The
    result is undefined behavior.

    My advice would be to ignore the fact that there IS such a thing as an
    array in C++. Using std::vector can virtually eliminate problems like
    this.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    Comment

    • Kai-Uwe Bux

      #3
      Re: Simple C++ program crash - why?

      Mathew McBride wrote:
      Hello all,
      I'm a self-taught java dev trying to teach (yet again) teach myself the
      ins-n-outs of C++. I'm working on the problem from
      >
      http://pages.cs.wisc.edu/~hasti/cs36...N.html#youTry3
      >
      I haven't gotten far in figuring out the reason why my program crashes.
      I've tried on three different platforms (cygwin, opensolaris and linux)
      but can't squeeze a decent stack trace out of gdb (yes, using g++ -g -O0).
      >
      It happens after cout (i.e the last thing my program does), and some
      stack traces I've got indicate a problem with ~Address - does this
      indicate some sort of destructor for a stack like there are in classes?
      >
      Heres the source:
      #import <iostream>
      #include is standard.
      #import is an extension.
      struct Address {
      std::string city;
      int zip;
      };
      >
      struct Student {
      int id;
      bool isGrad;
      Address addr;
      };
      int numOfGrads (Student std[], int nstd);
      int main(int nargs, char *args[]) {
      Student students[1];
      You have two students. Hence, you want:

      Student students [2];
      students[0].id = 0;
      students[0].isGrad = true;
      students[0].addr.city = "Geelong";
      students[0].addr.zip = 3200;
      students[1].id = 1;
      students[1].isGrad = false;
      students[1].addr.city = "Werribee";
      students[1].addr.zip = 7331;
      int gr = numOfGrads(stud ents, 1);
      std::cout << "Number of students is: " << gr << std::endl;
      return 0;
      }
      int numOfGrads(Stud ent std[], int nstd) {
      int num = 0;
      int x;
      for (x=0; x<nstd; x++) {
      if (std[x].isGrad) {
      num++;
      }
      }
      return num;
      }
      Suggestions:

      a) You want to ditch arrays for std::vector or std::deque.
      b) You want to make numOfGrads() an algorithm that works on iterators.
      c) You want to have a look at std::count_if() and boost::lamba.


      Best

      Kai-Uwe Bux

      Comment

      • Mathew McBride

        #4
        Re: Simple C++ program crash - why?

        *bangs head on wall, multiple times*
        yeah thanks, re #import, I've also done objc so I'm confusing myself.
        Thanks Jerry and Kai.
        Kai-Uwe Bux wrote:
        Mathew McBride wrote:
        >
        >Hello all,
        >I'm a self-taught java dev trying to teach (yet again) teach myself the
        >ins-n-outs of C++. I'm working on the problem from
        >>
        http://pages.cs.wisc.edu/~hasti/cs36...N.html#youTry3
        >I haven't gotten far in figuring out the reason why my program crashes.
        >I've tried on three different platforms (cygwin, opensolaris and linux)
        >but can't squeeze a decent stack trace out of gdb (yes, using g++ -g -O0).
        >>
        >It happens after cout (i.e the last thing my program does), and some
        >stack traces I've got indicate a problem with ~Address - does this
        >indicate some sort of destructor for a stack like there are in classes?
        >>
        >Heres the source:
        >#import <iostream>
        >
        #include is standard.
        #import is an extension.
        >
        >struct Address {
        >std::string city;
        >int zip;
        >};
        >>
        >struct Student {
        >int id;
        >bool isGrad;
        >Address addr;
        >};
        >int numOfGrads (Student std[], int nstd);
        >int main(int nargs, char *args[]) {
        >Student students[1];
        >
        You have two students. Hence, you want:
        >
        Student students [2];
        >
        >students[0].id = 0;
        >students[0].isGrad = true;
        >students[0].addr.city = "Geelong";
        >students[0].addr.zip = 3200;
        >students[1].id = 1;
        >students[1].isGrad = false;
        >students[1].addr.city = "Werribee";
        >students[1].addr.zip = 7331;
        >int gr = numOfGrads(stud ents, 1);
        >std::cout << "Number of students is: " << gr << std::endl;
        >return 0;
        >}
        >int numOfGrads(Stud ent std[], int nstd) {
        >int num = 0;
        >int x;
        >for (x=0; x<nstd; x++) {
        >if (std[x].isGrad) {
        >num++;
        >}
        >}
        >return num;
        >}
        >
        Suggestions:
        >
        a) You want to ditch arrays for std::vector or std::deque.
        b) You want to make numOfGrads() an algorithm that works on iterators.
        c) You want to have a look at std::count_if() and boost::lamba.
        >
        >
        Best
        >
        Kai-Uwe Bux

        Comment

        • Juha Nieminen

          #5
          Re: Simple C++ program crash - why?

          Jerry Coffin wrote:
          This defines an array of _one_ and only one student.
          >
          > students[0].id = 0;
          [ ... ]
          > students[1].id = 1;
          >
          Here you're accessing not just one, but _two_ students in the array. The
          result is undefined behavior.
          >
          My advice would be to ignore the fact that there IS such a thing as an
          array in C++. Using std::vector can virtually eliminate problems like
          this.
          Exactly how does std::vector prevent you from indexing out of bounds
          with the [] operator?

          Comment

          • Kai-Uwe Bux

            #6
            Re: Simple C++ program crash - why?

            Juha Nieminen wrote:
            Jerry Coffin wrote:
            >This defines an array of _one_ and only one student.
            >>
            >>students[0].id = 0;
            >[ ... ]
            >>students[1].id = 1;
            >>
            >Here you're accessing not just one, but _two_ students in the array. The
            >result is undefined behavior.
            >>
            >My advice would be to ignore the fact that there IS such a thing as an
            >array in C++. Using std::vector can virtually eliminate problems like
            >this.
            >
            Exactly how does std::vector prevent you from indexing out of bounds
            with the [] operator?
            Note that you are not paraphrasing the claim.

            It does not _prevent_ you from doing so. However, it _encourages_ the use of
            begin() and end(). Moreover, the original interface was something like

            int numOfGrads(Stud ent std[], int nstd)

            where it is up to the programmer to remember the size of the array
            correctly. With std::vector, you have the size() method and the interface
            would just be

            unsigned int numOfGrads( std::vector< Student const & s_vec );

            Also, as a quality of implementation issue, I came to expect an assert or
            something similar which I can turn on/off inside operator[] so that
            mistakes such as the one you point out are easily caught.


            But again, nothing _prevents_ you from running into undefined behavior.
            Nonetheless, the usual problems (as shown in, e.g., the original post) by
            and large disappear with the adoption of std::vector<and the idioms that
            come with it.


            Best

            Kai-Uwe Bux

            Comment

            • Jerry Coffin

              #7
              Re: Simple C++ program crash - why?

              In article <uenBk.29$fB.23 @read4.inet.fi> , nospam@thanks.i nvalid says...
              Jerry Coffin wrote:
              This defines an array of _one_ and only one student.
              students[0].id = 0;
              [ ... ]
              students[1].id = 1;
              Here you're accessing not just one, but _two_ students in the array. The
              result is undefined behavior.

              My advice would be to ignore the fact that there IS such a thing as an
              array in C++. Using std::vector can virtually eliminate problems like
              this.
              >
              Exactly how does std::vector prevent you from indexing out of bounds
              with the [] operator?
              By itself, it doesn't. Nonetheless, indexing out of bounds virtually
              disappears anyway. In this case, he defined an array of a given size,
              and then indexed into it. In the case of vector, you typically define it
              empty, and then use push_back to add items -- and it automatically
              allocates more memory as needed. Likewise, he passed a pointer and a
              size -- but, again, with vector you don't normally do that. Rather, you
              just pass the vector, which knows its size. Finally, with vector you can
              often avoid using indexing at all -- you use an algorithm, and pass the
              results from begin() and end() to the algorithm as the bounds on what
              it's going to work with. This, again, essentially removes the
              possibility of generating an index that's out of bounds to start with.

              --
              Later,
              Jerry.

              The universe is a figment of its own imagination.

              Comment

              • Ruben

                #8
                Re: Simple C++ program crash - why?

                On Sun, 21 Sep 2008 13:40:39 +1000, Mathew McBride wrote:
                Hello all,
                I'm a self-taught java dev trying to teach (yet again) teach myself the
                ins-n-outs of C++. I'm working on the problem from

                >
                I haven't gotten far in figuring out the reason why my program crashes.
                I've tried on three different platforms (cygwin, opensolaris and linux)
                but can't squeeze a decent stack trace out of gdb (yes, using g++ -g -O0).
                >
                It happens after cout (i.e the last thing my program does), and some stack
                traces I've got indicate a problem with ~Address - does this indicate some
                sort of destructor for a stack like there are in classes?
                >
                Heres the source:
                #import <iostream>
                struct Address {
                std::string city;
                int zip;
                };
                >
                struct Student {
                int id;
                bool isGrad;
                Address addr;
                };
                int numOfGrads (Student std[], int nstd); int main(int nargs, char
                *args[]) {
                Student students[1];

                This is a declaration of a students array with ONE element. It doesn't
                start with zero like indexing does.


                See:


                students[0].id = 0;
                students[0].isGrad = true;
                students[0].addr.city = "Geelong";
                students[0].addr.zip = 3200;
                students[1].id = 1;
                students[1].isGrad = false;
                students[1].addr.city = "Werribee";
                students[1].addr.zip = 7331;
                int gr = numOfGrads(stud ents, 1);
                std::cout << "Number of students is: " << gr << std::endl; return 0;
                }
                BTW - I'm not certain why one uses this kind of array with structs. The
                extension of C++ over C makes this superfilous

                declare a class Students and make the elements public members of the class.

                int numOfGrads(Stud ent
                std[], int nstd) {
                int num = 0;
                int x;
                for (x=0; x<nstd; x++) {
                if (std[x].isGrad) {
                num++;
                }
                }
                return num;
                }
                --
                http://www.mrbrklyn.com - Interesting Stuff
                http://www.nylxs.com - Leadership Development in Free Software

                So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

                http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

                "Yeah - I write Free Software...so SUE ME"

                "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

                "I'm an engineer. I choose the best tool for the job, politics be damned.<
                You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

                © Copyright for the Digital Millennium

                Comment

                Working...