wpp
wpp

Reputation: 17

Array allocation problem

This is probably 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

Upvotes: 0

Views: 121

Answers (2)

AudioDroid
AudioDroid

Reputation: 2322

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?

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272687

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.

Upvotes: 6

Related Questions