Answer to Question #529 in C++ for Caroline

Question #529
My program treats matrixes reading them from a file. Their size is given in the beginning of the file. How to allocate memory for the matrix?
1
Expert's answer
2010-08-03T11:22:19-0400
For this you need to place your matrix not in the stack, but in a heap. To allocate memory for objects in the heap, use the operator new. You can allocate memory for the matrix by several blocks. To do this you need to allocate memory for an array of string pointers. Then for each pointer using the new operator allocate memory for the string. The syntax for using such a matrix is similar to the syntax of regular matrix:

Foo** matrix;
/*Memory allocation*/
matrix[0][0] = 0; //matrix usage.

Objects, memory for which you have selected by the new operator does not remove themselves. You must call the delete operator to free the memory. You can also use operator delete[] to delete an array.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS