my vector class..easy dynamic arrays

This is a discussion on my vector class..easy dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; i was bored and wrote a vector class for arrays...i think its makes arrays easiers for people...took me around one ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Talking my vector class..easy dynamic arrays

    i was bored and wrote a vector class for arrays...i think its makes arrays easiers for people...took me around one hour to think of it up...please try it...feedback will be great..and please feel free to use it... you can download it or copy the source code..here is the source code

    Code:
    /**********************************
    ********** vector class ***********
    **********  by: nextus  ***********
    **** rsx_types_ricer@yahoo.com ****
    **********************************/
    // beta 1, feel free to add to this class
    
    template<class t>
    class vector
    {
    	public:
    
    		// constructors
    		vector();
    		vector(const vector<t>& aObject);
    		vector(int count);
    		vector(int count, t value);
    
    		// destructor
    		~vector();
    
    		// operators
    		t& operator[](int value) const;
    		void operator=(const vector<t> aObject);
    
    		// member functions
    		void resize(int value);
    		int size() const;
    
    	public://private:
    		t* m_array;
    		int m_size;
    };
    
    // empty array, size = 0
    template<class t>
    vector<t>::vector()
    {
    	m_array = 0;
    	m_size = 0;
    }
    
    // copy constructor, copies another object of <class t>
    template<class t>
    vector<t>::vector(const vector<t>& aObject)
    {
    	int count = aObject.m_size;
    	m_array = new t[count];
    	m_size = count;
    
    	for(int i = 0; i < count; i++)
    	{
    		*(m_array + i) = *(aObject.m_array + i);
    	}
    }
    
    // creates an array of size count
    template<class t>
    vector<t>::vector(int count)
    {
    	m_array = new t[count];
    	m_size = count;
    }
    
    // creates an array of size count and initializes it to value
    template<class t>
    vector<t>::vector(int count, t value)
    {
    	m_array = new t[count];
    	m_size = count;
    
    	for(int i = 0; i < count; i++)
    	{
    		*(m_array + i) = value;
    	}
    }
    
    // destroy the dynamic array
    template<class t>
    vector<t>::~vector()
    {
    	delete [] m_array;
    }
    
    // regular array notation returns value in the index of m_array[value]
    template<class t>
    t& vector<t>::operator[](int value) const
    {
    	return *(m_array + value);
    }
    
    // sets an object of vector to equal to object of another vector
    // 1)if the Rvalue is smaller then the Lvalue will be set smaller
    //   and the Lvalues will be set to equal all the Rvalues
    // 2)if the Rvalue is bigger then the Lvalue will be set bigger
    //   and the Lvalues will be set to equal all the Rvalues
    template<class t>
    void vector<t>::operator=(const vector<t> aObject)
    {
    	int count = aObject.m_size;
    
    	delete [] m_array;
    	m_array = new t[count];
    
    	for(int i = 0; i < count; i++)
    	{
    		*(m_array + i) = *(aObject.m_array + i);
    	}
    
    	m_size = count;
    	
    }
    
    // resizes the array and keeps all values if resizes is bigger than
    // the array, if its smaller than it keeps the values up until
    // the value of the variable value
    template<class t>
    void vector<t>::resize(int value)
    {
    	t* tempArray = new t[value];
    	m_size = value;
    
    	for(int i = 0; i < m_size; i++)
    	{
    		*(tempArray + i) = *(m_array + i);
    	}
    
    	delete [] m_array;
    	m_array = new t[value];
    	
    	for(i = 0; i < value; i++)
    	{
    		*(m_array + i) = *(tempArray + i);
    	}
    
    	delete [] tempArray;
    }
    
    // returns the size of the array
    template<class t>
    int vector<t>::size() const
    {
    	return m_size;
    }
    Attached Files Attached Files
    Last edited by nextus; 02-01-2003 at 05:40 PM.
    nextus, the samurai warrior

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    template<class t>
    vector<t>::vector()
    {
    m_size = 0;
    }


    ...must be...

    template<class t>
    vector<t>::vector()
    {
    m_array = 0;
    m_size = 0;
    }


    ...otherwise, you will throw an exception when you delete it....
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    thanks..there its fixed..i forgot about that..i didnt test that..i really appreciate you catching that....
    nextus, the samurai warrior

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    A couple things. The first doesn't really matter much, but you are using manual pointer arithmetic when indexing is shorter, and more clear. Why use

    *(m_array + i)

    when you can just do

    m_array[i]

    Some times it makes sense to use manual pointer arithmetic (IE when you are incrementing the pointer without using a variable (example: ++m_array) but here it just brings down readability.



    The second problem is VERY big.

    You've created a [probable] memory leak.

    You use the false assumption that a dynamically allocated array of 1 element can be deleted without using delete []. That is not true!

    When you dynamically allocate an array, two things are actually allocated. The array itself and some way of keeping track of the size of the array so that when you delete it, it will be deleted fully. Using delete [] checks that value, deletes the corresponding amount of data, but also deletes the data that was used to keep track of the size of the array internally.

    The way this is usually done by the compiler is the extra data is placed before the array in memory.

    So

    m_array = new Datatype[Amount];

    will actually allocate something similar to this type (pseudo-code):

    struct
    {
    inttype SizeOfArray; // = Amount
    Datatype Array[Amount];
    }

    //Where inttype would be an integer type of size pointer to primitive built-in-type

    When you call new, it will return a pointer to the Array member (not the beginning of the struct) so that you can use the array as normal.

    Then, when you call delete [] on it, it knows to go back sizeof(inttype) in memory to check how much to delete but it also has to delete itself as well.

    If you call delete without the brackets, then you totally miss the extra data.

    Code:
    if(m_size > 1)
    	{
    		delete [] m_array;
    	}
    	else
    	{
    		delete m_array;
    	}
    Should just be

    delete [] m_array;

    plain and simple. Otherwise you'll have problems in the future.
    Last edited by Polymorphic OOP; 02-01-2003 at 05:33 PM.

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Talking

    ok will fix..thanks.....
    nextus, the samurai warrior

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    I noticed that you have a destructor.
    I posted some code that used a vector destructor
    Code:
    percent.~vector<int>();
    and got this response.
    "It explicitly calls the destructor, this is a big no-no (unless you know exactly what you are doing and even then its mostly a desperate hack) The destructor will be called when percent goes out of scope. "
    Last edited by kes103; 02-03-2003 at 10:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct types and dynamic arrays
    By simone.marras in forum C Programming
    Replies: 6
    Last Post: 03-14-2009, 11:56 AM
  2. Dynamic Arrays?
    By screenfreak in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 11:38 AM
  3. Creating New Dynamic Arrays
    By mas0nite in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2006, 03:59 PM
  4. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 03:28 PM
  5. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 01:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21