MATRIXES - Dinamic Memory

J

Jose Garcia

Hi there,

Can somebody explain me how can i use matrixes with dinamic memory? I
use malloc and free (NOT new & delete). I want to access to the matrix
like Matrix[r][c] but i don't know how to do that with pointers and
malloc & free...

If I declare 4 statics matrixs of 576x720, the program brokes down (how
is it posible!?), so i must use dinamic memory (i suppose)...

Can somebody help me?

Txs a lot! ;)
Good luck for all time
 
?

=?ISO-8859-1?Q?St=E9phane?= Konstantaropoulos

Le jeudi 10 février 2005 à 05:35 -0800, Jose Garcia a écrit :
Hi there,

Can somebody explain me how can i use matrixes with dinamic memory? I
use malloc and free (NOT new & delete). I want to access to the matrix
like Matrix[r][c] but i don't know how to do that with pointers and
malloc & free...

If I declare 4 statics matrixs of 576x720, the program brokes down (how
is it posible!?), so i must use dinamic memory (i suppose)...

Can somebody help me?

int *matrix /* pointer to the start of the matrix */

matrix = malloc(sizeof((*matrix) * 576 * 720)); /* allocate the memory*/
if(matrix == NULL)
exit(1); /* not enough memory */
/* access the matrix by doing */
matrix[1][1]; /* for example, up to [575][719] */

NOTE that the allocated memory will not be zeroed by malloc, calloc does
that for you. You can use:
matrix = calloc(576 * 720, sizeof(*matrix)); /* same but all the members
of the new matrix are zeroed; */

To free your memory:
free(matrix);
matrix = NULL; /* for more safety */


Hope this helps,
 
K

Keith Thompson

Stéphane Konstantaropoulos said:
int *matrix /* pointer to the start of the matrix */

matrix = malloc(sizeof((*matrix) * 576 * 720)); /* allocate the memory*/
if(matrix == NULL)
exit(1); /* not enough memory */

Use exit(EXIT_FAILURE). exit(1) does not reliably return a failure
indication to the environment. You should also consider whether
immediately terminating the program is too drastic.
/* access the matrix by doing */
matrix[1][1]; /* for example, up to [575][719] */

matrix is a pointer to int.
matrix[1] is an int.
matrix[1][1] is an illegal expression, since matrix[1] is not an array
or pointer.

If your matrix is of a fixed size, known at compilation time, you can
do:

struct my_matrix_type {
int m[576][720];
};
struct my_matrix_type *matrix;
matrix = malloc(sizeof *matrix);
/* check for matrix == NULL */
matrix->m[1][1]; /* refers to an element of the matrix */

(My first attempt didn't wrap the array in a struct; m[1][1] indexed
by matrices rather than by ints. There may be a cleaner way to do
this.)

If it's potentially of dynamic size, you can either allocate a
one-dimensional array and roll your own indexing function, or you can
allocate an array of pointers *and* allocate each of the rows.

You can also allocate the whole array of 576*720 ints, then initialize
the pointers so each one points within the single array.

Details are left as an exercise (either for the OP or for anyone else
who wants to jump in).

[...]
To free your memory:
free(matrix);
matrix = NULL; /* for more safety */

This doesn't give you much more safety. If the value of matrix has
been copied to another variable, that other variable still holds the
invalid pointer value. Once you've free()d matrix, just don't refer
to it. Setting matrix to NULL can help you avoid (or detect) certain
errors, but it's better just not to make those errors in the first
place. (And yes, that's easier said than done.)
 
B

Barry Schwarz

Hi there,

Can somebody explain me how can i use matrixes with dinamic memory? I
use malloc and free (NOT new & delete). I want to access to the matrix
like Matrix[r][c] but i don't know how to do that with pointers and
malloc & free...

If I declare 4 statics matrixs of 576x720, the program brokes down (how
is it posible!?), so i must use dinamic memory (i suppose)...

Can somebody help me?

Read section 6.16 of the faq at
http://www.eskimo.com/~scs/C-faq/top.html


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top