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 7037471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:57+00:00 2026-05-28T01:32:57+00:00

I have been asked in an interview how do i allocate a 2-D array

  • 0

I have been asked in an interview how do i allocate a 2-D array and below was my solution to it.

#include <stdlib.h>

int **array;
array = malloc(nrows * sizeof(int *));

for(i = 0; i < nrows; i++)
{
    array[i] = malloc(ncolumns * sizeof(int));
    if(array[i] == NULL)
    {
        fprintf(stderr, "out of memory\n");
        exit or return
    }
}

I thought I had done a good job but then he asked me to do it using one malloc() statement not two. I don’t have any idea how to achieve it.

Can anyone suggest me some idea to do it in single malloc()?

  • 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-05-28T01:32:58+00:00Added an answer on May 28, 2026 at 1:32 am

    Just compute the total amount of memory needed for both nrows row-pointers, and the actual data, add it all up, and do a single call:

    int **array = malloc(nrows * sizeof *array + (nrows * (ncolumns * sizeof **array));
    

    If you think this looks too complex, you can split it up and make it a bit self-documenting by naming the different terms of the size expression:

    int **array; /* Declare this first so we can use it with sizeof. */
    const size_t row_pointers_bytes = nrows * sizeof *array;
    const size_t row_elements_bytes = ncolumns * sizeof **array;
    array = malloc(row_pointers_bytes + nrows * row_elements_bytes);
    

    You then need to go through and initialize the row pointers so that each row’s pointer points at the first element for that particular row:

    size_t i;
    int * const data = array + nrows;
    for(i = 0; i < nrows; i++)
      array[i] = data + i * ncolumns;
    

    Note that the resulting structure is subtly different from what you get if you do e.g. int array[nrows][ncolumns], because we have explicit row pointers, meaning that for an array allocated like this, there’s no real requirement that all rows have the same number of columns.

    It also means that an access like array[2][3] does something distinct from a similar-looking access into an actual 2d array. In this case, the innermost access happens first, and array[2] reads out a pointer from the 3rd element in array. That pointer is then treatet as the base of a (column) array, into which we index to get the fourth element.

    In contrast, for something like

    int array2[4][3];
    

    which is a “packed” proper 2d array taking up just 12 integers’ worth of space, an access like array[3][2] simply breaks down to adding an offset to the base address to get at the element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been asked in an interview how do you pass an array to
Recently I have been asked an interview question What are the events order in
I have been asked in an interview to write a SQL query which fetches
This have been asked in the interview. How to write own dynamic_cast. I think,
I have been asked recently in a job interview to develop an algorithm that
Interview Question I have been asked this question in an interview, and the answer
As part of preparation for an Interview I have been asked to read up
I have been asked at interview (C# 3.0) to provide a logic to remove
I have been asked this question in interview how does XML and Soap work
Recently, I have been asked a question in an interview what's the difference between

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.