Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7986547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:53:08+00:00 2026-06-04T11:53:08+00:00

I am trying to develop a generic List with templates. This list is compound

  • 0

I am trying to develop a generic List with templates. This list is compound by a Pointer array T* , an integer for getting the number of elements and some methods( find, contains…) It´s important to say that I cannot use std::library.

My problem comes when I am working with a List<List<int> > for instance.

One of the methods make a resizing of the T* pointer array, So when I have this List<List>>I create an auxpointer of a bigger size than T* and copying T content to auxpointer with a memcpy. The inner Pointers (list.T.T) are copied as pointers too, not memory duplicated, so when I delete the T* pointer and reasign T=auxpointer. I have already lost the data of that pointers in my new T.

template <typename T>
void CGenericList<T>::resize()
{
    T* auxPointer = new T[this->maxElements*2];
    memcpy (auxPointer,this->pointer,this->maxElements*sizeof(T));
    delete[] this->pointer;     
    this->pointer=auxPointer;
    this->maxElements=2*this->maxElements;
}

template<class T>
class CGenericList
{
public:
    T* pointer;
    int N;
    int maxElements;

    CGenericList();
    CGenericList(int);
    ~CGenericList();
    void resize();  
}

Can anyone give me any tips for doing it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:53:10+00:00Added an answer on June 4, 2026 at 11:53 am

    The code you posted shows some problems.

    T* auxPointer = new T[this->maxElements*2];
    

    here you allocate a new array of maxElements*2 – and call the default constructor.
    Which in your case probably initialises all Listelements.

    memcpy (auxPointer,this->pointer,this->maxElements*sizeof(T));
    

    After that you copy the content of your old array to the memory area of the newly allocated memory. This overwrites the pointers to the just created Listelements with the ones from the old array -> memory leak.

    delete[] this->pointer;
    

    Then you delete the array, this calls the destructors of all elements.
    Which hopefully will delete their content and free their memory.

    this->pointer=auxPointer;
    

    Finally you reassign the newly created array. The pointers in the list point to the old listselements and point to not allocated memory anymore (because of the call to the destructor via delete[]).

    A solution would be to implement an copy constructor for your list and call it for all
    elements in your array. (DeepCopy) And of course an assignment operator, i almost forgot 😉

    CGenericList(const CGenericList<T>& copy);
    CGenericList<T>& operator= (const CGenericList<T>& rhs)
    

    Probably somethig like this – be aware that this is “asis” and definitely not exceptionsafe 😉

    template<class T>
    class CGenericList
    {
    public:
        T* pointer;
        int N;
        int maxElements;
    
        CGenericList();
        CGenericList( const CGenericList<T>& copy );
        CGenericList<T>& operator=(const CGenericList<T>& rhs);
        CGenericList(int);
        ~CGenericList();
        void resize();
    };
    
    template <typename T>
    void CGenericList<T>::resize()
    {
        T* auxPointer = new T[this->maxElements*2];
        for(int i=0; i < this->maxElements; i++)
        {
            auxPointer[i] = this->pointer[i];
        }
        delete[] this->pointer;
        this->pointer = auxPointer;
        this->maxElements = this->maxElements*2;
    }
    
    template <typename T>
    CGenericList<T>::CGenericList()
        :N(0)
        ,maxElements(0)
    {
        this->pointer = new T[1];
    }
    
    template <typename T>
    CGenericList<T>::CGenericList(const CGenericList<T>& copy)
        :N(copy.N)
        ,maxElements(copy.maxElements)
    {
        T* temp = new T[copy.maxElements];
        for(int i=0; i<N; i++ )
        {
            temp[i] = copy.pointer[i];
        }
        this->pointer = temp;
    }
    
    template <typename T>
    CGenericList<T>& CGenericList<T>::operator=(const CGenericList<T>& rhs)
    {
        if( this != &rhs )
        {
            delete[] this->pointer;
            this->pointer = new T[rhs.maxElements];
            for(int i=0; i<rhs.maxElements; i++)
            {
                this->pointer[i] = rhs.pointer[i];
            }
        }
        return *this;
    }
    
    
    template <typename T>
    CGenericList<T>::CGenericList(int size)
        :N(0)
        ,maxElements(size)
    {
        this->pointer = new T[size];
    }
    
    template <typename T>
    CGenericList<T>::~CGenericList()
    {
        delete[] this->pointer;
    }
    
    
    int main(int /*argc*/, char */*argv*/[])
    {
        CGenericList<CGenericList<int> > list;
        list.resize();
    
        return 0;
    }
    

    If you don’t like to use stl you can have a look at stlport

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop some code to make generic calls to methods by it's
I'm trying to develop a class that supports an asynchronus method invocation. This is
I'm trying to develop a generic command processor. I would like to create command
Trying to develop using MVVM: I have this Csla.PropertyStatus control that is created in
I am trying to develop some general purpose custom ValidationAttributes. The fact that one
I am trying to develop some simple Eclipse plugins. I am having some problems
im trying to develop a social networking application and im having some trouble showing
I'd been trying to develop one with this for a long time. I'd try
I am trying to develop a application by using Cocos2d. I can't getting value
I am trying to develop an XSD for some XML that looks roughly like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.