Jump to content

Dynamic Memory Allocation in C++


Recommended Posts

Writing some C++ for a Uni project, and I've got a problem with dynamic memory allocations. I need to store N sets of data, where each set is made up of 2N long doubles. As N can change I decided to dynamically allocate the memory:

long double*    pSi;

pSi = new long double[iArraySize][iArraySize*2];

 

(Where iArraySize is an unsigned interger)

 

Needless to say it doesn't work, when I try to compile it I get the following error message:

[C++ Error] Evolution.cpp(63): E2313 Constant expression required

[C++ Error] Evolution.cpp(63): E2034 Cannont convert 'long double [*][1]' to 'long double*'

I can't put a constant value in, because if I could I wouldn't need to mess around with the whole dynamic memory in the first place. GRRR Do any of you amazingly clever people know how I do it? :cheesy:

 

 

PS I'm using Borland Builder 5.0, not through choice.

Link to comment
Share on other sites

  • 3 weeks later...
Guest pipplo

(long double) *pSi;

pSi = new (long double) [iArraySize * iArraySize * 2];

then just access it like this ??

 

pSi[y * (iArraySize*2) + x ]

 

:unsure:

 

edit

 

pSi[y * (iArraySize<<2) + x ]

 

or if you make iArraySize a power of two

 

pSi[(y << (iArraySize<<2)) + x ]

Edited by pipplo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...