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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...