常数数组问题 [英] Problem With Constant Array

查看:73
本文介绍了常数数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我写了一个使用动态数组的dll.我在其他软件中使用了该dll,但在y软件中发现了一些内存问题.我想使用常量数组nstant that.
我有如下动态数组:

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:

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)


在这里,barcount具有整数值,并且我称它随时改变.
我想知道如何将其定义为常数吗?


here barcount has integer value and anytime that I call that it changes.
I want know how define that as constant value?

double Tenkan_Buffer[barcount];

如果以这种类型定义,则必须写常量值而不是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,

推荐答案

您不能:堆栈分配的数组必须具有恒定的大小.
You cannot: stack allocated arrays must have constant size.


线索在这里:

double * Tenkan_Buffer =新的double [barcount];

您需要传递如下定义的指针:

The clue is here:

double* Tenkan_Buffer=new double[barcount];

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

double const* Tenkan_Buffer=new double[barcount];



即Tenkan_Buffer指向常数double.



i.e. Tenkan_Buffer points to a constant double.


您自己说过条形码可以具有不同的值.这使得计数不是非常恒定的.因此,您的要求没有任何意义.这就像要求某人带足够大"的货车来运送未指定的货物,而不事先告知货物的数量.取车就足够了吗?还是应该是卡车?也许也有衣架?没有办法知道.

这里的问题是,您没有说明您实际想要实现的目标,即e.为什么需要一个常量数组?我非常确定,上面建议的动态分配的数组可以正常工作.为什么您认为没有呢?

只是要清楚一点:如果您想要一个恒定大小的数组,则编译器需要在目标代码中正确定义其大小.编译器在运行程序之前运行,因此它没有机会知道您的变量在运行时将具有什么值.相反,它只能依赖您在源代码中提供的常量值.

定义大小恒定的数组的作用是,编译器在堆栈上保留了一个将容纳该数组的区域,一旦释放堆栈,该内存即可用于其他用途.换句话说,您不需要照顾内存的分配和释放.

使数组动态化的唯一区别是您需要为其分配内存和取消分配内存.对于初学者来说,这可能不完全是琐碎的事,但这是C和C ++的普遍做法.如果您还不熟悉它,现在是时候熟悉它了!您不能希望在没有动态内存的情况下编写任何有意义的C/C ++程序.
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.


这篇关于常数数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆