0

Trying to update to a pointer from a function return. Just for background this is a template that acts like the stl vector. This is the returning function.

    ////////////////////////////////////////////////////////////////////////////////
//removes an item from the array
const T& remove(int pos)
{
    if(pos > cnt)
        pos = cnt;
    if(pos < 0)
        pos = 0;
    static T v;
    for(int i,k = 0; i < cnt; i++,k++)
    {
        if(i == pos)
        {
            v = element[i];
            i++;
        }
        else
            element[k] = element[i];
    }
    cnt--;
    return v;
}

/////////////////////////////////////////////////////////////////////////

The pointer variable I am trying to update:

TVector<Member*> members;
Member* backmember;

backmember = members.remove(members.size()-1);

but backmember always returns null. I am sure I am missing something simple, just not sure what. Any ideas? Let me know if you have any questions and thanks in advance.

4
  • 1
    Just curious, why can't you use en.wikipedia.org/wiki/Erase-remove_idiom instead of writing your own method. Commented Nov 23, 2011 at 4:05
  • remove returns a const T&. Your backmember takes a pointer, which is not the same thing as a reference. It also takes a non-const pointer. So that's two ways that this code is wrong. Are you sure this is the code in question? How does this compile, let alone return NULL? Commented Nov 23, 2011 at 4:07
  • Making a static copy of the object to return is a bad idea - it means two threads can't access different arrays at the same time. If you want to return the removed value, you should do it by value. Commented Nov 23, 2011 at 4:09
  • Yes I understand guys this is not my normal programming style. I had to fill in the functions and the return types have to match. I know the general rule is not to post assignment work to stackoverflow, but I knew it was something simple I was overlooking. This is what happens when you have a java instructor teaching C. :) Commented Nov 23, 2011 at 4:17

2 Answers 2

6

I don't think this initializes i:

for(int i,k = 0; i < cnt; i++,k++)

Try changing it to this and testing:

for(int i = 0,k = 0; i < cnt; i++,k++)
Sign up to request clarification or add additional context in comments.

1 Comment

It's possible that the OP is under the impression that int i,k = 0 initialises both i and k, whereas it only initialises k to 0. Of course, this is exactly what you've just said, I just wanted to state it more verbosely :) Also, I personally find for loops containing multiple variables to be something to avoid wherever possible. They're too hard to read and understand at a glance.
2
for(int i,k = 0; i < cnt; i++,k++)

i,k uses comma operator, which evaluate arguments in order, returning result of last one. So this means "declare i, then declare k and set it to 0"

i is not set. So most likely it get some large random value from memory and your loop never run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.