Main issue
- When i used void function instead of jagged it did'nt threw an exception.
- how should i release memory in destructor?
- Thanks!
Explaination
It seems that my program is encountering a breakpoint instruction or a similar call exception, leading to program interruption during execution. Despite removing breakpoints and ensuring correct code logic, the issue persists. This interruption hampers the proper execution of the program and prevents further debugging. I seek advice on potential solutions to resolve this issue and ensure smooth program execution. Any insights or suggestions would be greatly appreciated.
Code
Class of Jagged array
#include <iostream>
using namespace std;
class Jagged {
private:
int row;
int* column;
int** members;
public:
// Default Constructor
Jagged() : row(0), column(nullptr), members(nullptr) {}
// Destructor
~Jagged() {
if (column != nullptr) {
delete[] column;
column = nullptr;
}
if (members != nullptr) {
for (int i = 0; i < row; ++i) {
if (members[i] != nullptr) {
delete[] members[i];
members[i] = nullptr;
}
}
delete[] members;
members = nullptr;
}
}
// Input function
Jagged Input();
// Display function
void Display();
};
Input and output functions
Jagged Jagged::Input() {
cout << "Enter the number of rows: ";
cin >> row;
column = new int[row];
for (int i = 0; i < row; i++) {
cout << "Enter the number of columns for row " << i + 1 << ": ";
cin >> column[i];
}
members = new int* [row];
for (int i = 0; i < row; i++) {
members[i] = new int[column[i]];
for (int j = 0; j < column[i]; j++) {
cout << "Enter value for row " << i + 1 << " and column " << j + 1 << ": ";
cin >> members[i][j];
}
}
return *this;
}
void Jagged::Display() {
cout << "\nJagged Array:\n" << endl;
for (int i = 0; i < row; i++) {
cout << "Row " << i + 1 << ": ";
for (int j = 0; j < column[i]; j++) {
cout << members[i][j] << " ";
}
cout << endl;
}
}
Main function
int main() {
Jagged J1;
J1.Input();
J1.Display();
return 0;
}