Hi,
I have the following C++ code
#include <string>
#include <iostream>
//maybe std::string
using namespace std;
int main() {
size_t r = 134480;
size_t c = 268960;
size_t **opt;
opt = (size_t **)malloc(r * sizeof(size_t *));
if(opt != NULL) {
opt[0] = (size_t *)malloc(r * c * sizeof(size_t));
if(opt[0] != NULL) {
for (int i = 1; i < c; i++) {
opt[i] = opt[0] + i * c;
}
} else {
cout << "line:189:r:" << r << endl;
cout << "line:190:c:" << c << endl;
cout << "ERR:diff:185:Memory allocation failed." << endl;
}
} else {
cout << "ERR:diff:183:Memory allocation failed." << endl;
}
..... free code ....
}
I am compiling it on CentOs using the command
g++ test.cpp -o test
When I run it I get the following error:
line:189:r:134480
line:190:c:268960
ERR:diff:185:Memory allocation failed.
Can someone explain me why my first c++ code is failing?