1

I am having trouble using a function.

I have two functions.

createTwoDArray: prompts user for row and column sizes, creates a new 2D array and returns it while also modifying the row and column variables passed to it.

printTwoDArray: should take in a 2d array and print everything. However, when calling this function, segmentation fault occurs immediately. Not one line of code inside the function is called even.

Thank you :)

int column, row;
char** createTwoDArray(int& column, int& row) {
   int min, max, i, j;
   cout << "\nPlease enter row size:";
   cin >> i;
   row = i;
   cout << "\nPlease enter column size:";
   cin >> j;
   column = j;
   char** dynamicArray2 = new char*[column];
   for(i = 0; i < row; i++) {
     dynamicArray2[i] = new char[column];
     for(j = 0; j < column; j++) {
       dynamicArray2[i][j] = '\0';
    }
   }
   return dynamicArray2;
}
void printTwoDArray(char** array, int row, int column) {
//
}

//
char** array2 = new createTwoDArray(column, row)
printTwoDArray(array2, column, row); //this causes the     segmentation error
//
2
  • 1
    You really should be validating that your input succeeds. If by accident a non-digit character is typed (such as catching ' while pressing Enter) things break quickly in your code. Commented Jun 17, 2019 at 1:55
  • @Galik I just spotted that, too. Commented Jun 17, 2019 at 1:59

1 Answer 1

2

There are two errors: 'column' was used to allocate rows, and row and column were mixed up when calling printTwoDArray().

Here is the fixed code. It runs fine in Visual C++.

#include "pch.h"
#include <iostream>

int column, row;
char** createTwoDArray(int& column, int& row) {
    int min, max, i, j;
    std::cout << "\nPlease enter row size:";
    std::cin >> i;
    row = i;
    std::cout << "\nPlease enter column size:";
    std::cin >> j;
    column = j;

    // *** Use row, not column to allocate the number of rows.
    char** dynamicArray2 = new char*[row]; 
    for (i = 0; i < row; i++) {
        dynamicArray2[i] = new char[column];
        for (j = 0; j < column; j++) {
            dynamicArray2[i][j] = '\0';
        }
    }
    return dynamicArray2;
}

void printTwoDArray(char** array, int row, int column) {
    printf("\nPrinting %d rows:\n\n", row);

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            printf(" %2d", array[i][j]);
        }

        printf("\n");
    }
}

int main()
{
    //
    char** array2 = createTwoDArray(column, row);

    // Pass row and column in the right order!
    printTwoDArray(array2, row, column); 
    //

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this fixed it! Is there any reason why the program would crash instantly if I pass it the wrong order of row/column? Shouldn't at least some lines of the function execute before it crashes?
You're welcome! FYI for me, it didn't crash instantly when I tried, just when it attempted to access out-of-bounds memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.