-2

After calling and using the function, I can access the parameters through the pointer. And I think the parameters are kept in memory. I'm trying to delete the parameters after using the code below.

int MyFunction(int p1,int p2)
{
    int total = p1 + p2;
    delete& p1;
    delete& p2;
    
    return total;
}

and using this

int a = MyFunction(15, 30);

(my project name) initiated a breakpoint.I get an error. How do I delete parameters ?

Scrabin
  • 1
  • 1
  • 6
    You’ll need to clarify what you’re trying to accomplish here. What do you expect the result of “deleting function parameters” to be? – Sneftel Nov 02 '20 at 21:32
  • 1
    Why would you want to delete integer numbers? – DYZ Nov 02 '20 at 21:32
  • 2
    What do you mean by "delete parameters"? What do you want to happen? – Code-Apprentice Nov 02 '20 at 21:32
  • 2
    If I remember correctly, you only need to delete pointers that have been allocated with `new` or `malloc`. As these aren't pointers, you can't delete them. – Corey Ogburn Nov 02 '20 at 21:32
  • 1
    @CoreyOgburn `delete` should only be used in conjunction with `new` and `free()` with `malloc()`. You techncially might be able to mix them (i.e. `delete` after a `malloc()`, but it's not a good practice. – Code-Apprentice Nov 02 '20 at 21:33
  • 2
    Please get a C++ book from the [recommended list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – n. m. could be an AI Nov 02 '20 at 21:35
  • @Sneftel I want it deleted from memory. I think these parameters remain in memory? – Scrabin Nov 02 '20 at 21:39
  • 1
    @Code-Apprentice • technically you **cannot** mix them. – Eljay Nov 02 '20 at 22:30
  • @Eljay I thought as much but was hedging since I didn't know for sure. – Code-Apprentice Nov 02 '20 at 22:52
  • @Scrabin What do you mean by "deleted from memory"? What are you actually trying to accomplish? Since the parameters are on the stack, their values are no longer accessible to your code. If you want the bits modified so some external attack cannot access the values, then you will most likely need to use other tools and possibly even assembler to do this. If your concern is about malicious code trying to access sensitive information, then you should describe this in your question rather than leave us guessing. Or if you are asking about this for some other reason, tell us. – Code-Apprentice Nov 02 '20 at 22:55

3 Answers3

4

Your function parameters are passed by value, so they are allocated automatically, not dynamically. You cannot delete them. Being automatically allocated, they will also be automatically deallocated when the function returns, so just skip trying to delete them and go straight to returning.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
3

You can only delete objects allocated on heap (via new).

The parameters to the function are allocated on stack before function call and the runtime "removes" them once the function exits.

(The removal just means that the stack pointer is moved to look as if the parameters no longer exist)

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
2

How to delete function parameter?

In general, you shouldn't delete arbitrary variables. You should only delete pointers that you explicitly allocated with new somewhere else in your code.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268