Do I need to delete pointer which don't use dynamic memory?

Do I need to delete pointer which don't use dynamic memory(new)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;
  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
}


Is there memory leak using mypointer? What will happen if I delete mypointer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;
  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  delete mypointer;
  return 0;
}


Mypointer is here MORE "REFERENCE TOOL" THAN "DYNAMIC MEMORY TOOL". Am I right about this?
Last edited on
No.
Don't call delete on something you didn't call new on.
Topic archived. No new replies allowed.