0

I am in a class working on templates. I am trying to create a template that will make an array for any datatype. I have got the template working I think but when I am outputting my first array for some reason it looks to have doubled my array and I have an array that is created with my random numbers correctly but then an empty array added on. I haven't used a debugger because I would have to cycle through the first 500 elements to get to where I can see what it does. I thought somebody might be able to tell me why the display function is outputting the correct array plus what looks to be an empty array behind it. Here is the code:

#include <iostream>
#include <cstdlib>
#include <array>
#include <iomanip>

using namespace std;
template< typename T > class myArray {
    public:
    
        T *myarray;

  myArray(int size)       
  {
    myarray = new T [size];
  }
         

void bubbleSort (int size)
{
    for (int i = size; i >= 0; i--)
        for (int j = size; j > size - i; j--)
            if (myarray[j] > myarray[j - 1])
                swap(myarray[j], myarray[j-1]);
}
void display(int size)
{
    cout << setprecision(2) << fixed << endl;
    int j = 0;
    for (int i = 0; i < size; i++)
    {
        
        cout << setw(4) << myarray[j] << setw(9) << myarray[j+1] << setw(9) << myarray[j+2] << setw(9) <<
                myarray[j+3] << setw(9) << myarray[j+4] << setw(9) << myarray[j+5] << setw(9) <<
                myarray[j+6] << setw(9) << myarray[j+7] << setw(9) << myarray[j+8] << setw(9) <<
                myarray[j+9] << setw(9) << myarray[j+10] << setw(9) << myarray[j+11] << endl;
        j = j + 12;
    }
}
};
int main(int argc, char** argv) {
    int size = 500;
    myArray <int> t1(size);
    for (int i = 0; i < size; i++)
    {
        int n = rand()% 5000 + 1;
        t1.myarray[i] = n;
    }
   
    t1.display(size);
    return 0;
}

  • Your `display` function is weird: you're displaying 12 elements 500 times. And I'm pretty sure it goes out of bounds at one point. Is there a particular reason for why it's written that way? – Etienne de Martel Jun 30 '21 at 16:56
  • Yes sir the instructor wanted 12 elements per line...it does what i want for the array for the first 500 elements but then it adds what looks like is another 500 elements that are empty. I am trying to get rid of the extra 500 elements. – johnnyboy1899 Jun 30 '21 at 16:58
  • I dont understand how the loop can do a 1000 elements if i set the size to 500. I also don't know why the array has a 1000 elements. – johnnyboy1899 Jun 30 '21 at 17:00
  • @johnnyboy1899 This statement if (myarray[j] > myarray[j - 1]) access memory beyind the allocated array. when j is equal to size. – Vlad from Moscow Jun 30 '21 at 17:03
  • `display()` has an outer loop that iterates `size` times, and outputs `12` values each iteration. So it outputs `12*size` elements - on an array with `size` elements. It is either outputting elements multiple times, or outputting non-existent elements (or both - I haven't checked). – Peter Jun 30 '21 at 17:08
  • You have a member variable that is a pointer to memory owned by the class. Making it `public` is risky. You should also read [The rule of three/five/zero](https://en.cppreference.com/w/cpp/language/rule_of_three) – Ted Lyngmo Jun 30 '21 at 17:11
  • ok i see what your saying thanks – johnnyboy1899 Jun 30 '21 at 17:13
  • Learn about conditional breakpoints. You should be able to create a condition based on the loop variable(s); btw: you should probably introduce a member variable holding the size instead of passing it to all of the functions. In addition to this you're missing a destructor deleting the dynamically allocated array. – fabian Jun 30 '21 at 17:55

1 Answers1

0

I agree with comments: You should use a debugger to fully understand why your code is wrong. You are going off the bounds of the array and your code has undefined behavior.

You made things too complicated. Don't use two counters, but only one:

size_t elements_per_line = 12;
for (size_t i = 0; i < size; ++i) {
     print_single_element(i);
     if (i > 0 && i % elements_per_line == 0) { std::cout << "\n"; }
}

You also should make size a member instead of letting the user to manage the size of the array, because thats error-prone. Also you must read What is The Rule of Three?, because your myArray can be considered broken without it. You will run into problems as soon as you make copies of it.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185