Thread: Class/Pointer Question

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Class/Pointer Question

    I am making my first (semi-)usefull application: my own vector class :P I'm having some troubles with pointers: when I delete them in the destructor, I get a memory leak when I run the program (Something like: "glibc detected blabal double free or corruption" and then a lot of memory addresses) If I don't delete the pointers in the destructor, everything runs fine. I am sure I don't delete somewhere else.
    Here's the class declaration:
    Code:
    template <class T>
    class myVec
    {
    	T *arrPtr;
    	T *temp;
    	int count;
    public:
    	myVec(int numElements);
    	myVec();
    	~myVec();
    
    	void addOnTop(T value);
    	void addBegin(T value);
    	void erase(int index);
    	int size() { return count; }
    	bool isEmpty();
    
    	T& operator=(const myVec &a);
    	T& operator[](int index) {if(index > (count-1)) { cout << "Memory overload!!"; } else { return arrPtr[index]; } }
    };
    And then I have a second question: if you have a class, you can declerate some variables outside public/private/protected. What I know you can't access the variables by myObj.value, but is it protected, private or something else?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Class variables are private by default. This, by the way, is the only real difference between the class keyword and struct keyword (which is public, by default) in C++.

    ... and you'll need to provide more code for me to see why deleting in the destructor causes leaks.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to implement a proper copy constructor and assignement operator for your class.
    Kurt

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    @ Kurt: I'm not finished yet!
    @ Sly:Nevermind, I found the mistake, I deleted in my Constructor :P Why Static?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Try:
    Code:
    delete[] arrPtr;
    delete[] temp;
    Also, those lines work better in your destructor, not your default constructor.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Ideswa
    @ Kurt: I'm not finished yet!
    I see but without them you will have multiple deletes on your data.
    Kurt

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I finished my Vector class... I think.

    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T>
    class myVec
    {
    	T *arrPtr;
    	T *temp;
    	int count;
    public:
    	myVec(int numElements);
    	myVec();
    	~myVec();
    
    	void addOnTop(T value);
    	void addBegin(T value);
    	void erase(int index);
    	void erase(int begin, int end);
    	int size() { return count; }
    	bool isEmpty();
    	void assign(int num, T value);
    	void resize(int num);
    	void clear();
    	void insert(int pos, T value);
    
    	T& operator=(const myVec &a);
    	T& operator[](int index) {if(index > (count-1)) { cout << "Memory overload!!"; } else { return arrPtr[index]; } }
    };
    
    template <class T>
    myVec<T>::myVec(int numElements)
    {
    	count = numElements;
    	arrPtr = new T[count];
    	temp = new T[count];
    }
    
    template <class T>
    myVec<T>::myVec()
    {
    	count = 0;
    	arrPtr = new T[count];
    	temp = new T[count];
    }
    
    template <class T>
    myVec<T>::~myVec()
    {
    	delete arrPtr;
    	delete temp;
    }
    
    template <class T>
    void myVec<T>::addOnTop(T value)
    {
    	temp = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		temp[i] = arrPtr[i];
    	}
    	count++;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i] = temp[i];
    	}
    	arrPtr[count-1] = value;
    }
    
    template <class T>
    void myVec<T>::addBegin(T value)
    {
    	temp = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		temp[i] = arrPtr[i];
    	}
    	count++;
    	arrPtr = new T[count];
    	arrPtr[0] = value;
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i+1] = temp[i];
    	}
    }
    
    template <class T>
    void myVec<T>::erase(int index)
    {
    	temp = new T[count-1];
    	for(int i = 0; i < index; i++)
    	{
    		temp[i] = arrPtr[i];
    	}
    	for(int i = (index+1); i < count; i++)
    	{
    		temp[i-1] = arrPtr[i];
    	}
    	count--;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i] = temp[i];
    	}
    }
    
    template <class T>
    void myVec<T>::erase(int begin, int end)
    {
    	int minus = end-begin;
    	temp = NULL;
    	temp = new T[count-minus];
    	for(int i = 0; i < begin; i++)
    	{
    		temp[i] = arrPtr[i];
    	}
    	for(int i = end; i < count; i++)
    	{
    		temp[i-minus] = arrPtr[i];
    	}
    	arrPtr = NULL;
    	count -= minus;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i] = temp[i];
    	}
    }
    
    template <class T>
    bool myVec<T>::isEmpty()
    {
    	if(count == 0)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    template <class T>
    T& myVec<T>::operator=(const myVec &a)
    {
    	count = a.count;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i] = a.arrPtr[i];
    	}
    }
    
    template <class T>
    void myVec<T>::resize(int num)
    {
    	count = num;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i] = 0;
    	}
    }
    
    template <class T>
    void myVec<T>::assign(int num, T value)
    {
    	count = num;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		arrPtr[i] = value;
    	}
    }
    
    template <class T>
    void myVec<T>::clear()
    {
    	count = 0;
    	arrPtr = new T[0];
    }
    
    template <class T>
    void myVec<T>::insert(int pos, T value)
    {
    	temp = new T[count+1];
    	for(int i = 0; i < pos; i++)
    	{
    		temp[i] = arrPtr[i];
    	}
    	for(int i = (pos+1); i < count; i++)
    	{
    		temp[i-1] = arrPtr[i];
    	}
    	count++;
    	arrPtr = new T[count];
    	for(int i = 0; i < count; i++)
    	{
    		if(i == pos)
    			arrPtr[i] = value;
    		else
    			arrPtr[i] = temp[i];
    	}
    }
    What do you think of it?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Lots of memory leaks. Every time you call new (except in the constructors) you should delete the original value first.

    You are calling delete wrong, see SlyMaelstrom's example.

    >> I finished my Vector class... I think.
    There are quite a few things that need to be fixed or added, but keep going and you'll get there.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The constructor that accepts an int argument should be declared explicit. If not, you could run into situations like:
    Code:
    myVec<int> vec = 10;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I fixed all the bugs you pointed out, I'll expand it with other vector-like functions.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM