0

I ran into the following problem: I want to return a Y-coordinate, and after I returned it, it needs to be deleted.

Is there a way to do this, because I think I'm missing some major things here.

void setY(int y)
    {
        this->y = new(int); //pointing to new address
        *this->y = y; //change value of pointer
    }

    int getY()
    {
        return *y; //give the coordinate
        delete y; //what I want
        *y = NULL; //what I want
    }

Thank you in advance. If the question is not well described, please let me know.

user3208216
  • 84
  • 1
  • 8
  • 1
    Why in the blazes is `y` a pointer to a single integer? – StoryTeller - Unslander Monica Apr 23 '14 at 10:01
  • store the value in a temporary. also, what the heck are you trying to accomplish? c++ has value-semantic, use that to your advantage – sp2danny Apr 23 '14 at 10:02
  • Thanks for your fast response. For a school project we needed to use pointers. So I am learning with pointers and stuff. Although I am getting the hang of it, this is something I don't get. – user3208216 Apr 23 '14 at 10:03
  • 3
    @user3208216 You don't get it, because this is not how this language feature should be used. If something is hard to do or seems awkward it often is not how something should be done. – pmr Apr 23 '14 at 10:05
  • @pmr Well, that makes sense, indeed. Actually I thought that this was the purpose. Thank you very much for your help – user3208216 Apr 23 '14 at 10:08
  • if the assignment calls for using a pointer, `new` and `delete`, for something not specified in your question, you'd better *quote the assignment text*. – Cheers and hth. - Alf Apr 23 '14 at 10:15

4 Answers4

3

Simply change your data member type fromj int* to plain int.

Then store the value there, and return the value, no new stuff.

Additional tip: a get prefix is useful in a language with introspection such as Java or C#, but is not neccessary (just verbiage) in C++.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thanks for your fast response. So you recommend not using a pointer? I passed the value from the *int to a regular int, so I can delete the and NULL the pointer. But I think that the whole idea of a pointer is then lost in this program. I am still learning C++ and this is a school exercise, so any further explanation would be much appreciated :) – user3208216 Apr 23 '14 at 10:05
3

Notwithstanding the design flaws, the simplest thing to do is to take a copy:

int getY()
{
    int ret = *y;
    delete y; //what I want
    /*No assignment to deferenced y is possible - you've deleted it*/
    y = nullptr; /*a further delete on y will be benign*/
    return ret;
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

Since you say you really do need to muck around with pointers as part of a learning exercise, you need to read the return value before deleting:

int result = *y;
delete y;
y = nullptr;    // NOT *y - that's been deleted
return result;

(If you're stuck with a historic version of C++, then write NULL or 0 instead of nullptr. nullptr is better though - it would have prevented the mistake in your code).

You should also check y before dereferencing it, and fix the memory leak you get when calling setY more than once. Since the class is managing a separate resource, you'll also need to carefully follow the Rule of Three to avoid further memory errors.

Hopefully, the point of this exercise is to show you how difficult pointer-juggling can be, and how much easier it is to use more idiomatic techniques - in this case, simply storing an int directly.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thank you for your response. This is indeed what I need to do, since I need to use pointers. Your answer helped me the most, because of a well explained answer which is in line of my exercise. Thank you! – user3208216 Apr 23 '14 at 10:16
  • Better phrased than my answer, just want to append that you should add a `nullptr` check before dereferencing the pointer, if you dereference a `nullptr` you are not going having a good time. – Casper Beyer Apr 23 '14 at 10:22
  • @CasperVonB: Indeed, you should check `y` before dereferencing it, as the answer says. – Mike Seymour Apr 23 '14 at 10:23
  • @MikeSeymour Could've sworn that said "you should also check `y` before setting it to fix the memory leak you get when calling `setY`", i stand corrected. – Casper Beyer Apr 23 '14 at 10:32
0

Something like this

int getY()
{
   int value = -1;

   if (y_ptr) {
      value = *y_ptr;
      y_ptr = nullptr;
   }

   return value;
}

However i must add that this is just plain abuse of pointers, don't do it.

Casper Beyer
  • 2,203
  • 2
  • 22
  • 35