C++ Builder

La Isla Bonita



Dynamic Arrays, How to change the size of
Dynamic Arrays, How to change the size of


Description

In this article I show how to change the size of a dynamic array at run time.

DynamicArrays.cpp

#include <vcl.h>
#pragma hdrstop

#include "DynamicArrays.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

const int SIZE1 = 5;
const int SIZE2 = 2;
const int SIZE3 = 10;

TForm1 *Form1;
TButton* Button1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Width    = 500;
    Height   = 300;
    Color    = (TColor)0xb1ee1;
    Position = poScreenCenter;

    Button1 = new TButton(this);
    Button1->Parent  = this;
    Button1->Top     = Height/2;
    Button1->Width   = 150;
    Button1->Left    = (Width - Button1->Width)/2;
    Button1->Height  = 50;
    Button1->Caption = "Button 1";
    Button1->OnClick = Button1Click;

}
//---------------------------------------------------------------------------

class Array
{
  public:
    int a;
};

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    //Create an object of the class Array
    Array* oA;

    //----------------------------------------------------    
    oA = new Array[SIZE1];
    oA[0].a = 1;
    oA[1].a = 2;
    oA[2].a = 3;
    oA[3].a = 4;
    oA[4].a = 5;

    String S1;
    
    for ( int i=0; i< SIZE1; i++)
    {
        S1 += String( oA[i].a ) + "\n";
    }

    ShowMessage( "Size = " + String(SIZE1) +":\n" + S1 );
    delete[] oA;

    //----------------------------------------------------
    //Resize array
    oA = new Array[SIZE2];
    oA[0].a = 100;
    oA[1].a = 200;

    String S2;
    
    for ( int i=0; i< SIZE2; i++)
    {
        S2 += String( oA[i].a ) + "\n";
    }
    ShowMessage( "Size = " + String(SIZE2) +":\n" + S2 );
    delete[] oA;

    //----------------------------------------------------
    //Resize array    
    oA = new Array[SIZE3];
    oA[0].a = 1;
    oA[1].a = 2;
    oA[2].a = 3;
    oA[3].a = 4;
    oA[4].a = 5;
    oA[5].a = 6;
    oA[6].a = 7;
    oA[7].a = 8;
    oA[8].a = 9;
    oA[9].a = 10;

    String S3;

    for ( int i=0; i< SIZE3; i++)
    {
        S3 += String( oA[i].a ) + "\n";
    }
    ShowMessage( "Size = " + String(SIZE3) + ":\n" + S3 );
    delete[] oA;
}

DynamicArrays.h

#ifndef DynamicArraysH
#define DynamicArraysH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.