dynamically resize arrays in C++

Discussion in 'C++' started by Frank Reich, Dec 3, 2006.

  1. Frank Reich

    Frank Reich New Member

    Joined:
    Nov 28, 2006
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    HI.

    I would like to resize the array '*data' dynamically. In C it is possible to use realloc, but what would I do in C++?

    Thanks.
    F.R.
    Code:
    #include <iostream>
    #include "cTest.h"
    
    bool MyFunction (cTest &data)
    {
    	int i=0;
    	// DO SOMETHING
    	if (!i)
    		return false;
    	else true;
    }
    
    int main()
    {
    	int i;
    	bool ent=false;
    	cTest *data;
    
    	data = new cTest [5];
    
    	for (i=0;i<5;i++)
    	{
    		ent = MyFunction(data[i]);	
    	}
    
    	if (ent)
    	{
    		for (i=0;i<5;i++)
    		{
    			//increase the size of data
    		}
    	}
    
    	return 0;
    }
     
  2. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Use vector container. It will resize automatically.
     
  3. Frank Reich

    Frank Reich New Member

    Joined:
    Nov 28, 2006
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Hi Aztec.

    thanks for the reply, but vector container takes too much time, because my class cTest contains a lot of data and the vector function push_back is too time consuming.

    Any other idea?
     
  4. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    How did you find out the time? Did you profiled your code? If yes, then show me the exact code which you profiled with the profiled output.
     
  5. Frank Reich

    Frank Reich New Member

    Joined:
    Nov 28, 2006
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    1.) How did you find out the time? --> use GetTickCount() in windows.h.
    2.) then show me the exact code which you profiled with the profiled output --> see the code below.

    main.cpp
    Code:
    #include <iostream>
    #include <windows.h>
    #include <iostream>
    #include "cBild.h"
    
    const int DIM1 = 768;
    const int DIM2 = 1024;
    
    int main()
    {
        DWORD zeit1=GetTickCount(); 
        cBild data;
        std::vector <cBild> Vector;
        int i;
    
        DWORD zeit2; 
        for (i=0;i<5;i++)
        {
            zeit2 = GetTickCount(); 
            Vector.push_back(data);
            std::cout << "\ttime: " << (double)(GetTickCount()-zeit2)/1000 << "s" << std::endl; 
        }
    
        return 0;
    }
    cBild.h
    Code:
    #include <iostream>
    #include <ctime>
    #include <vector>
    
    #pragma once
    
    extern const int DIM1;
    extern const int DIM2;
    
    class cPixel
    {
    public:
        cPixel(void)
        {
            for (int i=0;i<3;i++)
                itsRGB[i] = 0;
        }
        void SetRGB (int type, int val)
        {itsRGB[type] = val;}
        int GetRGB (int type) const
        {return itsRGB[type];}
    
    private:
        int itsRGB[3]; //Red,Green,Blue
    };
    
    class cBild
    {
    public:
        cBild(void)
        {
            itsVal = new cPixel *[DIM1];
            for (int i=0;i<DIM1;i++)
                  itsVal[i] = new cPixel [DIM2];
        }
        ~cBild(void)
        {
            for (int i=0;i<DIM1;i++)
                  delete[] itsVal[i];
            delete[] itsVal;
        }
        cBild (const cBild &right);
        const cBild& operator=(const cBild &right);
    private:
        cPixel **itsVal;
    };
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The time penalty for vectors is usually not severe enough to warrant not using it. However, if you'd like to reallocate, simply create a new, larger dynamic array (using 'new'), copy the old contents over, and delete [] the original. Copying is obviously a time penalty, but that's precisely what realloc does in C. Try to determine the size you need in advance, or allocate as much as you can each time. If you need to upsize fairly frequently you might look into the various algorithms that are used for such operations.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice