0

Just to clarify this is part of my programming assignment I know how people hate when they ask about hw, but I'm stumped and my current understanding of the topic needs to be clarified.

I need to create a Class called UniqueVector, I have to create this using dynamic array(no vectors allowed). All the data in the array has to be unique ( no duplicates). Now initially they should all begin with size 3, but nothing is inside. I believe I have that correct however I might be wrong.

#include <iostream>
using namespace std;

template<typename T>
class UniqueVector {
public:
    UniqueVector();
    unsigned int capacity();//Returns the size of the space currently allocated for the vector.
    unsigned int size(); //- Returns the current number of elements in the vector.


private:
    T* uniVector;
};

template<typename T>
UniqueVector<T> :: UniqueVector(){
    uniVector = new T[3]; // default constructor 
    delete []uniVector;    
}
template<typename T>
unsigned int UniqueVector<T> :: capacity()
{
    int uni_size= sizeof(uniVector)/sizeof(uniVector[0]); 
    cout << uni_size << endl; //Gives 1
    return (3); //forcing return size 3 even tho it is wrong
}

template<typename T>
unsigned int UniqueVector<T> :: size()
{
    int unique_size=0;
    for(int i=0; i<3; i++){
       cout <<i<<" "<< uniVector[i] << endl; //[0] and [1] gives values? But [2] doesnt as it should 
       if(uniVector[i]!=NULL){ //Only [2] is empty for some reason
          unique_size++; // if arrays arent empty, adds to total
       }               
    };        
    return (unique_size); 
}
int main() 

{
    UniqueVector<int> testVector;
    cout<< "The cap is " << testVector.capacity() << endl;
    cout<< "The size is " <<testVector.size() << endl; 

return 0;
}

At first my capacity function worked if it was just a T uniVector [3] in private and no default constructor, but now it just returns 1 when it should be 3.

Size() never worked in the first place, because I'm some how creating values when I never inputted anything but the size.

1 Answer 1

1
uniVector = new T[3]; // default constructor 
delete []uniVector;    

First, this constructor allocates an array of three Ts.

Then, this array is immediately deleted.

This makes no sense.

Furthermore, the rest of the template assumes that uniVector is a valid pointer. When it is not, since the array it's pointed to is deleted. This is undefined behavior.

template<typename T>
unsigned int UniqueVector<T> :: capacity()
{
    int uni_size= sizeof(uniVector)/sizeof(uniVector[0]); 
    cout << uni_size << endl; //Gives 1
    return (3); //forcing return size 3 even tho it is wrong
}

uniVector is a pointer to T. Therefore, sizeof(uniVector) gives you the size of the pointer, in bytes. Then, dividing by the size of what uniVector is pointing to, produces a completely meaningless result.

It is obvious that you need to keep track of the size of the allocated array. sizeof does not give you that. sizeof is a constant expression. The size of the uniVector pointer is exactly the same, whether it points to an array of 3 values, or an array of million values.

What you need to do, in the constructor, is to allocate the initial array of size 3, then store 3 in a class member that keeps track of the capacity of the array, and then your capacity() class method simply returns the value of this class member.

Similarly, you will also need to keep track of the actual size of the array, with the constructor initializing it to 0.

This should be enough to get you started.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe I have that part down, I just wasnt sure if there was just an easier way, I just dont know why this array would contain anything even though I never put anything inside it.
The issue of existence is completely separate from the issue of initialization. Every array starts out without containing anything, except random garbage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.