打印值输出不一致
Inconsistant outputs on printing values
在这个程序中,我用 C++ 克隆了一个图形。
This is the question that i am coding.
这是我的程序,然后是问题和问题区域。
#include <vector>
#include <unordered_map>
#include <queue>
#include <iostream>
using namespace std;
#define neighbours neighbors
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
#define neighbours neighbors
class Solution {
public:
Node* cloneGraph(Node* node)
{
// BFS
queue <Node*> q;
// add starting vec
q.push(node);
Node *curr;
int value;
vector <int> visited;
unordered_map <int, Node*> umap;
while(!q.empty())
{
curr = q.front();
q.pop();
visited.push_back(curr->val);
//cout << curr->val << "-";
// create new node
Node *newNode = new Node(curr->val);
// add new node val and addr to umap
umap[value] = newNode;
// clone neighbour list
vector <Node*> nlist;
//vector <Node*> list = curr->neighbours; // make copy of given list
for(Node* node: curr->neighbours)
{
value = node->val;
//cout << value << " ";
// search in map first, if exists take addr else make and insert into
// list
if(umap.find(value) == umap.end())
{
umap[value] = new Node(value);
}
nlist.push_back(umap[value]);
if(find(visited.begin(), visited.end(), value) == visited.end())
{
q.push(node);
}
}
cout << endl;
newNode->neighbours = nlist; // copy nlist to nodes list part
}
// starting of new node = umap[1];
return umap[1];
}
};
int main()
{
Node ob1;
Node ob2;
ob1.val = 1;
ob2.val = 2;
vector <Node*> nlist;
nlist.push_back(&ob1);
ob2.neighbours = nlist;
nlist.pop_back();
nlist.push_back(&ob2);
ob1.neighbours = nlist;
Solution obj;
Node *sv = obj.cloneGraph(&ob1);
cout << sv->val << "-";
for(Node *node : sv->neighbours)
{
cout << node->val << endl;
}
cout << &ob1 << " " << sv << endl;
}
当我注释掉最后一行时,我的输出是
1-2
当我保持原样时,输出为
1-0x7ffee561e4e0 0x7fb1ba402840
我还试图注释掉 while
循环末尾的 cout << endl;
并且还注释掉了我的程序的最后一条语句,它给出了输出
1-
出现这种奇怪行为的原因是什么?
umap[value] = newNode;
通过访问未初始化的变量 value
.
表现出未定义的行为
实际上,value
包含一些恰好位于堆栈上的垃圾。对程序看似无关部分的更改会影响堆栈的内容,从而影响 value
的初始值,并最终影响上述未定义行为的表现方式。
在这个程序中,我用 C++ 克隆了一个图形。
This is the question that i am coding.
这是我的程序,然后是问题和问题区域。
#include <vector>
#include <unordered_map>
#include <queue>
#include <iostream>
using namespace std;
#define neighbours neighbors
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
#define neighbours neighbors
class Solution {
public:
Node* cloneGraph(Node* node)
{
// BFS
queue <Node*> q;
// add starting vec
q.push(node);
Node *curr;
int value;
vector <int> visited;
unordered_map <int, Node*> umap;
while(!q.empty())
{
curr = q.front();
q.pop();
visited.push_back(curr->val);
//cout << curr->val << "-";
// create new node
Node *newNode = new Node(curr->val);
// add new node val and addr to umap
umap[value] = newNode;
// clone neighbour list
vector <Node*> nlist;
//vector <Node*> list = curr->neighbours; // make copy of given list
for(Node* node: curr->neighbours)
{
value = node->val;
//cout << value << " ";
// search in map first, if exists take addr else make and insert into
// list
if(umap.find(value) == umap.end())
{
umap[value] = new Node(value);
}
nlist.push_back(umap[value]);
if(find(visited.begin(), visited.end(), value) == visited.end())
{
q.push(node);
}
}
cout << endl;
newNode->neighbours = nlist; // copy nlist to nodes list part
}
// starting of new node = umap[1];
return umap[1];
}
};
int main()
{
Node ob1;
Node ob2;
ob1.val = 1;
ob2.val = 2;
vector <Node*> nlist;
nlist.push_back(&ob1);
ob2.neighbours = nlist;
nlist.pop_back();
nlist.push_back(&ob2);
ob1.neighbours = nlist;
Solution obj;
Node *sv = obj.cloneGraph(&ob1);
cout << sv->val << "-";
for(Node *node : sv->neighbours)
{
cout << node->val << endl;
}
cout << &ob1 << " " << sv << endl;
}
当我注释掉最后一行时,我的输出是
1-2
当我保持原样时,输出为
1-0x7ffee561e4e0 0x7fb1ba402840
我还试图注释掉 while
循环末尾的 cout << endl;
并且还注释掉了我的程序的最后一条语句,它给出了输出
1-
出现这种奇怪行为的原因是什么?
umap[value] = newNode;
通过访问未初始化的变量 value
.
实际上,value
包含一些恰好位于堆栈上的垃圾。对程序看似无关部分的更改会影响堆栈的内容,从而影响 value
的初始值,并最终影响上述未定义行为的表现方式。