In contest programming, I've often been recommended to allocate more space than required for more-than-zero-dimensional data types, i.e. arrays or arrays of arrays, than actually required by the limits of the task.
E.g. for the simple DP task of computing the maximum sum of values when choosing a path in a triangle of integer values from top to bottom, with a maximum height of 100 (see example below), I was recommended to allocate space for 110 rows instead.
The reason I've been given: Just in case it requires more space, it won't crash.
However, I don't see the logic behind this. Should a program attempt to use more space of this array, at least in my eyes, it has to contain some bug. In this case, it would make more sense not to allocate more space, so the bug will be noticed instead of giving the program the room to do whatever it isn't supposed to do.
So I hope that someone can give me an explanation, saying why it's done and in which cases this is actually useful.
Example for above task (bottom right corner):
Without additional allocation: With additional allocation:
1 0 0 0 1 0 0 0 0 0
2 3 0 0 2 3 0 0 0 0
4 5 6 0 4 5 6 0 0 0
7 8 9 5 7 6 9 5 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
In this example, the path with the max. sum would be right-right-left with a sum of 1+3+6+9 = 19
An example C++ implementation to solve the problem (works perfectly without additional allocation):
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> p(100, vector<int>(100));
vector<vector<int>> dp(100, vector<int>(100, -1));
int n = 0;
int maxsum(int r, int c) {
if (r == n-1) {
dp[r][c] = p[r][c];
} else {
if (dp[r][c] == -1) {
dp[r][c] = max(maxsum(r+1, c), maxsum(r+1, c+1)) + p[r][c];
}
}
return dp[r][c];
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; j++) {
cin >> p[i][j];
}
}
cout << maxsum(0, 0) << "\n";
return 0;
}