Re: MATRIXES - Dinamic Memory

From: Keith Thompson (kst-u_at_mib.org)
Date: 02/10/05


Date: Thu, 10 Feb 2005 17:12:28 GMT

Stéphane Konstantaropoulos <stephane@cs.york.ac.uk> writes:
[...]
> 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.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.


Relevant Pages

  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: object creation and naming at runtime?
    ... >>replaced with memory references by the compiler. ... I don't know where the computer will allocate space in ... memory for my int so I'll use a name for the address and let ... In the source code the pointer object (which will ...
    (alt.comp.lang.learn.c-cpp)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)