mtweidmann Posted February 25, 2004 Report Share Posted February 25, 2004 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? PS I'm using Borland Builder 5.0, not through choice. Quote Link to comment Share on other sites More sharing options...
roland Posted February 25, 2004 Report Share Posted February 25, 2004 (edited) unsigned tmpsz = iArraySize * sizeof(long double); pSi = (long double*)malloc(tmpsz * tmpsz * 2); ?? edit: assuming tmpsz * tmpsz * 2 fits in a size_t Edited February 25, 2004 by roland Quote Link to comment Share on other sites More sharing options...
Guest pipplo Posted March 15, 2004 Report Share Posted March 15, 2004 (edited) (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 March 15, 2004 by pipplo Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted March 16, 2004 Report Share Posted March 16, 2004 Another thing to consider about your original code is that you are making the pointer a long double. Only the array elements need to be a long double. variable psi is a memory area that contains the address of the first array element. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.