0

I would expect pFirst to always point to the same place in the address. However it looks like pFirst moves along with pCurrent even though the function only entered the Else statement once.

Note:code is creating a linked list .

void Push(T data) {
    ++_size;
    Data d = Data(data);
    if (_pCurrent != nullptr) _pCurrent->SetNext(&d);
    else _pFirst = &d;

    _pCurrent = &d;
}
  • 4
    The pointer `&d` is not valid after the function returns because it points to a local variable. – interjay Nov 18 '21 at 13:32
  • Yes , d is created locally so it does not exist after the function is over , Debug error is a littl bit strange ,because the program initializes a new element at the same address so I did not immediately understand exactly what is happening. – Nemanja Pavlovic Nov 18 '21 at 13:33
  • *Yes , d is created locally so it does not exist after the function is over* -- Did you understand this before posting your question, or just now after the comment by @interjay was made? – PaulMcKenzie Nov 18 '21 at 13:35
  • I realized at the same time(whne comment was made i was in debuging). It was quite confusing in the debugger because it initialized the objects at the same address. – Nemanja Pavlovic Nov 18 '21 at 13:38
  • In either case you will have to remove the usage of `&d` since the pointer is invalid after the function ends. – drescherjm Nov 18 '21 at 13:39
  • yes ,& is out now is working as expected . – Nemanja Pavlovic Nov 18 '21 at 13:44
  • *"However `pFirst` moves along with `pCurrent`"* -- please provide evidence of this, or else I will assume you debugged by looking at the pointed-to data (`*pFirst`) instead of the pointer (`pFirst`). See also [mre]. – JaMiT Nov 18 '21 at 13:53
  • Since d was a local variable the program would just redo the new pointer with the next elements entered so pFirst and pCurent would point to the same place in memory. PFirst did not track pCurrent. they were initialized at the same address as the previous element that was deleted. – Nemanja Pavlovic Nov 18 '21 at 14:03

2 Answers2

1

d is created locally, so it does not exist after the function is over. Debug error is a little bit strange, because the program initializes a new element at the same address, so I did not immediately understand exactly what is happening.

This is the working version:

void Push(const T data) {

Data*d = new Data(data);
++_size;
if (_pCurrent != nullptr)
    _pCurrent->_pNext = d;
else
    _pFirst = d;
   _pCurrent = d;

}

  • On a totally unrelated note: Also see https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier. Symbols beginning with an underscore followed by an upper-case letter (like e.g. _pFirst) are reserved in all scopes and situations. – digito_evo Nov 18 '21 at 14:53
  • 1
    _pFirst is an underscore followed by a lowercase letter – mike Nov 18 '21 at 15:20
  • Small nitpick: I would not increment `_size` first, in case `new` fails. And `data` should be passed around by const reference, unless `T` supports move semantics and the `Data()` constructor is using `std::move()` to transfer `data`'s content. – Remy Lebeau Nov 18 '21 at 18:03
  • Hi,T does not supports move , it should be Const , and incrementing of _size should be most deffinitly done after we are sure we have created new element in List . Great advices thank you . – Nemanja Pavlovic Nov 18 '21 at 18:15
-1

Data should be a static variable. The code should be like this:

Data d;
void UpdateData(Data newData) {
    //Do something on d using newData;
}

void Push(T data) {
        ++_size;
        UpdateData(data);
        if (_pCurrent != nullptr)
            _pCurrent->_pNext = d;
        else
            _pFirst = d;
           _pCurrent = d;
    }
ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 1
    Clearly this code is creating a linked list. Your solution would have all nodes in the list point to a single node in memory. That will not work. – Remy Lebeau Nov 18 '21 at 17:55
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 18 '21 at 18:53