开发者

Array allocation problem

开发者 https://www.devze.com 2023-03-30 19:03 出处:网络
This is pro开发者_StackOverflow中文版bably a very easy question, but for some reason I don\'d understand what I\'m doing wrong here.

This is pro开发者_StackOverflow中文版bably a very easy question, but for some reason I don'd understand what I'm doing wrong here.

Anyways, I got a function that takes in function(size_type m, size_type n) and I have to build an array which is pointed to by a private variable in the class called int *value. I am trying to create an integer array of mxn size but I am having difficulty changing the type of m and n.

I tried:

*value = int[(int)m*(int)n];

as well as using (unsigned int) can someone please help.

EDIT: size_type isn't declared as any type in the specs


You may consider:

value = new int[m*n];

because you need to create a dynamic array. You will need to remember to delete [] this at the correct times.

You will probably find it easier to work with a std::vector, because the memory management is handled for you.


How about this?

typedef int size_type; //could also be this or that other type...

void myFunction(size_type m, size_type n)
{
   int array[(int) m * (int) n ];
   int *value = array;
};

int main()
{
   myFunction(2, 2);
   return 0;
}

Or do you need value outside of the function? Do you want the function to return the value?

0

精彩评论

暂无评论...
验证码 换一张
取 消