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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:36:08+00:00 2026-06-11T20:36:08+00:00

When allocating memory for the 2-dimensional array using malloc() ,segmentation fault occurs when the

  • 0

When allocating memory for the 2-dimensional array using malloc(),segmentation fault occurs when the input size(matrix N*N) is more than 5 (i.e., N>5).

The below code is working fine for inputs(N) less than 5.

Could you please help me out in figuring the problem?

#include<stdio.h>
#include <stdlib.h>

int main(){
    int n;
    int i,j;
    int **adj;
    //reading size of a N*N matrix
    scanf("%d",&n);

    //dynamically allocating memory for a 2-dimensional array
    adj=(int**)malloc(sizeof(int)*n);
    for(i=0;i<n;i++){
        adj[i]=(int*)malloc(sizeof(int)*n);
    }

    //taking input from the file        
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&adj[i][j]);
        }
    }

    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            printf("%d\t",adj[i][j]);
        }
        printf("\n");
    }
    return 0;
}
  • 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-11T20:36:09+00:00Added an answer on June 11, 2026 at 8:36 pm

    This is incorrect:

    adj=(int**)malloc(sizeof(int)*n);
    

    as you are allocating an array of int*, not int. Change to:

    adj = malloc(sizeof(int*)*n); /* Cast unnecessary. */
    /* or: adj = malloc(sizeof(*adj)*n); */
    

    Recommend checking return value of scanf() to ensure an int was correctly read:

    if (1 == scanf("%d", &n))
    {
    }
    

    Useful read: Do I cast the result of malloc?

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

Sidebar

Related Questions

I am allocating memory for array of pointers to structure through malloc and want
The following piece of code gives a segmentation fault when allocating memory for the
I am allocating memory in a C program using malloc . It's possible for
I'm allocating memory using new as and when I receive data in one of
I'm having some issues in allocating memory for an array dynamically in C++ within
I am implementing a two dimensional array dynamic memory allocation . While compiling the
I need some help with allocating arrays dynamically. I have an array whose size
In my C code I am allocating memory for 2d array double E[2000][2000]; but
I'm seeing code allocating memory for float using sizeof(int) . I'm wondering whether sizeof(float)
We allocate memory in C using malloc and in C++ using new. I know

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.