简体   繁体   English

如何声明指向二维浮点矩阵的指针?

[英]How to declare a pointer to a 2d float matrix?

Im trying to declare a pointer to a 2d float matrix in order to have a dynamical behaviour of my image data but Im having a compilation error C2057: expected constant expression.我试图声明一个指向二维浮点矩阵的指针,以便我的图像数据具有动态行为,但我有一个编译错误 C2057:预期的常量表达式。 I thought a pointer had to be casted in that way but apparently not.. Please anyone could help me ?我认为必须以这种方式投射指针,但显然不是……请任何人都可以帮助我? Thanks!!谢谢!!

    //Image size input

int imheight;
int imwidth;

cout << "Please, enter image height: \n>";
scanf ("%d",&imheight);
cout << "Please, enter image width: \n>";
scanf ("%d",&imheight);

const int imheight2 = imheight;
const int imwidth2 = imwidth;

float *zArray[imheight2][imwidth2];

Here is one of my other functions where I´m trying to hace access to zArray.这是我尝试访问 zArray 的其他函数之一。 Im not getting the data properly read:我没有正确读取数据:

void LoadRIS( char* inputFileName , float** zArray, int imageHeight , int  imageWidth){

    // Load input RIS file
FILE* lRis = fopen ( inputFileName, "rb" );

// Jump to data position
for (int i = 0; i < 88; i++){       
    uchar a = getc (lRis);   
}   

// Read z array
size_t counter = fread ( *zArray , 1 , imageHeight * imageWidth * sizeof(zArray) , lRis );

//Get max value of RIS
float RISmax = zArray [0][0];
float RISmin = zArray [0][0];
for (int i=0; i<imageHeight; i++) 
{
    for (int j=0; j<imageWidth; j++)
        {
            if (zArray[i][j] > RISmax)
            RISmax = zArray [i][j];
            if (zArray[i][j] < RISmin)
            RISmin = zArray [i][j];
        }
}
std::cout<<"The max value of the RIS file is: "<<RISmax<<"\n";
std::cout<<"The min value of the RIS file is: "<<RISmin<<"\n";
Beep(0,5000);


// Close input file
fclose (lRis);

} }

const int imheight2 = imheight;
const int imwidth2 = imwidth;

It doesn't make constant expressions.它不会产生常量表达式。 You cannot create array with such bounds.您不能创建具有此类边界的数组。 You should use dynamic-allocation , or vector .您应该使用dynamic-allocationvector

The problem is that you're declaring 2 const int variables but you're not assigning them const values.问题是您声明了 2 个const int变量,但没有为它们分配const值。 imheight and imwidth are not constant. imheightimwidth不是常数。

If you're fine with STL:如果您对 STL 满意:

std::vector<std::valarray<float> > floatMatrix;

edit: Just for your information, the space I placed between the > in the above line of code has nothing to do with my coding style.编辑:仅供参考,我在上面代码行中的>之间放置的空间与我的编码风格无关。 Your compiler might assume that >> is the right shift operator instead of 2 template argument list terminators.您的编译器可能会假定>>是右移运算符,而不是 2 个模板参数列表终止符。 Angew's comment below sums it up.下面Angew的评论总结了它。

instead of float *zArray[imheight2][imwidth2];而不是float *zArray[imheight2][imwidth2]; it should be:它应该是:

float **zArray = new float*[imheight2];

for(int i=0; i<imheight2; i++)
{
    zArray[i] = new float[imwidth2];
}

Try this (dynamical allocation)试试这个(动态分配)

//Image size input

int imheight;
int imwidth;

cout << "Please, enter image height: \n>";
scanf ("%d",&imheight);
cout << "Please, enter image width: \n>";
scanf ("%d",&imwidth);

float** zArray = new float*[imheight];
for(int i=0;i<imheight;i++){
    zArray[i] = new float[imwidth];
}

Of course you need to free the allocation by:当然,您需要通过以下方式释放分配:

for(int i=0;i<imheight;i++){
    delete[] zArray[i];
}
delete[] zArray;

Hope this helps :)希望这可以帮助 :)

PS As @FrankH says, this calls too many new s and delete s, wasting a lot of time. PS 正如@FrankH 所说,这调用了太多newdelete ,浪费了很多时间。 Better idea should be to alloc imwidth*imheight space together.更好的主意应该是一起分配 imwidth*imheight 空间。

If you have to do this, then code it at least as:如果您必须这样做,那么至少将其编码为:

float **zArray = new float*[imheight];
float *tmp = new float[imheight*imwidth];

for(int i=0; i<imheight; i++, tmp += imwidth)
    zArray[i] = tmp;

...
delete[] *zArray;
delete[] zArray;

This at least avoids doing more than two new / delete[] calls.这至少避免了两次以上的new / delete[]调用。 And it preserves the functionality of your fread(*zArray, ...) which breaks if the memory isn't contiguous (and it won't generally be if you initialize this via many new calls).并且它保留了fread(*zArray, ...) ,如果内存不连续,它会中断(如果您通过许多new调用对其进行初始化,通常不会这样)。

A proper wrapper class would do just a single new / malloc , like:一个合适的包装类只会做一个new / malloc ,比如:

template <class T> class Array2D {
private:
    size_t m_x;
    T* val;
public:
    Array2D(size_t x, size_t y) :
        m_x(x)),
        val(new T[x*y]) {}
    ~Array2D() { delete[] val; }
    T* operator[](size_t y) { return val + y*m_x; }
}

You still cannot assign an instance of this to a float** .您仍然无法将 this 的实例分配给float** And it still allocates on the heap, where ordinary constant-dimension arrays can be on the stack.它仍然在堆上分配,普通的常量维数组可以在堆栈上。 The only advantage of the additional allocation for the float** is that you're not bound to use a multiplication operation - but a separate memory access instead;float**额外分配的唯一优点是您不必使用乘法运算——而是使用单独的内存访问; that type of behaviour could be templated / traited into the wrapper class.这种类型的行为可以模板化/特征化到包装类中。

Generically, I'm more on the side of multidimensional arrays are evil (see also https://stackoverflow.com/a/14276070/512360 , or C++ FAQ, 16.16 ) but tastes vary ...一般来说,我更倾向于多维数组是邪恶的(另见https://stackoverflow.com/a/14276070/512360C++ FAQ, 16.16 )但口味各不相同......

You cannot use Arrays with dynamic sizes (your width and height variables are not compile time constant).您不能使用具有动态大小的数组(您的宽度和高度变量不是编译时常量)。

You can either use malloc() or new Operator to allocate Memory in a dynamic fashion.您可以使用 malloc() 或 new Operator 以动态方式分配内存。

float *pMatrix = new float[imheight2*imwidth2];

然后访问这样的元素

float f = pMatrix[x + imwidth2 * y];

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

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