dynamically resize arrays in C++

Frank Reich's Avatar
Go4Expert Member
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;
}
0
Aztec's Avatar, Join Date: May 2006
Contributor
Use vector container. It will resize automatically.
0
Frank Reich's Avatar
Go4Expert Member
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?
0
Aztec's Avatar, Join Date: May 2006
Contributor
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.
0
Frank Reich's Avatar
Go4Expert Member
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;
};
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.