我在迭代數組時遇到了一些內存問題。這裏是我的代碼:C++數組拷貝,地址問題
...
std::vector<uint32_t> iD(32);
packBytes(intialPad.data(), intialPad.size(), iD);
const char32_t* modifiedInitPad = iD.data();
uint32_t H[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
...
for (uint32_t i = 0; i < 2; i++)
{
uint32_t block[16];
std::copy(modifiedInitPad + (i * 16), modifiedInitPad + (i * 16) + 16, block);
uint32_t w[64];
uint32_t temp[10];
transformBlock(block, w, temp, H);
}
...
void transformBlock(uint32_t* block, uint32_t* W, uint32_t* temp, uint32_t* H) {
const uint32_t K[] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
const uint32_t size = 16;
for (int i = 0; i < size; i++)
{
W[i] = block[i];
}
for (int t = 16; t < 64; ++t)
W[t] = crypto::f6(W[t - 2]) + W[t - 7] + crypto::f5(W[t - 15]) + W[t - 16];
for (int i = 0; i < 8; i++)
{
temp[i] = H[i];
}
for (int t = 0; t < 64; ++t) {
temp[8] = temp[7] + crypto::f4(temp[4]) + crypto::f1(temp[4], temp[5], temp[6]) + K[t] + W[t];
temp[9] = crypto::f3(temp[0]) + crypto::f2(temp[0], temp[1], temp[2]);
temp[7] = temp[6];
temp[6] = temp[5];
temp[5] = temp[4];
temp[4] = temp[3] + temp[8];
temp[3] = temp[2];
temp[2] = temp[1];
temp[1] = temp[0];
temp[0] = temp[8] + temp[9];
}
for (int i = 0; i < size; i++)
{
H[i] = crypto::add(H[i], temp[i]);
}
}
...
namespace crypto {
namespace {
uint32_t word(int a, int b, int c, int d) {
a &= 0xff;
b &= 0xff;
c &= 0xff;
d &= 0xff;
int val = a << 24 | b << 16 | c << 8 | d;
return val;
}
uint32_t ROTR(uint32_t number, unsigned bits) {
return (number >> bits) | (number << (32 - bits));
}
uint32_t f1(uint32_t x, uint32_t y, uint32_t z) {
return (x & y)^(~x & z);
}
uint32_t f2(uint32_t x, uint32_t y, uint32_t z) {
return (x & y)^(x&z)^(y&z);
}
uint32_t f3(uint32_t x) {
return ROTR(x, 2)^ROTR(x, 13)^ROTR(x, 22);
}
uint32_t f4(uint32_t x) {
return ROTR(x, 6)^ROTR(x, 11)^ROTR(x, 25);
}
uint32_t f5(uint32_t x) {
return ROTR(x, 7)^ROTR(x, 18)^(x >> 3);
}
uint32_t f6(uint32_t x) {
return ROTR(x, 17)^ROTR(x, 19)^(x >> 10);
}
uint32_t add(uint32_t a, uint32_t b) {
return a + b;
}
}
}
當我= 0時,一切順利。但是,當我= 1時,modifiedInitPad沒有與以前相同的地址。所以我在「std:copy(..)」得到一個異常。
有什麼想法?
在環路下面你可能緩衝區溢出
當'i'爲'1'時,您不會複製任何內容。當'i'是'2'時,由於32大於16而遇到麻煩。 – juanchopanza
'std :: copy(x + 32,x + 16,...)'需要修復,因爲juan說 – bobah
我的不好,錯誤的複製/粘貼。我編輯了內容。如果我在「transformBlock(block,w,temp,H);」之後放置斷點modifiedInitPad的地址已更改。 – pehaze