Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 20-11-2009
Member
 
Join Date: Jun 2009
Posts: 105
Problem of template and delete function

I have a problem of memory management and I can not find the solution. I have a Array_1D class, which meets roughly the role of the standard Vector class. This class is a template, so I have a pointer to the memory block containing the data set:
Code:
template <class type>
class      Array_1D
{
private:
   int  number_of_elements;
   int  last_element;
   type* array;
      .............
To add items to the storage class, I extend_array a function taking as parameter the number of element to add:
Code:
template <class type>
type&
Array_1D<type>::extend_array(const int& number_of_elements_to_add)
{
   int  new_number_of_elements = number_of_elements + number_of_elements_to_add;
 if(new_number_of_elements > 0)
 {
  if(array != NULL)
  {
   type* buffer = new type[new_number_of_elements];
         memcpy(buffer, array, sizeof(type) * number_of_elements);
     delete[] array;
     array = buffer;
  }
  else
     array = new type[new_number_of_elements];
 }
     number_of_elements = new_number_of_elements;
     last_element = number_of_elements - 1;
     return array[last_element];
}
The problem is delete []. It planted my application. If I put comments in it works, but obviously I have big memory leaks. I do not understand where is the problem, do you have an idea?
Reply With Quote
  #2  
Old 20-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: Problem of template and delete function

1. Why not do use vector?
2. The copy constructor which are not executed, that can explain you many things
3. A crash problem with a delete, it is often a corrupt and it can have its cause and may close any time before.
Reply With Quote
  #3  
Old 20-11-2009
Member
 
Join Date: Jun 2009
Posts: 105
Re: Problem of template and delete function

It is true that I have used vector, but for several reasons I chose my own coded class. Could you explain the concept of copy constructor if that's the case of interest? If I replace memcpy with a loop which copies elements 1 by 1, it works:
Code:
for(int i = 0;i < number_of_elements;i++)
    buffer[i] =array[i];
I do not understand why it works better, especially since the use of memcpy optimizes performance, and that's what I want ...
Reply With Quote
  #4  
Old 20-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Problem of template and delete function

1. memcpy copies lots of bytes
2. buffer [i] = array [i] calls the constructor type (const type &) ... So that's what you do. If you want better perfs then you can consider your template specialization for certain types or memcpy poses no problem
3. use vector
Reply With Quote
  #5  
Old 20-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Problem of template and delete function

Quote:
It is true that I have used vector, but for several reasons I chose my own coded class.
For more you mean that your class is buggy, less efficient, less generic and less than interoperable std:: vector? or because you have nothing else to do with your time reinventing the cold water? or your are one of those who think the STL is wrong?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem of template and delete function"
Thread Thread Starter Forum Replies Last Post
Where can I find bank check template for Microsoft Word or Template aMADeO! MS Office Support 2 28-01-2012 03:50 PM
Word template problem AMISH Windows Software 4 27-08-2010 01:05 PM
Problem of C++ Template MADGE25 Software Development 5 09-12-2009 04:33 PM
Syntax/Semantics for a C++ "Function Template"? Juan-Carlos Software Development 3 11-11-2009 06:44 PM


All times are GMT +5.5. The time now is 03:31 AM.