|
| ||||||||||
| Tags: delete function, memory leak, memory management, template |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Problem of template and delete function
Code: template <class type>
class Array_1D
{
private:
int number_of_elements;
int last_element;
type* array;
............. 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];
} |
|
#2
| |||
| |||
| 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. |
|
#3
| |||
| |||
| 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]; |
|
#4
| |||
| |||
| 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 |
|
#5
| ||||
| ||||
| Re: Problem of template and delete function Quote:
|
![]() |
|
| Thread Tools | Search this Thread |
| |
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 02:50 PM |
| Word template problem | AMISH | Windows Software | 4 | 27-08-2010 01:05 PM |
| Function of the Delete and Backspace keys in Linux | Chandranath | Tips & Tweaks | 1 | 17-01-2010 05:36 AM |
| Problem of C++ Template | MADGE25 | Software Development | 5 | 09-12-2009 03:33 PM |
| Syntax/Semantics for a C++ "Function Template"? | Juan-Carlos | Software Development | 3 | 11-11-2009 05:44 PM |