我想修改变量th
。但是,当我修改th
时,变量k
也会被修改。我不清楚变量th
和k
是如何相关的,因此,为什么修改变量会影响另一个变量。
#include <memory>
#include <vector>
#include <iostream>
#include <string>
#include <cstring>
using sptrVec = std::shared_ptr< std::vector< int > >;
int main()
{
std::vector< int > b = {4, 6, 8 ,20};
sptrVec k = std::make_shared< std::vector< int > >(b.begin(), b.end());
auto th = std::make_shared< std::vector< sptrVec > >();
th->push_back( k );
std::cout << "Org: " << k->at(0) << std::endl;
std::cout << "Copy: " << th->at(0)->at(0) << std::endl;
std::cout << "Corrupt!" << std::endl;
uint8_t* merkle_ptr = (uint8_t*)&(th->at(0)->at(0));
memfrob(merkle_ptr, sizeof(int));
std::cout << "Org: " << k->at(0) << std::endl;
std::cout << "Copy: " << th->at(0)->at(0) << std::endl;
}
Org: 4
Copy: 4
Corrupt!
Org: 707406382
Copy: 707406382