[英]Linked list c++: append function does not seem to work
I am trying to understand pointers and linked lists in c++. 我正在尝试理解c ++中的指针和链接列表。 for this i have written a small Programm.
为此,我写了一个小程序。 I seem to have trouble going through the list with a curPtr which seems to give me all initial elements in the list but not the one i have appended.
我似乎很难通过curPtr遍历列表,这似乎给了我列表中的所有初始元素,但没有提供我附加的元素。 I have no clue what is the problem.
我不知道这是什么问题。 here is my main function:
这是我的主要功能:
int sizeOfLL = 5;
int* LLtail;
int* LLcur;
int* LLhead;
std::cout << "start values: " << (long long) LLhead << " " << (long long) LLcur << " " << (long long) LLtail << std::endl;
makeLListCstyle(LLhead, LLtail, sizeOfLL);
std::cout << "head in main(): " << (long long) LLhead << std::endl;
LLcur = LLhead;
std::cout << "cur in main(): " << (long long) LLcur << std::endl;
std::cout << "tail in main(): " << (long long) LLtail << std::endl;
for (int i = 0; i < sizeOfLL; i++){
LLcur[i] = 3*i;
}
//LLcur[3]=3;
appendElement(LLtail, 2);
std::cout << "tail in main(): " << (long long) LLtail << " value: " << LLtail[0] << std::endl;
sizeOfLL = getElementsInList(LLhead, LLtail);
std::cout << sizeOfLL << endl;
for (int i = 0; i < sizeOfLL; i++){
cout << "cur in main(): " << (long long) &LLcur[i] << " value: " << LLcur[i] << endl;
}
return 0;
here my llist.hpp 这是我的llist.hpp
void makeLListCstyle(int* &head, int* &tail, int numElements){
head = (int*) malloc(numElements * sizeof(int));
// std::cout << "sizeof(int): " << sizeof(int) << endl;
// std::cout << "head in makeLList(): " << (long long) head << std::endl;
tail = head + numElements; // points now at address which is sizeif(int) * numElements further in memory.
// std::cout << "tail in makeLList(): " << (long long) tail << std::endl;
// return head;
}
void appendElement(int* &tail, int newElement){
std::cout << "tail in appendElement(): " << (long long) tail << std::endl;
tail = tail+1;
std::cout << "tail in appendElement(): " << (long long) tail << std::endl;
// std::cout << "value of tail in appendElement(): " << (long long) *tail << std::endl;
*tail = newElement;
// std::cout << "value of tail in appendElement(): " << (long long) *tail << std::endl;
}
int getElementsInList(int* &head, int* &tail){
/*
std::cout << "head in getElementsInList(): " << (long long) head << std::endl;
std::cout << "tail in getElementsInList(): " << (long long) tail << std::endl;
std::cout << ( (long long)(tail) - (long long)(head) )/ sizeof(int) << endl;
*/
return ( (long long)(tail) - (long long)(head) ) / sizeof(int);
}
And here the output. 这里是输出。
cur in main(): 18857008 value: 0
cur in main(): 18857012 value: 3
cur in main(): 18857016 value: 6
cur in main(): 18857020 value: 9
cur in main(): 18857024 value: 12
cur in main(): 18857028 value: 0
the last one should be a 2 not a zero. 最后一个应该是2而不是零。 What am i doing wrong here?
我在这里做错了什么?
In your makeLListCstyle
function you do 在您的
makeLListCstyle
函数中
tail = head + numElements;
That makes tail
point to one beyond the allocated memory. 这使得
tail
指向已分配内存之外的一个 。
So in appendElement
when you do 所以在执行
appendElement
时
tail = tail+1;
you're making tail
point to one more element beyond the end of your allocated memory, leading to undefined behavior when you then write to this location. 您使
tail
点指向已分配内存末尾的另一个元素,从而在您随后写入此位置时导致未定义的行为 。
Your code is basically equivalent to 您的代码基本上相当于
int LLHead[5];
// Initialize elements 0 to 4
LLHead[6] = 2;
What you have is nowhere close to a linked list. 你有什么是无处接近链表。 All you have is a dynamically allocated array which you go out of bounds of.
您所拥有的只是一个动态分配的数组,您可以超出范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.