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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:29:58+00:00 2026-05-26T17:29:58+00:00

I don’t use C that much and I recently got confused about 2d array

  • 0

I don’t use C that much and I recently got confused about 2d array initialization problem. I need to debug somebody’s code and stuck in the following(her original code):

const int location_num = 10000;
bool **location_matrix;
if (node_locations)
    {
        location_matrix = (bool **)malloc(location_num*sizeof(bool *));
        if (!location_matrix)
        {
                cout<<"error 1 allocating location_matrix" << endl;
                exit;
        }
        for (i=0; i<location_num; i++)
        {
                location_matrix[i] = (bool *) malloc(location_num*sizeof(bool ));
                if (!location_matrix[i])
                {
                        cout<<"error 2 allocating location_matrix" << endl;
                        exit;
                }
                for (j=0; j<location_num; j++)
                        location_matrix[i][j] = false;
        }
    }

I thought is was redundant, so I changed it to the following:

location_matrix[location_num][location_num] = { {false} };

However, segmentation fault happens at runtime.
My question is: how does the above code fail? If it looks right, what’s the difference between dynamically allocation and static allocation? Is it just because the dimension might not be constant, so we need to do it dynamically?
Also, just for curiosity, how do I malloc 2d array that stores pointers? Thanks.

  • 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-26T17:29:59+00:00Added an answer on May 26, 2026 at 5:29 pm

    I actually don’t see anything wrong with the code.

    The following code doesn’t work because location_matrix is not allocated:

    location_matrix[location_num][location_num] = { {false} };
    

    GCC will allow the following (as an extension):

    bool location_matrix[location_num][location_num] = { {false} };
    

    But it will blow your stack because 10000 x 10000 is too large.

    Currently, your code uses dynamic allocation. That’s the correct way to do it because the matrix is too large to be done as a static array (and may overrun the stack).

    As for your last question, “how to make a 2d array that stores pointers”: It can be done almost the same way as your current code. Just change bool to int*.

    So a 2D array of NULL int pointers will look like this:

    int ***location_matrix;
    if (node_locations)
    {
        location_matrix = (int***)malloc(location_num*sizeof(int**));
        if (!location_matrix)
        {
                cout<<"error 1 allocating location_matrix" << endl;
                exit;
        }
        for (i=0; i<location_num; i++)
        {
                location_matrix[i] = (int**) malloc(location_num*sizeof(int*));
                if (!location_matrix[i])
                {
                        cout<<"error 2 allocating location_matrix" << endl;
                        exit;
                }
                for (j=0; j<location_num; j++)
                        location_matrix[i][j] = NULL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know much about encryption... Say I'm preparing a SAML request to submit to
Don't ask me why but i can't use this method because I need to
Don't need to do this right now but thinking about the future... What would
Don't be scared of the extensive code. The problem is general. I just provided
I don't edit CSS very often, and almost every time I need to go
I don't understand where the extra bits are coming from in this article about
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Don't think that I'm mad, I understand how php works! That being said. I
Don't know the difference between the System.Window.Controls.TextBox and System.Windows.Forms.TextBox . Noticed that the System.Windows.Forms.TextBox
Don't ask why but I need to add class zebra to the <li> elements

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.