Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:19:19+00:00 2026-06-05T05:19:19+00:00

We can allocate memory for 2d matrix using 1 malloc call as int (*a)[5];

  • 0
We can allocate memory for 2d matrix using 1 malloc call as
int (*a)[5];
int i,j;

a=malloc(sizeof(int*) * 5); //allocating 5 pointers
and each pointer points to an array of 5 ints

How can we free this memory allocated successfully?
Using free(a) gives run-time error

Using
for(i=0;i<5;i++)
free(a[i]);

free(a);

This also gives run-time error

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:19:21+00:00Added an answer on June 5, 2026 at 5:19 am

    Edit: THE WHOLE STORY.

    Previously I ignored THREE other ways to allocate 2d arrays.

    Dynamic 2d array method 1:

    This one works if you know the the number of columns at compile time.

    #define CCOLS 200
    
    int (*m)[CCOLS] = malloc(cRows * sizeof(*m));
    m[iRow][iCol] = n; // sets the item at iRow*CCOLS + iCol
    
    ...
    
    free(m);
    

    This works because m is declared as a pointer to an array of CCOLS ints. The compiler knows its size and does the math for you. m[iRow] = an array of CCOLS ints.

    You can only pass this to functions with this signature:

    foo(int (*m)[CCOLS]) { ... }
    

    and maybe this signature, depending upon your compiler and the switches you use:

    foo(int m[][CCOLS]) { ... }
    

    not this signature:

    foo(int **m) { ... }
    

    Since the memory layouts and sizes are different.

    int m[][CCOLS] looks like this:

    +---------+---------+---------+---------+     
    | m[0][0] | m[0][1] | m[0][2] | m[0][3] |     
    +---------+---------+---------+---------+     
    | m[1][0] | m[1][1] | m[1][2] | m[1][3] |     
    +---------+---------+---------+---------+     
    | m[2][0] | m[2][1] | m[2][2] | m[2][3] |     
    +---------+---------+---------+---------+     
    | m[3][0] | m[3][1] | m[3][2] | m[3][3] |     
    +---------+---------+---------+---------+     
    

    int **m looks like this:

    +----+        +----+----+----+----+----+      
    |m[0]|  --->  |    |    |    |    |    |      
    +----+        +----+----+----+----+----+      
    |m[1]|  --->  |    |    |    |    |    |      
    +----+        +----+----+----+----+----+      
    |m[2]|  --->  |    |    |    |    |    |      
    +----+        +----+----+----+----+----+      
    |m[3]|  --->  |    |    |    |    |    |      
    +----+        +----+----+----+----+----+      
    

    Dynamic 2d array method 2 (C99 which is not supported by all compilers):

    This one is the same as the previous but you don’t need to know dimensions at compile time.

    int cCols, cRows, iCol, iRow;
    ... set cRows, cCols somehow, they could be passed in as parameters also ...
    int (*m)[cCols] = malloc(cRows * sizeof(*m));
    m[iRow][iCol] = n; // sets the item at iRow*cCols + iCol
    
    ...
    
    free(m);
    

    You can only pass this to functions with this signature:

    foo(int cCols, m[][cCols])  {}
    

    or this one

    foo(int cRows, int cCols, m[cRows][cCols])  {}
    

    If you use gcc, here is more info.

    Dynamic 2d array method 3 using the STACK! (C99 which is not supported by all compilers):

    This lets you avoid malloc entirely if you are ok with your 2d array on the stack.

    int cRows, cCols;
    ... set cRows, cCols somehow ...
    int m[cRows][cCols];
    m[iRow][iCol] = n; 
    

    I assume you could declare a global variable this way too.

    You pass this to functions the same way as method 2.

    Dynamic 2d array method 4:

    This is the array of pointers method that a lot of people use.

    You use one malloc to allocate to be efficient. And of course you then only use one free. Only if you have HUGE arrays where contiguous memory becomes and issue, would you want to malloc each row individually.

    int cCols = 10, cRows = 100, iRow;
    
    // allocate:
    // cCols*cRows*sizeof(int) = space for the data
    // cRows*sizeof(int*) = space for the row ptrs
    int **m = malloc(cCols*cRows*sizeof(int) + cRows*sizeof(int*));
    
    // Now wire up the row pointers.  They take the first cRows*sizeof(int*) 
    // part of the mem becasue that is what m[row] expects.
    // we want each row pointer to have its own cCols sized array of ints.
    // We will use the space after the row pointers for this.
    // One way to calc where the space after the row pointers lies is to
    // take the address of the nth + 1 element: &m[cRows].
    // To get a row ptr, cast &m[cRows] as an int*, and add iRow*cCols to that.
    for (iRow = 0; iRow < cRows; ++iRow)
        m[iRow] = (int*)&m[cRows] + iRow*cCols; 
    
    // or 
    for (p=(int*)&m[cRows] ; iRow = 0; iRow < cRows; ++iRow, p+=cCols)
        m[iRow] = p; 
    
    
    // use it:
    ...
    m[iRow][iCol] = 10;
    ...
    
    // free it
    free(m);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I Allocate a specitic memory address using pointers in c++ ? For example:
I'm using malloc to make an error check of whether memory can be allocated
When you allocate an array using new [] , why can't you find out
We allocate memory in C using malloc and in C++ using new. I know
How can I allocate memory for a struct pointer and assign value to it's
In C, I know I can dynamically allocate a two-dimensional array on the heap
I was trying to figure out how much memory I can malloc to maximum
What is the proper/preferred way to allocate memory in a C API? I can
I have noticed that I can use memory blocks for matrices either allocated using
I read somewhere that java can allocate memory for objects in about 12 machine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.