简体   繁体   English

C ++-使用尺寸为[250] [12]的2D数组

[英]C++ - using a 2D array of which the dimension is [250][12]

I have a CSV file which contains 250 lines and each line contains 12 items separated by commas. 我有一个CSV文件,其中包含250行,每行包含12个项目,以逗号分隔。 I am going to store this in a 2D array of which the dimension is [250][12]. 我将其存储在二维数组中,其尺寸为[250] [12]。

My question is : " Is it a bad programming practice to use such a huge array ? I am going to pass this array to a method which takes a 2D array as the argument. It comes with openCV. will there be a memory overflow ? " 我的问题是:“使用这么大的数组是一种不好的编程习惯吗?我将把这个数组传递给一个以2D数组为参数的方法。它是openCV附带的。是否会发生内存溢出?”

Well, if you don't have to use it, it would be better. 好吧,如果您不必使用它,那会更好。 For example, read the file line by line and enter each line into the csv parser. 例如,逐行读取文件,然后将每一行输入到csv解析器中。 That way each line is dealt with, and you rely on the (hopefully professional and optimized) memory management. 这样,处理每一行,您就可以依靠(希望是专业且经过优化的)内存管理。

However, if it works it works. 但是,如果可行,它将起作用。 If you don't need this in a production environment, I don't see why you should have to change it, other than good practice. 如果您在生产环境中不需要此功能,除了良好实践之外,我不明白您为什么必须更改它。

First, you have to be clear about how you'll break a line of text into 12 fields typed as expected by openCV. 首先,您必须清楚如何将一行文本分成12个由openCV预期输入的字段。 You may want that to be the central area of the design. 您可能希望将其作为设计的中心区域。

No problem using a static array if the size 250x12 will never change and memory consumption is suitable for the hardware your program is supposed to run on. 如果大小为250x12永远不会改变并且内存消耗适合您的程序应在其上运行的硬件,则使用静态数组没有问题。 You face a trade-off between memory usage and complexity of code: if memory is a concern or if you have flexibility in mind then you should process line by line or even token by token, provided openCV implements those modes. 您将在内存使用率和代码复杂性之间进行权衡:如果需要考虑内存问题或如果您考虑了灵活性,那么只要openCV实现了这些模式,就应该逐行甚至逐个令牌地进行处理。

If you know the size of the array is going to be limited to 250*12 then that is not a huge array, assuming you are using a reasonable machine. 如果您知道阵列的大小将限制为250 * 12,那么假设您使用的是合理的机器,那并不是一个很大的阵列。 Even with long double type elements your array is going to take 36 MB of space. 即使使用长双精度型元素,您的数组也将占用36 MB的空间。 If, however, your underlying elements are objects with sub-elements then you may want to re-think your approach eg, processing the array row-by-row or element-by-element instead of reading it into the memory all at once. 但是,如果您的基础元素是带有子元素的对象,那么您可能需要重新考虑您的方法,例如,逐行或逐元素处理数组,而不是一次将其全部读入内存。

As for passing the array to the function, you will not pass the array by value, you will pass a pointer to the array so it should not be a big overhead. 至于将数组传递给函数,您将不会按值传递数组,而是将指针传递给数组,因此开销不会很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM