Free2Code
 
Time: 2008-12-04, 07:59pm
Best method to create dynamic array in Class?
Subject: Best method to create dynamic array in Class?  ·  Posted: 2007-02-23, 01:37am
Rank: ? (5)
Member #: 28271
Hello,

I have a class which has a few structures too.

One structure contains a large amount of arrays such as
struct hi {
int hello[200];
}

the structure is then used in the class

hi welcome[20];


However, on many occasions I do not need 20 instances of the welcome struct, therefore if I only need 10 I will be wasting 10*200 of system memory each time an instance of my class is created. What is the best method to ensure only the required amount of structures are established for each instance of the object?

Many thanks

 
  Reply to this ·  Post link ·  Top
Subject: Re: Best method to create dynamic array in Class?  ·  Posted: 2007-02-23, 08:45am
Rank: ? (767)
Member #: 11085
Use the new operator.

Code:
  1. hi* welcome;
  2. welcome = new hi[10];


Also, in the destructor, make sure you call delete [] welcome;

- relpats_eht
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons