Like Tree2Likes
  • 2 Post By m37h0d

Can you create an array/vector of resizable containers?

This is a discussion on Can you create an array/vector of resizable containers? within the C++ Programming forums, part of the General Programming Boards category; Can you create an array of templated, resizable containers? For example, would this array work? Code: TemplatedContainer<ExampleClass>* ptr; ptr = ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Can you create an array/vector of resizable containers?

    Can you create an array of templated, resizable containers? For example, would this array work?

    Code:
    TemplatedContainer<ExampleClass>* ptr;
    
    ptr = new TemplatedContainer<ExampleClass>[value];
    If my logic is correct, this should work initially, because it will initialize each object in the array to a specific size, but when you try to resize one of the containers it will mess up the array because the memory addresses are contiguous. Am I correct here?

    If this is the case, would a vector work?

    Code:
    std::vector<TemplatedContainer<ExampleClass> > example;
    When you resized one of the objects in the vector, would it properly resize the vector as well, even though you aren't pushing or popping any objects?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That depends on how the container is implemented.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Meaning...?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    The templated container should not itself ever physically change size. The memory it manages however might grow/shrink dynamically. A vector for instance is a container that can hold elements whose number can grow or shrink on the demands of the user. The vector itself never changes size, its size is fixed regardless of how many objects it manages. As the vector "grows" or "shrinks" it is really the memory that the vector manages which does the shrinking and growing, not the vector itself. So, an array of vectors for instance has a fixed size as does a single vector but the individual vectors may manage an ever changing number of elements themselves.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Well, suppose TemplatedContainer contained only a pointer to the given type and a current size. In that case when it changes size this will not change the size of the objects stored in the array or vector, it will only change the amount of storage used from the heap.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by hk_mp5kpdw View Post
    The templated container should not itself ever physically change size.
    More generally, sizeof(T) where T is anything, is a constant.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by LyTning94 View Post
    If my logic is correct, this should work initially, because it will initialize each object in the array to a specific size, but when you try to resize one of the containers it will mess up the array because the memory addresses are contiguous. Am I correct here?
    No.
    The TemplatedContainer objects will be contiguous within the array (or vector), but the data that the pointers inside those containers point to would normally be unchanged and still pointing to the effectively arbitrary memory address that it obtained from 'new'. It does depend on how the TemplatedContainer is implemented though, and whether it has sensible copy-constructor, assignment operator, move-constructor, and move-assignment operators.

    How about you tell us what this TemplatedContainer is? Show the code if it's something you made.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    I did not write the code myself, but here is the grow array function which is called each time an object is added to the container:

    Code:
    template <class T>
    void FFreeListTrashArray<T>::growArray()
    {
    	FFreeListTrashArrayNode* pOldArray;
    	int iOldNumSlots;
    	int iI;
    
    	assert(m_pArray != NULL);
    
    	pOldArray = m_pArray;
    	iOldNumSlots = m_iNumSlots;
    
    	m_iNumSlots *= FLTA_GROWTH_FACTOR;
    	assert((m_iNumSlots <= FLTA_MAX_BUCKETS) && "FFreeListTrashArray<T>::growArray() size too large");
    	m_pArray = new FFreeListTrashArrayNode[m_iNumSlots];
    
    	for (iI = 0; iI < m_iNumSlots; iI++)
    	{
    		if (iI < iOldNumSlots)
    		{
    			m_pArray[iI] = pOldArray[iI];
    		}
    		else
    		{
    			m_pArray[iI].iNextFreeIndex = FFreeList::INVALID_INDEX;
    			m_pArray[iI].pData = NULL;
    		}
    	}
    
    	delete [] pOldArray;
    }
    So the size of the array here never changes because all it does is change the memory m_pArray is pointing to? That would mean that creating an array like my first example above would work, right?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<int> intVect(20);
        std::vector<char> charVect(20);
    
        std::cout << "sizeof(intVect)  is " << sizeof(intVect)
                  << " and it stores " << intVect.size()
                  << " elements" << std::endl;
        std::cout << "sizeof(charVect) is " << sizeof(charVect)
                  << " and it stores " << charVect.size()
                  << " elements" << std::endl;
    
        std::cout << "\nDoubling size of vectors\n" << std::endl;
        intVect.resize(intVect.size()*2);
        charVect.resize(charVect.size()*2);
    
        std::cout << "sizeof(intVect)  is " << sizeof(intVect)
                  << " and it stores " << intVect.size()
                  << " elements" << std::endl;
        std::cout << "sizeof(charVect) is " << sizeof(charVect)
                  << " and it stores " << charVect.size()
                  << " elements" << std::endl;
    
        return 0;
    }
    The output of this is below. Notice the size of the vectors remains constant regardless of what type of data it stores (char or int) and regardless of how many elements it is storing (after the resize operation).

    In the case of the intVect, the vector's size is 20 bytes and it manages (initially) 20 ints each of which are 4 bytes in size. Therefore it manages 80 bytes worth of space and takes up 20 bytes itself.

    An array of such containers is therefore going to be some fixed size equal to whatever the size of one container is multiplied by how many of them are in your array. This size will not change no matter how many objects the individual array elements (the vectors) are themselves managing.

    You shouldn't have any array/memory address issues.
    Attached Images Attached Images  
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you should probably stick with the already provided containers, e.g.

    Code:
    vector< vector<int> > vectorOfVectors;
    works fine. don't reinvent the wheel.
    iMalc and Elysia like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  2. Resizable array of references?
    By JMK in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2010, 12:18 AM
  3. Containers: List vs Vector
    By PetrolMan in forum C++ Programming
    Replies: 8
    Last Post: 04-09-2009, 03:14 AM
  4. When to use vector containers?
    By thetinman in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 05:23 PM
  5. can we create instance of a vector?
    By chintugavali in forum C++ Programming
    Replies: 6
    Last Post: 12-20-2007, 12:32 PM

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