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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:32:44+00:00 2026-06-15T20:32:44+00:00

I am trying to allocate two-D array at runtime. Size of the array is

  • 0

I am trying to allocate two-D array at runtime. Size of the array is N*N where N is input by the user.
In the end I am deleting the allocated memory. But I am getting Segmentation Fault.
I know that Segmentation fault occurs when I try to access memory not allocated by me. Please Help Code is as follows:

#include<iostream>
using namespace std;
#include <stdio.h>

int main ()
{
    int t,n;
    int **judge;
    int **result;
    int i,j,l;
    int high;
    float count;
    int item;
    cin>>t;
    while(t-->0)
    {
        cin>>n;
        judge=new int*[n];result=new int*[n];
        for(i=0;i<n;i++)
        {
            judge[i]=new int[n];
            result[i]=new int[n];
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                cin>>judge[i][j];
            }
        }
        count=0;
        result[0][0]=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                item=0;
                if(j>0&&i>0)
                {
                if(item<result[i][j-1])
                    item=result[i][j-1];
                else if(item<result[i-1][j])
                    item=result[i-1][j];
                }
                else if(i==0)
                {
                    if(item<result[i][j-1])
                        item=result[i][j-1];
                }
                else if(j==0)
                {
                    if(item<result[i-1][j])
                        item=result[i-1][j];
                }

                result[i][j]=judge[i][j]+item;
            }
        }

        if(result[n-1][n-1]<0.0)
            cout<<"Bad Judges"<<endl;
        else
        {
            count=(result[n-1][n-1]/(float)n);
            cout<<count<<endl;
        }

        for( i = 0 ; i < n  ; ++i)
        {
            delete[] result[i] ;
        }
        delete[] result;
        for(i = 0 ; i < n ; ++i)
        {
            delete[] judge[i] ;
        }
        delete[] judge;
        /* for(i=0;i<n;i++)
        {
            delete[] judge[i];
            delete[] result[i];       
        }*/
        // delete[] judge;
        //delete[] result;
    } 
}
  • 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-15T20:32:45+00:00Added an answer on June 15, 2026 at 8:32 pm

    You loop judge till n+2 and you do not allocate that much.

    In those three lines:

    for(int i = 0 ; i < n + 2 ; ++i)
    {
       delete[] judge[i] ;
    }
    

    By the way this is not the only time you access memory you have not allocated:

    for(i=1;i<n+1;i++)
    {
       for(j=1;j<n+1;j++)
       {
         cin>>judge[i][j];
       }
    }
    

    Here i and j might reach up to n, whilst your allocation allows for values up to n -1.

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

Sidebar

Related Questions

I'm trying to allocate memory for a array of structures, but after it is
i'm trying to declare and allocate memory for two 2d-arrays. However when trying to
I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's
I'm trying to allocate some memory with realloc(). This works so far. But if
I am trying to allocate a 2D dimension array of File Descriptors... So I
I am trying to allocate a dynamic string array in the following way and
I'm trying to create a function that would dynamically allocate an array, sets the
I'm trying to allocate a large space of contiguous memory in C and print
I have a byte array in Java of size 4, which I am trying
I am trying to set a negative zero to an array in two ways.

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.