void BTreeTest::testOneBTreeLeafNode() {
BTreeLeafNode *test = new BTreeLeafNode();
BTreeData *info1 = new BTreeData;
BTreeData *info2 = new BTreeData;
BTreeData *info3 = new BTreeData;
BTreeData *info4 = new BTreeData;
BTreeData *info5 = new BTreeData;
setInfo(&*info1, (char *)"Lester", (char *)"24", (char *)"student", 6, 2, 7);
setInfo(&*info2, (char *)"David", (char *)"20", (char *)"student", 5, 2, 7);
setInfo(&*info3, (char *)"June", (char *)"4", (char *)"toddler", 4, 1, 7);
setInfo(&*info4, (char *)"Lisa", (char *)"18", (char *)"tutor", 4, 2, 5);
setInfo(&*info5, (char *)"Savannah", (char *)"22", (char *)"barista", 8, 2, 7);
correctData(&*info1, (char *)"Lester", (char *)"24", (char *)"student");
correctData(&*info2, (char *)"David", (char *)"20", (char *)"student");
correctData(&*info3, (char *)"June", (char *)"4", (char *)"toddler");
correctData(&*info4, (char *)"Lisa", (char *)"18", (char *)"tutor");
correctData(&*info5, (char *)"Savannah", (char *)"22", (char *)"barista");
delete info1;
delete info2;
delete info3;
delete info4;
delete info5;
delete test;
}
這是我爲我正在嘗試構建的程序編寫的測試。但是,爲了讓我有更清晰可讀的測試,我需要一個輔助函數。這就是setInfo的用途。這是代碼爲:C++通過引用與幫助函數傳遞
void setInfo(BTreeData *info, char *name, char *age, char *occupation, int
nameSize, int ageSize, int occSize) {
strncpy(info->name, name, nameSize);
strncpy(info->age, age, ageSize);
strncpy(info->occupation, occupation, occSize);
}
這個問題是我試圖設置信息的價值。我試圖通過引用來傳遞信息,所以我可以設置信息。當我檢查xCode調試器時,它會正常工作,直到setInfo函數結束。不過由於某些原因,當我的代碼返回到testOneBTreeLeafNode
繼續執行代碼的其餘部分時,信息內的值會發生變化。它有如下圖所示的額外垃圾垃圾。我通過參考傳遞的理解很差。有人能夠啓發我嗎?
通知大衛的年齡和職業如何應該是20和學生。它有20和學生,但也有其他不必要的垃圾。這是爲什麼發生?
在情況下,有必要在這裏是被B樹結構:
//
// BTree.hpp
// CS130FinalProject
//
// Created by Lester Dela Cruz on 2/19/16.
// Copyright © 2016 Lester Dela Cruz. All rights reserved.
//
#ifndef BTree_hpp
#define BTree_hpp
#define M (5)
#define L (3)
#include <stdio.h>
struct BTreeData {
char name[20];
char age[3];
char occupation[30];
};
struct BTreeLeafNode {
BTreeData dataItems[L];
};
struct BTreeInternalNode {
char keys[M-1][20];
BTreeLeafNode *branch[M];
};
class BTree {
};
#endif /* BTree_hpp */
只是有些挑剔,但最好使用'const int M = 5;'和'const int L = 3'而不是'#define M(5)'和'#define L(3)' ,因爲後兩者不是類型安全的。與C不同,C++的'const'實際上是常量。的xD –