1

I have a linked list like this:

typedef struct _LIST_ELEMENT
{
    T* data;
    _LIST_ELEMENT* nextItem;
    int index;

}LITEM,*P_LITEM,**PP_LITEM;

and four pointers to pointer

PP_LITEM _head;
PP_LITEM _tail;
PP_LITEM _current;
PP_LITEM _previous;

The situation is like this:

I have 4 elements in the list and _head points to the first and _tail to the last, _current and _previous work together as a cursor. After i erase element with index 2, the list contains 3 elements with indexes 0,1 and 3 and all pointers point right. At this point if i do this (to move the cursor to the start):

(*_previous) = (*_tail);
(*_current) = (*_head);

The list is missing element with index 1 but all pointers point correctly. If i do this:

_previous = &(*_tail);
_current = &(*_head);

everything is fine and nothing missing.

If anyone has any idea why is this happening please enlighten me.

Atlantrix
  • 149
  • 1
  • 8
  • 1
    `(_previous) = (*_tail)` - this shouldn't even compile! `_previous` is a pointer-to-pointer, but `*_tail` is not. I suggest turning up your compiler's warning level. – Oliver Charlesworth Apr 23 '12 at 13:26
  • 1
    More generally, it's usually considered bad practice to hide pointers behind typedefs (regardless of whether Microsoft used to do that a lot in Win32 stuff)... – Oliver Charlesworth Apr 23 '12 at 13:27
  • 1
    Why do people tend to write C and then tag the question with [tag:c++]? – Griwes Apr 23 '12 at 13:29
  • (_previous) = (*_tail) was wrong i corected – Atlantrix Apr 23 '12 at 13:30
  • because from what i know there are no templates in C – Atlantrix Apr 23 '12 at 13:33
  • Aside: `_LIST_ELEMENT` is a reserved identifier, as are all identifiers that begin with an underscore followed by a capital letter. http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – Robᵩ Apr 23 '12 at 13:34
  • If this is not a learning excercise, don't bother to build your own list: just use `std::list`. If this *is* a learning excercise, please tag it as homework. – Robᵩ Apr 23 '12 at 13:36
  • that is defined inside a template – Atlantrix Apr 23 '12 at 13:37
  • its not a learning and std::list is not good for what i need here – Atlantrix Apr 23 '12 at 13:39
  • (*_previous) = (*_tail); (*_current) = (*_head); is not working right but _previous = &(*_tail); _current = &(*_head); does and i want to know why if anyone as any idea. – Atlantrix Apr 23 '12 at 13:40
  • What do you need that std::list, std::deque, std::map or std::vector don't provide? – RedX Apr 23 '12 at 13:44
  • Note that names beginning with underscore are reserved for the implementation; you should not use names starting with an underscore. In C++ 2011, the rules are in §17.6.4.3 Reserved Names, and §17.6.4.3.2 Global names specifically: _Certain sets of names and function signatures are always reserved to the implementation: Each name that contains a double underscore `__` or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace._ – Jonathan Leffler Apr 23 '12 at 13:45
  • @user1351310, by that question why was it tagged with C++ I meant that code you posted is entirely C. No-one uses `typedef struct XXX {} YYY;` in C++, because you can just do `struct XXX {};` and use `XXX` as type name, whereas in C you had to use `struct XXX` as type name. Don't write C in C++. Choose one of them. Also - how, in the world, `std::list` isn't "good for what you need here"? – Griwes Apr 23 '12 at 14:01
  • my code is way faster, 500 ms to add 500k elements compared to 5000 for the list. – Atlantrix Apr 23 '12 at 14:01
  • Ah, just noticed that `T*`. So - you think that C++ is just `C with templates`? Go re-learn everything you know and then attempt to claim that you know C++ once again. – Griwes Apr 23 '12 at 14:06
  • typedef is for LITEM,*P_LITEM,**PP_LITEM without typedef there is no LITEM,*P_LITEM,**PP_LITEM and that struct is defined in a template that is why you see a T* inside. and i use this list on around 100 threads in parallel and its much faster then std::list, also i use it in conjunction with PPL and so far no standard containers worked for me. – Atlantrix Apr 23 '12 at 14:09
  • I have no idea what your problem is Giwes but i really don't want to know. I don't remember claiming to know c++ and from what i see you are not very knowledgeable either as you were unable to answer the question and you just comment other stuff. – Atlantrix Apr 23 '12 at 14:12

1 Answers1

4

(*_previous) = (*_tail) and _previous = &(*_tail) mean completely different things.

(*_previous) = (*_tail) means "set the value of the thing pointed to by _previous to be equal to the value of thing pointed to by _tail.

_previous = &(*_tail) is equivalent to _previous = _tail, and so it means "set _previous to point at the same thing as _tail.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • I know that but probably my question was not very clear. When i set the values the nextItem of the element with index 0 is pointing to the element with index 3, so the element with index 1 is missing. in the second case i don't have the index 1 element missing. What i don't understand is why when i set the values one element is missing. – Atlantrix Apr 23 '12 at 13:52
  • I have elem1->nextItem pointing to elem2, elem2->nextItem to elem3 and elem3->nextItem to elem1. When i do his (*_previous) = (*_tail) elem1->nextItem is pointing to elem3 and elem3->nextItem is pointing to elem1, *_tail points to elem3 and _head to elem1 so elem2 is gone. if i do this _previous = _tail everything is fine. i don't understand why in the first case elem1->nextItem is changing to point to elem3 even though i made no changes to the nextItem of any element. – Atlantrix Apr 23 '12 at 15:12