3 Replies - 376 Views - Last Post: 03 September 2019 - 08:51 PM Rate Topic: -----

#1 adamchalkley   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 17-July 15

program not terminating gracefully

Posted 03 September 2019 - 01:51 PM

Hi guys,

I'm writing a custom container(just for practice), the program seems to exit ungracefully, I don't know if an exception is being thrown, it doesn't seem like it, but when adding more than 3 items to my container the program terminates ungracefully BUT the strange thing is it doesn't crash, it will print out all the debugging cout statements here1,here2,here3,here4 six times and it will even print more if I choose to print the size after I add 6 items.

I thought possibly it was because I was adding temp strings or rvalue strings but I decided to add lvalue strings but nonetheless it still terminates ungracefully

also it doesn't seem to be the sorting algorithm, as it will still terminate ungracefully when mySort is commented out

this has never happened to me before, and to me the code looks fine but yet this is happening, I wonder what I am doing wrong?

this is what I am getting vv

Process returned -1073741819 (0xC0000005) execution time : 1.934 s


#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

template<class T>
void mySort(T* items,int size)
{
    bool fixed = false;

    while(!fixed)
    {
        fixed = true;

        for(int i = 0; i < size; i++)
        {
            if(items[i+1] < items[i])
            {
                T temp;
                temp = items[i];
                items[i] = items[i+1];
                items[i+1] = temp;
                fixed = false;
            }
        }
    }
}

template<class T>
class Set{

   private:
       int currentSize;
       T arr[];

   public:
       static const int MAX_SIZE = 10;
       Set(){
           currentSize = 0;
       }


       bool isEmpty(){

         return currentSize == 0;
       }

       bool add(T item){

          cout << "here" << endl;

          if(currentSize == MAX_SIZE-1)
            return false;

          cout << "here 2" << endl;

          mySort(arr,currentSize);

          cout << "here 3 " << endl;

          for(int i = 0; i < currentSize; i++){

             if(item == arr[i])
                return false;
          }

          arr[currentSize] = item;
          ++currentSize;
          cout << "here 4" << endl;
          return true;
       }

       T& operator[](int i){

          return arr[i];
       }

       int size(){
          return currentSize;
       }
};


int main()
{

     Set<string> mySet;
     string one = "one";
     string two = "two";
     string three = "three";
     string four = "four";
     string five = "five";
     string six = "six";
     string seven = "seven";
     string eight = "eight";

     mySet.add(one); // lvalue strings did not fix the issue, same as adding rvalue strings
     mySet.add(two);
     mySet.add(three);
     mySet.add(four);
     mySet.add(five);
      mySet.add(six);
//     mySet.add("seven");
//     mySet.add("eight");
//     mySet.add("nine");
//     mySet.add("ten");
//     mySet.add("eleven");
//     mySet.add("twelve");

    cout <<mySet.size() << endl;

}






thanks

This post has been edited by adamchalkley: 03 September 2019 - 01:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: program not terminating gracefully

#2 adamchalkley   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 17-July 15

Re: program not terminating gracefully

Posted 03 September 2019 - 02:02 PM

I think I found the problem, the array size was never specified, so I was essentially writing into random memory and by chance it didn't crash, right?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,167
  • Joined: 12-June 08

Re: program not terminating gracefully

Posted 03 September 2019 - 02:10 PM

I definitely get a crash at the assignment.

In VS if you put a breakpoint down and follow the code you can see the T arr[] is garbage.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: program not terminating gracefully

Posted 03 September 2019 - 08:51 PM

I can't duplicate your problem, probably because the code doesn't compile for me. Perhaps you should increase your compiler warning levels?

||=== Build: Debug in c++homework (compiler: GCC 9-1) ===|
main.cpp|172|error: ISO C++ forbids flexible array member ‘arr’ [-Wpedantic]|
main.cpp||In destructor ‘Set<std::__cxx11::basic_string<char> >::~Set()’:|
main.cpp|168|error: unknown array size in delete|
main.cpp||In function ‘int main()’:|
main.cpp|225|note: synthesized method ‘Set<std::__cxx11::basic_string<char> >::~Set()’ first required here|
main.cpp||In instantiation of ‘Set<T>::Set() [with T = std::__cxx11::basic_string<char>]’:|
main.cpp|225|required from here|
main.cpp|176|error: unknown array size in delete|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|



Also it looks like you've got a problem with out of bounds array access in line 18 of your code (once you fix the above issues).


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1