C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
Problems w/ callback function over integers of a LinkedList nested within another LinkedList in C++
我在迭代和应用回调函数时遇到问题,该函数嵌套在另一个链接列表中。调用board.forEach(printBoard);
,我没有得到76543217654321
的预期输出,相反,我没有得到任何输入。我很难弄清楚问题,因为A.forEach(printByte);
确实返回了7654321
.如果您知道我的代码出了什么问题,请告诉我。谢谢。
template <class T>
class LinkedList {
struct Node {
T x;
Node *next;
};
private:
Node *head;
public:
LinkedList(){
head = NULL;
}
~LinkedList(){
Node *next = head;
while(next) {
Node *deleteMe = next;
next = next->next;
delete deleteMe;
}
}
// prepend a new value to the beginning of the list
void add(T val){
Node *n = new Node();
n->x = val;
n->next = head;
head = n;
}
void forEach(void (*callback)(T)) {
Node *n = head;
while (n != NULL) {
(*callback)(n->x);
n = n->next;
}
}
};
void printInt(int x) {
Serial.print(x);
}
void printBoard(LinkedList<int> x) {
x.forEach(printInt);
}
void setup() {
Serial.begin(9600);
LinkedList<int> A;
A.add(1);
A.add(2);
A.add(3);
A.add(4);
A.add(5);
A.add(6);
A.add(7);
A.forEach(printInt); // returns 7654321
LinkedList<LinkedList<int>> board;
board.add(A);
board.add(A);
board.forEach(printBoard); // returns nothing
}
我认为代码有一些内存问题。节点的地址(指针(重复(双重释放问题(。如果要复制结构,则需要为链表分配新的内存和重新寻址节点。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std ;
template <class T>
class LinkedList {
struct Node {
T *x;
Node *next;
};
private:
Node *head;
public:
LinkedList(){
printf("constructor:%pn", this) ;
head = NULL;
}
~LinkedList(){
printf("destructor:%pn", this) ;
Node *next = head;
while(next) {
Node *deleteMe = next;
next = next->next;
printf("delete node:%pn", deleteMe) ;
delete deleteMe->x ; // x
delete deleteMe;
}
}
LinkedList* Dup() {
LinkedList *newList = new LinkedList<T>() ;
Node *n = head ;
while (n != NULL) {
T *newx = new T();
*newx=*(n->x) ;
// newList->addTail(n->x) ;
newList->addTail(newx) ;
n = n->next;
}
return newList ;
}
// prepend a new value to the beginning of the list
// val : caller must allocate memory.
void add(T *val){
Node *n = new Node();
printf(" addhead: new alloc node %pn", n) ;
// T *newval = new T();
// *newval = *val ;
// n->x = newval;
n->x = val ;
n->next = head;
head = n;
}
// add to end
// val : caller must allocate memory.
void addTail(T *val) {
Node *n = new Node();
Node *l = head ;
printf(" addtail: new alloc node %pn", n) ;
// T *newval = new T();
// *newval = *val ;
// n->x = newval;
n->x = val ;
n->next = NULL ;
if ( l==NULL ) {
head = n ;
return ;
}
while(l->next!=NULL) {
l = l->next ;
}
l->next = n ;
}
void forEach(void (*callback)(T*)) {
Node *n = head;
while (n != NULL) {
(*callback)(n->x);
n = n->next;
}
printf("n");
}
};
void printInt(int *x) {
// Serial.print(x);
printf("%d ", *x) ;
}
void printBoard(LinkedList<int> *x) {
x->forEach(printInt);
}
int main() {
//Serial.begin(9600);
printf("make An") ;
int avail_Afree=0 ;
#if 0
// use stack memory
LinkedList<int> A;
LinkedList<int> *pA=&A;
#else
// use heap
LinkedList<int> *pA = new LinkedList<int>();
avail_Afree=1 ;
#endif
int val=0 ;
for (int i=1; i<=7; i++) {
printf("add node:%dn", i) ;
// val=i ;
int *pval = new int() ;
*pval = i ;
// pA->add(&val);
pA->add(pval);
}
pA->forEach(printInt);
// prepare copied data.
LinkedList<int> *pA2, *pA3 ;
printf("make A2n") ;
pA2 = pA->Dup() ;
pA2->forEach(printInt) ;
printf("make A3n") ;
pA3 = pA->Dup() ;
pA3->forEach(printInt) ;
// make board
LinkedList<LinkedList<int> > *pBoard;
printf("make Boardn") ;
pBoard = new LinkedList<LinkedList<int> > () ;
printf(" Board add: A2n") ;
pBoard->add(pA2);
printf(" Board add: A3n") ;
pBoard->add(pA3);
printf(" print Boardn") ;
pBoard->forEach(printBoard); // returns nothing
if ( avail_Afree ) {
printf("delete pA:%pn", pA) ;
delete pA ;
}
/*
printf("delete pA2:%pn", pA2) ;
delete pA2 ;
printf("delete pA3:%pn", pA3) ;
delete pA3 ;
// pBoard will free A2,A3.
*/
printf("delete pBoard:%pn", pBoard) ;
delete pBoard ;
return 0 ;
}
输出是这个
make A
constructor:0x7fb8f8d00000
add node:1
addhead: new alloc node 0x7fb8f8d00020
add node:2
addhead: new alloc node 0x7fb8f8d00040
add node:3
addhead: new alloc node 0x7fb8f8d00060
add node:4
addhead: new alloc node 0x7fb8f8d00080
add node:5
addhead: new alloc node 0x7fb8f8d000a0
add node:6
addhead: new alloc node 0x7fb8f8d000c0
add node:7
addhead: new alloc node 0x7fb8f8d000e0
7 6 5 4 3 2 1
make A2
constructor:0x7fb8f8d000f0
addtail: new alloc node 0x7fb8f8d00110
addtail: new alloc node 0x7fb8f8d00130
addtail: new alloc node 0x7fb8f8d00150
addtail: new alloc node 0x7fb8f8d00170
addtail: new alloc node 0x7fb8f8d00190
addtail: new alloc node 0x7fb8f8d001b0
addtail: new alloc node 0x7fb8f8d001d0
7 6 5 4 3 2 1
make A3
constructor:0x7fb8f8d001e0
addtail: new alloc node 0x7fb8f8d00200
addtail: new alloc node 0x7fb8f8d00220
addtail: new alloc node 0x7fb8f8d00240
addtail: new alloc node 0x7fb8f8d00260
addtail: new alloc node 0x7fb8f8d00280
addtail: new alloc node 0x7fb8f8d002a0
addtail: new alloc node 0x7fb8f8d002c0
7 6 5 4 3 2 1
make Board
constructor:0x7fb8f8d002d0
Board add: A2
addhead: new alloc node 0x7fb8f8d002e0
Board add: A3
addhead: new alloc node 0x7fb8f8d002f0
print Board
7 6 5 4 3 2 1
7 6 5 4 3 2 1
delete pA:0x7fb8f8d00000
destructor:0x7fb8f8d00000
delete node:0x7fb8f8d000e0
delete node:0x7fb8f8d000c0
delete node:0x7fb8f8d000a0
delete node:0x7fb8f8d00080
delete node:0x7fb8f8d00060
delete node:0x7fb8f8d00040
delete node:0x7fb8f8d00020
delete pBoard:0x7fb8f8d002d0
destructor:0x7fb8f8d002d0
delete node:0x7fb8f8d002f0
destructor:0x7fb8f8d001e0
delete node:0x7fb8f8d00200
delete node:0x7fb8f8d00220
delete node:0x7fb8f8d00240
delete node:0x7fb8f8d00260
delete node:0x7fb8f8d00280
delete node:0x7fb8f8d002a0
delete node:0x7fb8f8d002c0
delete node:0x7fb8f8d002e0
destructor:0x7fb8f8d000f0
delete node:0x7fb8f8d00110
delete node:0x7fb8f8d00130
delete node:0x7fb8f8d00150
delete node:0x7fb8f8d00170
delete node:0x7fb8f8d00190
delete node:0x7fb8f8d001b0
delete node:0x7fb8f8d001d0
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- C++存储带有可变参数的回调
- 如何使用 Node-addon-API 实现 node-nan 回调
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 访问类C++ C 样式回调
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 在C++中实现回调
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题