Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I wrote one dll that using dynamic arrays or that. I am using this dll in another software but find some memory problem in y software. I want use constant array nstant that.
I have dynamic array as below:

C++
double* Tenkan_Buffer=new double[barcount];
I get barcount value from this :
double __stdcall ind_Ichimoku(BarData *bars, int barcount, int Tenkan, int Kijun, int Senkou, int mode, int shift)

here barcount has integer value and anytime that I call that it changes.
I want know how define that as constant value?
C++
double Tenkan_Buffer[barcount];
If define in this type it says must write Constant Value instead barcount.
How I can convert barcount value to constant value?
Or any other solution?
Regards,
Posted
Updated 13-Oct-11 8:03am
v2

You cannot: stack allocated arrays must have constant size.
 
Share this answer
 
The clue is here:

double* Tenkan_Buffer=new double[barcount];

you'll need to pass around pointers defined like this:

C++
double const* Tenkan_Buffer=new double[barcount];


i.e. Tenkan_Buffer points to a constant double.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-Oct-11 12:52pm    
[Moved from OP's non-solution]

Thanks,
I need declaring array with barcount.
For example I can say:
double SS[100];
I want know possible define that with size=barcount too?
Regards,
You said yourself that barcode can have different values. That makes barcount a non-constant. Your request therefore doesn't make sense. It's like asking someone to bring a van 'big enough' to carry an unspecified cargo, without telling the amount of the cargo beforehand. Will a pickup suffice? Or should it be a lorry? Maybe with a hanger too? There's no way to know.

The problem here is that you don't state what you actually want to achieve, i. e. why do you need a constant array? I am quite sure that a dynamically allocated array as suggested above will work just fine. Why do you think that it doesn't?

Just to be clear: If you want a constant sized array, the compiler needs to define it's size right in the object code. The compiler runs before you run the program, so it has no chance to know what values your variables will have at run time. Instead it can only rely on constant values that you provide right in your source code.

The effect of defining a constant sized array is that the compiler reserves an area on your stack that will hold this array, and once the stack is released, the memory can be used for other things. In other words, you do not need to take care of memory allocation and deallocation.

The only difference of making an array synamic is that you need to allocate and deallocate the memory for it. This may not be entirely trivial to beginners, but it is common practice in C and C++. If you're not familiar with it yet, it's high time to familiarize yourself with it now! You cannot hope to write any meaningful C/C++ programs without dynamic memory.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900