Al ejecutar este código
#include <iostream>
using namespace std;
template <class T>
class huge{
public:
huge(int bytes) :bytes(bytes) {
size = sizeof(T);
maxi = (bytes / size)+1;
data = (T*)malloc(bytes);
for (int i=0; i < maxi; i++) data[i] = 0;
max_value = 1 << 8 * size;
}
int gmax() {
return maxi;
}
int gbytes() {
return bytes;
}
T& operator[](int index) {
return data[index];
}
huge operator=(huge x) {
//data = (T*)malloc(x.gbytes());
maxi = x.gmax();
for (int i=0; i < maxi; i++) data[i] = x[i];
return x;
}
huge operator=(T x) {
data[0] = x;
for (int i = 1; i < maxi; i++) data[i] = 0;
return x;
}
huge operator+(T x) {
huge y(bytes);
y = *this;
for (int a = 0; x!=0; a++) {
if (x >= (max_value - y[a]-1) && y[a] != 0) {
//cout << y[a] << '+' << x << "; x=1"<<endl;
y[a] += x;
x = 1;
}
else {
//cout << y[a] << '+' << x << "; x=0"<<endl;
y[a] += x;
x = 0;
break;
}
}
return y;
}
huge operator+=(T x) {
for (int a = 0; x != 0; a++) {
if (x >= (max_value - data[a] - 1) && data[a] != 0) {
data[a] += x;
x = 1;
}
else {
data[a] += x;
x = 0;
break;
}
}
return *this;
}
huge operator+(huge x) {
int maximax = (x.gmax() > maxi) ? maxi : x.gmax();
T resto=0;
for (int a=0; a < maximax; a++) {
if (resto >= (max_value - x[a]-1) && x[a] != 0) {
x[a] += resto;
resto=1;
}
else {
x[a] += resto;
resto = 0;
}
if (data[a] >= (max_value - x[a]-1) && x[a] != 0) {
x[a] += data[a];
resto++;
}
else {
x[a] += data[a];
}
}
return x;
}
huge operator+=(huge x) {
int maximax = (x.gmax() > maxi) ? maxi : x.gmax();
T resto = 0;
for (int a = 0; a < maximax; a++) {
if (resto >= (max_value - data[a] - 1) && data[a] != 0) {
data[a] += resto;
resto = 1;
}
else {
data[a] += resto;
resto = 0;
}
if (x[a] >= (max_value - data[a] - 1) && data[a] != 0) {
data[a] += x[a];
resto++;
}
else {
data[a] += x[a];
}
}
return x;
}
bool operator==(huge x){
int a = 1;
if (x.gmax() != maxi)return 0;
for (int i = 0; i < maxi; i++)a = a && (x[i] = data[i]);
return a;
}
bool operator<(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] > data[i];
return 0;
}
bool operator>(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] < data[i];
return 0;
}
bool operator>=(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] < data[i];
return 1;
}
bool operator<=(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] > data[i];
return 1;
}
huge operator<<(int x) {
huge y(bytes);
y = *this;
//cout << endl << !(*this)<<", "<<x << endl;
T r1 = 0, r2 = 0;
int z = 1;
while (x >= 8 * size) {
x /= 2;
z *= 2;
}
T rec = (size - 1 + 7 * size) - x;
//cout << x << ',' << z << endl;
for (int e = 0; e < z; e++) {
for (int i = 0; i < maxi; i++) {
r1 = (unsigned int)y[i] >> rec;
//cout << !y << "<<"<<rec<<'='<<(y[i]>>rec) << ',' << r1 << endl;
r1 = r1 >> 1;
//cout << !y << "<<"<<x<<endl;
y[i] = y[i] << x;
//cout << !y << endl;
y[i] = y[i] + r2;
r2 = r1;
}
}
//cout << !y << endl;
return y;
}
huge operator*(T x) {
huge y(bytes);
for (int i = 0; i < x; i++) {
y += *this;
}
return y;
}
huge operator*=(T x) {
for (int i = 1; i < x; i++) {
*this += *this;
}
return *this;
}
huge operator*(huge x) {
huge y(bytes), rec(bytes);
for (int i = 0; i < x.gmax(); i++) {
for (int e = 0; e < maxi; e++) {
rec = x[i];
//cout <<endl<< !rec << endl;
rec = rec * data[e];
//cout << !rec << endl;
rec = rec << 8 * size * (i + e);
//cout << !rec << endl;
y += rec;
//cout << !rec << endl;
}
}
return y;
}
huge operator*=(huge x) {
huge y(bytes), rec(bytes);
for (int i = 0; i < x.gmax(); i++) {
for (int e = 0; e < maxi; e++) {
rec = x[i];
//cout <<endl<< !rec << endl;
rec = rec * data[e];
//cout << !rec << endl;
rec = rec << 8 * size * (i + e);
//cout << !rec << endl;
y += rec;
//cout << !rec << endl;
}
}
*this = y;
return y;
}
char* operator!() {
char* out;
out = (char*)malloc(sizeof(char)*(size * 16 + 5));
sprintf_s(out, size * 2 + 5, "");
int act = 0;
for (int i = maxi-1; i >=0; i--) {
if ((data[i] != 0 || i == 0) && act == 0) {
act = 1;
sprintf_s(out, size * 16 + 5, "%x", data[i]);
}
else if (act == 1)sprintf_s(out, size * 16 + 5, "%s%08x", out, data[i]);
}
return out;
}
private:
int bytes;
T *data;
T size;
int maxi;
int max_value;
};
int main() {
unsigned int x = (1 << 8 * sizeof(unsigned int)) - 1;
printf("%x\n", x);
x = x>>sizeof(int)*8-1;
x = x >> 1;
printf("\n%x\n", x);
huge<unsigned int> y(8);
y = (1 << 8 * sizeof(unsigned int))-1;
cout << endl << !y << endl;
y = y + 1+12;
cout << endl << !y << endl;
//y = y << 32;
y = y * y + 1 + 0x10000000;
cout << endl << !y << endl;
getchar();
return 0;
}
escrito en c++ en Visual Studio 2017 se produce una excepción en la linea 13:
for (int i=0; i < maxi; i++) data[i] = 0;
Se produjo una excepción:infracción de acceso de escritura. this->data fue 0x1110112.
Sin embargo cuando defino data como un array y comento la linea del malloc:
#include <iostream>
using namespace std;
template <class T>
class huge{
public:
huge(int bytes) :bytes(bytes) {
size = sizeof(T);
maxi = (bytes / size)+1;
//data = (T*)malloc(bytes);
for (int i=0; i < maxi; i++) data[i] = 0;
max_value = 1 << 8 * size;
}
int gmax() {
return maxi;
}
int gbytes() {
return bytes;
}
T& operator[](int index) {
return data[index];
}
huge operator=(huge x) {
//data = (T*)malloc(x.gbytes());
maxi = x.gmax();
for (int i=0; i < maxi; i++) data[i] = x[i];
return x;
}
huge operator=(T x) {
data[0] = x;
for (int i = 1; i < maxi; i++) data[i] = 0;
return x;
}
huge operator+(T x) {
huge y(bytes);
y = *this;
for (int a = 0; x!=0; a++) {
if (x >= (max_value - y[a]-1) && y[a] != 0) {
//cout << y[a] << '+' << x << "; x=1"<<endl;
y[a] += x;
x = 1;
}
else {
//cout << y[a] << '+' << x << "; x=0"<<endl;
y[a] += x;
x = 0;
break;
}
}
return y;
}
huge operator+=(T x) {
for (int a = 0; x != 0; a++) {
if (x >= (max_value - data[a] - 1) && data[a] != 0) {
data[a] += x;
x = 1;
}
else {
data[a] += x;
x = 0;
break;
}
}
return *this;
}
huge operator+(huge x) {
int maximax = (x.gmax() > maxi) ? maxi : x.gmax();
T resto=0;
for (int a=0; a < maximax; a++) {
if (resto >= (max_value - x[a]-1) && x[a] != 0) {
x[a] += resto;
resto=1;
}
else {
x[a] += resto;
resto = 0;
}
if (data[a] >= (max_value - x[a]-1) && x[a] != 0) {
x[a] += data[a];
resto++;
}
else {
x[a] += data[a];
}
}
return x;
}
huge operator+=(huge x) {
int maximax = (x.gmax() > maxi) ? maxi : x.gmax();
T resto = 0;
for (int a = 0; a < maximax; a++) {
if (resto >= (max_value - data[a] - 1) && data[a] != 0) {
data[a] += resto;
resto = 1;
}
else {
data[a] += resto;
resto = 0;
}
if (x[a] >= (max_value - data[a] - 1) && data[a] != 0) {
data[a] += x[a];
resto++;
}
else {
data[a] += x[a];
}
}
return x;
}
bool operator==(huge x){
int a = 1;
if (x.gmax() != maxi)return 0;
for (int i = 0; i < maxi; i++)a = a && (x[i] = data[i]);
return a;
}
bool operator<(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] > data[i];
return 0;
}
bool operator>(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] < data[i];
return 0;
}
bool operator>=(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] < data[i];
return 1;
}
bool operator<=(huge x) {
if (x.gmax() != maxi)return 0;
for (int i = maxi - 1; i >= 0; i--) if (x[i] != data[i]) return x[i] > data[i];
return 1;
}
huge operator<<(int x) {
huge y(bytes);
y = *this;
//cout << endl << !(*this)<<", "<<x << endl;
T r1 = 0, r2 = 0;
int z = 1;
while (x >= 8 * size) {
x /= 2;
z *= 2;
}
T rec = (size - 1 + 7 * size) - x;
//cout << x << ',' << z << endl;
for (int e = 0; e < z; e++) {
for (int i = 0; i < maxi; i++) {
r1 = (unsigned int)y[i] >> rec;
//cout << !y << "<<"<<rec<<'='<<(y[i]>>rec) << ',' << r1 << endl;
r1 = r1 >> 1;
//cout << !y << "<<"<<x<<endl;
y[i] = y[i] << x;
//cout << !y << endl;
y[i] = y[i] + r2;
r2 = r1;
}
}
//cout << !y << endl;
return y;
}
huge operator*(T x) {
huge y(bytes);
for (int i = 0; i < x; i++) {
y += *this;
}
return y;
}
huge operator*=(T x) {
for (int i = 1; i < x; i++) {
*this += *this;
}
return *this;
}
huge operator*(huge x) {
huge y(bytes), rec(bytes);
for (int i = 0; i < x.gmax(); i++) {
for (int e = 0; e < maxi; e++) {
rec = x[i];
//cout <<endl<< !rec << endl;
rec = rec * data[e];
//cout << !rec << endl;
rec = rec << 8 * size * (i + e);
//cout << !rec << endl;
y += rec;
//cout << !rec << endl;
}
}
return y;
}
huge operator*=(huge x) {
huge y(bytes), rec(bytes);
for (int i = 0; i < x.gmax(); i++) {
for (int e = 0; e < maxi; e++) {
rec = x[i];
//cout <<endl<< !rec << endl;
rec = rec * data[e];
//cout << !rec << endl;
rec = rec << 8 * size * (i + e);
//cout << !rec << endl;
y += rec;
//cout << !rec << endl;
}
}
*this = y;
return y;
}
char* operator!() {
char* out;
out = (char*)malloc(sizeof(char)*(size * 16 + 5));
sprintf_s(out, size * 2 + 5, "");
int act = 0;
for (int i = maxi-1; i >=0; i--) {
if ((data[i] != 0 || i == 0) && act == 0) {
act = 1;
sprintf_s(out, size * 16 + 5, "%x", data[i]);
}
else if (act == 1)sprintf_s(out, size * 16 + 5, "%s%08x", out, data[i]);
}
return out;
}
private:
int bytes;
T data[100];
T size;
int maxi;
int max_value;
};
int main() {
unsigned int x = (1 << 8 * sizeof(unsigned int)) - 1;
printf("%x\n", x);
x = x>>sizeof(int)*8-1;
x = x >> 1;
printf("\n%x\n", x);
huge<unsigned int> y(8);
y = (1 << 8 * sizeof(unsigned int))-1;
cout << endl << !y << endl;
y = y + 1+12;
cout << endl << !y << endl;
//y = y << 32;
y = y * y + 1 + 0x10000000;
cout << endl << !y << endl;
getchar();
return 0;
}
el problema se soluciona.
¿A que se debe?