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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:19:06+00:00 2026-05-15T16:19:06+00:00

this is an interview question my friend was asked yesterday. The question was something

  • 0

this is an interview question my friend was asked yesterday. The question was something like: will this program crash with an “access violation” error or not? I looked at it for a while and thought no, it won’t. But actually trying this out in visual studio proved me wrong. I cannot figure out what happens here… or to be more precise, i know what happens, but do not understand WHY. The problem seems to be that the matrix2 array does not get allocated at all.

Code below:

#include <iostream>
#include <ctime>

using namespace std;

int** matrixAlloc( const int rows, const int cols );
void matrixAlloc( int** matrix, const int rows, const int cols );
void matrixDealloc( int** m, const int rows);
void matrixPrint( const int* const * const m, const int rows, const int cols );

int main( int argc, char** argv )
{   
    srand( (unsigned int)time( NULL ) );
    int** matrix1 = matrixAlloc( 4, 5 );
    matrixPrint( matrix1, 4, 5 );
    matrixDealloc( matrix1, 4 );

    int ** matrix2 = NULL;
    matrixAlloc( matrix2, 4, 5 );
    matrixDealloc( matrix2, 4 ); // <--- crash occurs here  
}

int** matrixAlloc( const int rows, const int cols )
{
    int **matrix = new int *[ rows ];
    for ( int i = 0; i < rows; i++ )
    {
        matrix[ i ] = new int[ cols ];
        for ( int j = 0; j < cols; j++ )
        {
            matrix[ i ][ j ] = (rand() * 12347) % 10;
        }
    }

    return matrix;
}

void matrixAlloc( int** matrix, const int rows, const int cols )
{
    matrix = new int *[ rows ];
    for ( int i = 0; i < rows; i++ )
    {
        matrix[ i ] = new int[ cols ];
        for ( int j = 0; j < cols; j++ )
        {
            matrix[ i ][ j ] = (rand() * 12347) % 10;
        }

    }
}

void matrixDealloc( int** matrix, const int rows )
{       
    for ( int i = 0; i < rows; i++ )
    {
        delete [] matrix[ i ];
    }
    delete [] matrix;
}

void matrixPrint( const int* const * const matrix, const int rows, const int cols )
{
    for ( int i = 0; i < rows; i++ )
    {
        for ( int j = 0; j < cols; j++ )
        {
            cout << matrix[ i ][ j ] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
  • 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-15T16:19:07+00:00Added an answer on May 15, 2026 at 4:19 pm

    You are passing the double pointer “matrix2” by value. Therefore, when matrixAlloc finishes doing its thing, “matrix2” will still be whatever it was before the function was called. In order to get the change to populate, consider passing matrix2 by reference:

    int** matrix2 = NULL;
    matrixAlloc(&matrix2, 4, 5);
    ...
    

    Don’t forget to change the implementation of matrixAlloc to dereference matrix2 when necessary.

    EDIT: Simple solution below. Change this line:

    void matrixAlloc( int** matrix, const int rows, const int cols )
    

    to this:

    void matrixAlloc( int**& matrix, const int rows, const int cols )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of my friend was asked this question in an interview - You have
This question was asked in an interview for my friend. The interviewer asked to
This was a question that was asked to my friend in a Google interview
A friend of mine was asked this question for an interview and couldn't solve
I was asked this as interview question. Couldn't answer. Write a C program to
This is an interview question asked a month ago.... Do session use cookies? If
This is an open ended interview question. I am not able to get a
Its a interview question. Interviewer asked this basic shell script question when he understand
Hi i was asked in an interview about this question. I did google a
Note: This was an interview question and may not have an actual use case

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.