对标题感到抱歉,但我真的不知道如何处理这个“value=???”
得到一个带有树迭代器的简单二叉树。迭代器可以用 ++ 或 -- 上下移动
它包含一个值和一个键以及根节点、左节点、右节点。
如果我在树上启动一个迭代器
for (Map::TreeIterator i=tree.begin(); i != tree.end(); i++) {
std::cout << i.key() << ": " << i.value() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
迭代器调用 begin()
TreeIterator Tree::begin() {
return TreeIterator(m_root->findFirst());
}
Run Code Online (Sandbox Code Playgroud)
调用 findFirst()
TreeNode* TreeNode::findFirst() {
if (m_left != NULL) {
return m_left->findFirst();
} else {
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
如果树包含很多值,那就没问题了。现在我清除树,打印出计数并尝试启动迭代器来查看是否有项目显示它们,无论如何......
尝试调试显示迭代器进入可能为空的树并查找元素。他从树中获取了根节点,这是一个零节点,取出左侧节点(也是一个零节点)并在左侧节点上运行 findFirst 。
在这里,根(零节点)的左节点(零节点)没有左节点。
m_left=??? m_right=??? m_up=???
Run Code Online (Sandbox Code Playgroud)
所以我有默认构造函数,它将所有节点设置为零节点(我应该需要这个吗?它总是默认构造函数,对吧?)
最后,我的问题是我该如何处理这个问题?也许捕获异常?整个事情在内存 xxx 处引发了未处理的异常
感谢您的解答
我知道当两个指针寻址同一个动态分配的对象时,会发生"删除相同的内存两次"错误.如果delete将其应用于其中一个指针,则将对象的内存返回到免费存储区.如果我们随后删除第二个指针,则免费存储可能已损坏.
但为什么这段代码不会导致运行时错误?
string *str_1 = new string;
auto str_2 = str_1;
*str_1 = "AAA";
cout<<*str_2<<endl;
delete str_1;
delete str_2; // No Error
// Prints AAA
Run Code Online (Sandbox Code Playgroud) 在这段代码中:
int length = atoi(argv[1]);
char *tab = malloc(length * sizeof(char));
memset(tab, '-', length);
puts(tab);
Run Code Online (Sandbox Code Playgroud)
无论我传递什么价值argv[1],输出都是正确的.例如,argv[1] = "5"我得到-----(五个连字符).
我想知道puts()当我没有在我的数组的末尾放置'\ 0'时如何找到输入字符串的结尾char.
我的程序基本上要求用户输入,它会将登录数据,用户名等数据保存在文本文件中,并使用相同的文件检索数据并在输出控制台中显示.当程序执行时,用户可以在选项1到6之间进行选择,选项6用于存在于应用程序之外,其余选项从1到5用于用户输入数据和查看文件中存储的数据.
当程序在控制台中显示数据时,我看到许多不必要的ASCii代码.它为什么会出现,如何让它们出现?谢谢!!
int main() {
//Considering the max length of data entered (name) to be 15.
char data[15];
int n = 0, option = 0, count_n = 0;
//This is the initial mark alloted to a subject.
string empty = "00";
string proctor = "";
//Name of the file in which DB is stored.
ifstream f("Example.txt");
string line;
//The following for loop counts the total number of lines in the file.
for (int i = 0; std::getline(f, line); …Run Code Online (Sandbox Code Playgroud) 为什么不是矩阵随机数的每一行的第一个之后的数字?为什么他们是零?例如,如果我打印了这个矩阵的一行我得到4 0 0 0 0但我应该得到4之后的数字作为随机数.
void readfile(FILE *input,int **matrix){
int i=0, num;
while(fscanf(input, "%d ", &num) == 1){
matrix[i] = malloc((num+1)*sizeof(int));
matrix[i][0] = num;
i++;
}
}
Run Code Online (Sandbox Code Playgroud) 我认为发生的是ASetVar返回的rvalue 是一个相同的副本Class并且共享相同的指针Var.但是当右值调用其解构它删除Class的Val.
class A
{
private:
int* Var;
public:
A SetVar(int);
~A()
{
delete Var;
}
};
A A::SetVar(int NewVar)
{
Var=new int;
*Var=NewVar;
//POINT A
return *this;
}
int main()
{
A Class;
Class.SetVar(8);
//POINT B
}
Run Code Online (Sandbox Code Playgroud)
在POINT A *Val等于8,但POINT B *Val平等-17891602.由于尝试删除Val两次,我也得到_BLOCK_TYPE_IS_VALID(pHead-> nHeadUse).
删除解构器可以解决问题,但会造成内存泄漏.
我正在使用Visual Studio 2013.我收到Access violation reading location 0xCDCDCDD1错误.来自维基百科的参考文献:Magic Numbers,这里,这个问题告诉我,我正在使用未初始化的指针.我认为问题在于malloc我正在使用的功能,尽管在使用之前我确实参考过CPlusPlus文档.
我的代码:
#include "SingleNode.h"
#include "SingleList.h"
#include <iostream>
SingleList::SingleList() {}
SingleList::~SingleList() {}
...
void SingleList::addHead(int value){
ISingleNode *temp1 = (ISingleNode *)malloc(sizeof(SingleList));
head = NULL;
temp1->setValue(value);
temp1->setNext(head->getNext());
if (!head->getNext())
{
head = temp1;
}
else
{
temp1->setNext(head->getNext());
head = temp1;
}
}
...
Run Code Online (Sandbox Code Playgroud)
错误在行中 temp1->setValue(value);
该setValue(int value)功能包括在这里:
#include "SingleNode.h"
SingleNode::SingleNode() {}
SingleNode::~SingleNode() {}
void SingleNode::setValue(int value){
this->value = …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么,但这是我的代码:
char expression[256] = {};
cin >> expression;
cout << endl;
int** variableTable = NULL;
int numOfVals = getNumberOfVariables(expression);
variableTable = static_cast<int**>(calloc(numOfVals, sizeof(int*)));
for (int i = 0; i < numOfVals; i++)
{
variableTable[i] = static_cast<int*>(calloc(2, sizeof(int)));
}
fillPromoterTable(expression, variableTable, numOfVals);
Run Code Online (Sandbox Code Playgroud)
这是fillPromoterTable
void fillPromoterTable(const char* expression, int** variableTable, int numOfVals)
{
char promoter[15] = {};
char *token;
char* expCpy = pcstrdup(expression);
for (int i = 0; numOfVals; i++)
{
token = strtok(expCpy, "+-*/");
int nLen = istrlen(token);
for (int j …Run Code Online (Sandbox Code Playgroud) 我遇到了一个函数示例如下:
int someFunction(void)
{
int i;
for(i = 0; i < 10; i++)
{
a_var[i] = 0xFFFF; //uninitialize a_var
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
取消初始化变量的目的是什么,为什么必须使用0XFFFF?
我一直在尝试弄清楚为什么我的数组使用以下代码打印出 -858993460
int myInt = 0;
int myArray[10]; // I know this is undefined, but the same results occur if I put {} to initialize it
while(myInt <= 10)
{
myArray[myInt] = myInt;
myInt++;
std::cout << "Test " << myArray[myInt] << std::endl;
std::cout << "Counter " << myInt << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当打印出来时,myInt增量会很好。但是,myArray打印出-858993460。如果我插入一个断点并在每次迭代时跳过 while 循环,我可以看到数字被输入到数组中,但它只打印出该随机数(假设它是来自堆栈/堆的随机数?)。
现在,如果我交换 while 循环,那么现在就是
while(myInt <= 10)
{
myInt++;
myArray[myInt] = myInt;
}
Run Code Online (Sandbox Code Playgroud)
它正确打印出数字。我似乎无法弄清楚这里发生了什么......