function accepting reference as parameter?

This is a discussion on function accepting reference as parameter? within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a very basic copy of a vector. when i add too many elements to the ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    Lightbulb function accepting reference as parameter?

    I am trying to make a very basic copy of a vector.

    when i add too many elements to the vector..all the sudden randomly it starts to behave strangely...only explanation is that maybe i am not understanding how references pass (?)

    Heres a part of the code pertaining to my error...
    (erased nonessential methods for forum)

    Code:
    //myVector.h
    
    template <class T>
    class myVector
    {
    public:
    	myVector(const int sz = 10);
    	void add(const T&);
    	T operator[] (int index){ return arr[index]; }
    private:
    	int avail;
    	T *arr;
    	void expand();
    };
    
    template <class T>
    
    template <class T>
    myVector<T>::myVector(const int sz = 10)
    {
    	avail = 0;
    	arr = new T[sz];
    }
    
    template <class T>
    void myVector<T>::add(const T &in)
    {
            //check to see if arr array is full, if it is then call expand function.
    	if( sizeof(arr) == avail)
    	{	
    		expand();
    	}
    	arr[avail] = in;
    	++avail;
    }
    
    template <class T>
    void myVector<T>::expand()
    {
    	const int sz = sizeof(arr);
    	T temp[sz];
    	for(int i=0;i<sz;++i)
    		temp[i] = arr[i];
    	arr = new T[sz*2];
    	for(int i=0;i<sz*2;++i)
    		arr[i]=temp[i];
    }
    Now here is the code I am testing with it...(there are no compile errors in this program)
    and here is the output following it.

    Code:
    #include <iostream>
    #include "myVector.h"
    
    using namespace std;
    int main()
    {
    	myVector<int> al(1000);
    	al.add(5);al.add(5);al.add(5);al.add(5);al.add(5);al.add(5);
    	al.add(5);al.add(5);al.add(5);al.add(5);al.add(5);al.add(5);
    
    	cout << "size : " << al.size()<< endl;
    	for(int i=0;i<al.size();++i){
    		cout << al[i]<<endl;
    	}
    }
    size : 12
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    1444607057
    5
    It added a random LARGE number to the 11th slot there..(every time)

    If i add one more addition to myVector..the program crashes on runtime...

    what is the problem?

    thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    In expand(), this is wrong:
    Code:
    const int sz = sizeof(arr);
    T temp[sz];
    Firstly, arr is a pointer, so sizeof(arr) results in the size of a pointer in bytes. Next, you use this to create a variable length array named temp, but variable length arrays are not part of standard C++. Rather, you should use new[] to create an array that has the increased capacity that you require, then copy over the elements from the existing array. After the copying is done, you destroy the existing array, and set arr to point to the first element of the new array.

    Additionally, you should keep track of both size (the number of elements in use) and capacity (the number of elements allocated). You should also implement the destructor to destroy the array, and the copy constructor and copy assignment operators to perform a deep copy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    so I changed it, I understand that sizeof was wrong, i removed the sizeof in my add method and now the program works UNTIL i have to expand my array
    (my expand still doesnt work)

    any idea what is wrong with this solution?

    I added a variable sizeArr to hold how big the new size will be for future reference.
    Code:
    void myVector<T>::expand()
    {
        sizeArr = avail*2;
    
    	T *temp = new T[avail*2];
    
    	for(int i=0;i<avail;++i)
    		temp[i] = arr[i];
    
    	arr = temp;
    
    	delete temp;
    }
    Or am i making the same mistakes?

    (btw good post , thanks )
    Last edited by rodrigorules; 11-23-2009 at 03:37 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    You want to delete[] (not delete) arr, not temp, before you assign temp to arr.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    wow now the program works perfectly
    Code:
    template <class T>
    void myVector<T>::expand()
    {
        sizeArr = avail*2;
    
    	T *temp = new T[avail*2];
    
    	for(int i=0;i<avail;++i)
    		temp[i] = arr[i];
    
    	delete [] arr;
    	arr = temp;
    }
    why do I have to delete arr beforehand? , shouldn't the array just point elsewhere now?

    thanks again

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by rodrigorules
    why do I have to delete arr beforehand? , shouldn't the array just point elsewhere now?
    If you do not destroy the existing array, then you will have a memory leak.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library troubles with makefile
    By mslate in forum Linux Programming
    Replies: 17
    Last Post: 07-23-2009, 04:43 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM

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