-1

I'm currently trying to learn and understand C and I'm stuck at understanding pointers.

For an assignment we have to allocate a matrix as follows:

int main(){

    int *mat = (int *)malloc(rows * cols * sizeof(int));
}

However I do not understand why this should bring any advantages over using a simple array.

How would I handle this matrix? What are the advantages to it?

Druckermann
  • 681
  • 2
  • 11
  • 31
  • Not sure of what you mean by "simple array", but [this question](https://stackoverflow.com/q/47956300/1233251) might be relevant. – E_net4 Jun 16 '19 at 13:26
  • Well, that code just allocates a simple array... it's not how you allocate a 2D array – Support Ukraine Jun 16 '19 at 13:26
  • matrix is not necessarily a 2D array. it could be represented by 1D array as well – mangusta Jun 16 '19 at 13:28
  • 1
    @4386427 Depends on what you mean. There is no trouble whatsoever to use the allocated area as a 2D array. The only caveat is that you have to sacrifice the luxury of being able to use the notation `m[x][y]` and instead use `m[x+y*rows]` – klutt Jun 16 '19 at 13:54

1 Answers1

3

What are the advantages to it?

There are many differences between these, but one of the biggest is that the stack is usually limited to 1MB or 8MB. So if you try to declare an array of that size, the program will probably crash. With dynamic memory, it's not strange to allocate hundreds of MB or more.

Another big difference is that you can return pointers to dynamically allocated memory. That won't work with arrays.

// Dummy init function
void init(int * arr, size_t size) 
{
    for(int i=0; i<size; i++) arr[i] = i;
}

// Will compile, but the program will behave strange. Accessing a variable
// after it has gone out of scope causes undefined behavior.
int * returnArray() 
{
    int arr[10];
    init(arr, 10);
    return arr;
}

// Works perfectly;
int * returnMallocatedArray()
{
    int * arr = malloc(10*sizeof(*arr));
    init(arr, 10);
    return arr;
}    

How would I handle this matrix?

Just as a regular array. You can access the element (x,y) with something like mat[x+y*rows] or whatever suits your need best. Just remember though, that as far as the compiler and runtime environment is concerned, the only thing that mat is, is a pointer to a chunk of memory. It's up to you to do the mapping. Doing something like mat[x*cols+y] would also work, as long as you don't mix them.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • @DanielSiegel That sounds like a homework question that you should figure out on your own. I can give you a hint to the answer: It has to do with the cache. – klutt Jun 16 '19 at 14:18
  • @DanielSiegel And when it comes to "efficiency" (which is very imprecise, but I assume you mean speed) the only way to find out for sure is to test both approaches in a realistic environment for your actual problem. Try both for large matrices and you will see a difference. There are questions here on SO on exactly that matter. – klutt Jun 16 '19 at 14:24