简体   繁体   English

动态分配QImage数组

[英]Dynamic allocation of QImage array

I need to allocate dynamically memory for QImage array. 我需要为QImage数组动态分配内存。

It worked for me in Linux: 它在Linux中对我有用:

void Arrange_Images(int numOfCams, int numBoards)
{
     QImage * array_of_images[numOfCams*numOfBoards];
     .
     .
     .
 }

But Windows will not resolve this problem and will mark it as error. 但是Windows无法解决此问题,并将其标记为错误。 For a standard array i can use malloc, but this is a QImage array. 对于标准数组,我可以使用malloc,但这是QImage数组。 After this each cell will be filled by images for reordering and further processing. 此后,每个单元将被图像填充以进行重新排序和进一步处理。 Thanks for help. 感谢帮助。

I think Visual Studio compiler still cannot allocate dynamic amount of memory on stack (I think C++03 didn't allowed this, altrough C99 did). 我认为Visual Studio编译器仍然无法在堆栈上分配动态内存(我认为C ++ 03不允许这样做,而C99允许)。 You could use _malloca as an analogue. 您可以使用_malloca作为类似物。 Or just switch to using gcc on Windows. 或者只是切换到在Windows上使用gcc

The problem is that your dynamic array is not really dynamic. 问题在于您的动态数组不是真正的动态。

array_of_images is a static array of pointers to QImage objects, and the size of static arrays has to be known at compile time - this size is not known in your case since the size of your array depends on the input parameters of the Arrange_Images() function. array_of_images是指向QImage对象的指针的静态数组,并且静态数组的大小必须在编译时知道-在您的情况下,此大小是未知的,因为数组的大小取决于Arrange_Images()函数的输入参数。

If you want to create a dynamically allocated array of QImage objects, you should do this this way: 如果要创建动态分配的QImage对象数组,则应采用以下方式:

QImage* array_of_images = new QImage[numOfCams*numOfBoards];

I would also consider if you really need a dynamically allocated array and cannot replace it with something like std::vector. 我还会考虑您是否真的需要动态分配的数组,并且无法将其替换为std :: vector之类的东西。

If the Linux compiler really allowed it in this form, I'm not sure why as it seems to me that it shouldn't compile. 如果Linux编译器确实允许采用这种形式,那么我不确定为什么不应该编译。

To get it working quickly, try this line instead of what you have now: 要使其快速运行,请尝试以下方法,而不是现在的方法:

std::vector<QImage*> array_of_images(numOfCams*numOfBoards);

You will need #include <vector> too, of course. 当然,您还将需要#include <vector> Rest of your code should stay identical, as std::vector will unallocate itself (but not the stuff the pointers in it point to!) when it goes out of scope, just like your current C-style array does. 其余代码应保持相同,因为std::vector超出范围时将取消分配自身(但不会指向其中的指针!),就像您当前的C样式数组一样。

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

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