Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   dynamically resize arrays in C++ (http://www.go4expert.com/forums/dynamically-resize-arrays-cpp-t2111/)

Frank Reich 3Dec2006 13:49

dynamically resize arrays in C++
 
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;
}


Aztec 3Dec2006 14:37

Re: dynamically resize arrays in C++
 
Use vector container. It will resize automatically.

Frank Reich 3Dec2006 14:40

Re: dynamically resize arrays in C++
 
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?

Aztec 3Dec2006 16:33

Re: dynamically resize arrays in C++
 
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.

Frank Reich 3Dec2006 16:47

Re: dynamically resize arrays in C++
 
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;
};


DaWei 6Dec2006 20:34

Re: dynamically resize arrays in C++
 
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.


All times are GMT +5.5. The time now is 21:40.