Thread: How to create a matrix in the heap....

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    49

    How to create a matrix in the heap....

    refroidPtr->temp_airPtr = (int *) calloc(refroidPtr->num_columns, sizeof (int));

    this is my call to calloc, it creates an array of dimensions refroidPtr->num_columns in the heap where the adresse of the first cell is temp_airPtr.

    now I want to create a matrix in the heap, how do I do this?

    can i do:

    pointer = (int *) calloc(x, y, sizeof (int))?

    would that create an array of dimensions [x][y] in the heap?
    Last edited by Tyrant; 12-02-2007 at 04:13 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Show us how you've done it in the stack.

    Todd

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    int x = 5, y = 6;
    float matrix[x][y];

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Then allocate a block of size ((x*y)*sizeof(data_type));

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You need to multiply rows times columns times sizeof(int) ( x * y * sizeof(int) ) to get the proper amount of heap.

    Todd

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    According to my book, calloc takes two arguments, the first for the amount of elements, and the second for the size of one element.

    But I dont want to wright (x*y, sizeof(int)) because that would be the same as doing mat[x*y]
    in the stack instead of doing mat[x][y]....

    Im new at this so please bare with me....

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you don't want to use calloc(#of elements, element size) to get the storage you need, then what do you want to do then?

    That's the syntax. Do you want to obfuscate it?

    Instead of
    Code:
    ptr = calloc(x*y, sizeof(int)) ;
    are you wanting to do something like
    Code:
    int z = x*y ; 
    ptr = calloc(z, sizeof(int))  ;
    The syntax you are describing, what you don't want to do, is "how it's done".

    Todd

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    mat[x][y] as you put it is just a convenience of the compiler. The array is still x*y in memory. Take the advice that has been given because what you are asking does not make sense.

    The only difference between mat[x][y] and mat[x*y] is one can be accessed in 2D and one is accessed linearily. But using mat[x][y] forces the compiler to do an implicit multiply (or shift) and add to find the offset. I'm also not sure if 2D arrays are guaranteed to lie in the same block of memory although I'm sure they probably do.

    The more dimensions you get the more multiplies and add's it requires to access the actual offset into the array.
    Last edited by VirtualAce; 12-02-2007 at 08:01 PM.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    I found the answer to my question in my book....:

    Code:
    refroidPtr->mat_tempspeedPtr = (float **) calloc(refroidPtr->num_rows, sizeof (float *));
          for(i = 0; i < refroidPtr->num_rows; i++)
               {
               refroidPtr->mat_tempspeedPtr[i] = (float *) calloc(refroidPtr->num_columns, sizeof (float));
               }
    I simply wanted the matrix to be stored in the heap, the same as in the stack when you wright mat[x][y].
    Now I can access the data stored simply by going refroidPtr->mat_tempspeedPtr[x][y].

    I dont understand the code complitely, but it works.
    Last edited by Tyrant; 12-02-2007 at 08:26 PM.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The code you posted and your stated intention of "having it like the stack" do not coincide. But, if you are happy with it, I am happy with it.

    Todd

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Any time you use malloc, calloc, etc, it is stored in the heap. I don't think you are very clear on what the heap is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Sparse Matrix
    By kavka3 in forum C Programming
    Replies: 12
    Last Post: 02-08-2009, 03:23 PM
  2. Passing object references to functions
    By circuitbreaker in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2008, 12:21 PM
  3. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM