-10

I know if I create a pointer in the header file, I should always delete it when the destructor is being called, but what about if I create a pointer inside of a function. I know basic variables get destroyed at the end of the block, is it the same for pointers?

For example:

Class::Function()
{
    int i = 3; // This gets destroyed after the function ends
    int* j = 5; // What about this? Do I have to delete it somewhere to keep from a leak?
}

If I initialize j inside of the constructor, I would say delete j; to prevent leaks, etc. Is there something I should do in this case?

Drew Dormann
  • 50,103
  • 11
  • 109
  • 162
Vince
  • 2,214
  • 9
  • 34
  • 66

2 Answers2

2

Assigning int value to pointer

int* j = 5;

is illegal because you are storing int to int*. Anyway you can cast it

int* j = reinterpret_cast<int*>( 5 );

but dereferencing this pointer would lead to undefined behavior, since you dont know where does that pointer point.


You should init pointers like that

int* j = nullptr;

Since c++11 you cant create instance of nullptr_t and assign it.

nullptr_t initPointer;
int* j = initPointer;

If you dont use new operator to assign memory to pointer, you can't delete this pointer, it would lead to undefined behavior. Otherwise if you use new you need matching delete or you would get memory leak. If you want to check your program have memory leaks, check this thread and choose one tool. I can recommend valgrind.

kocica
  • 6,172
  • 2
  • 11
  • 34
  • Addendum: On a modern system with virtual memory, every process has it's own virtual address space. A pointer will be in the scope of the program, even if no actual storage has been assigned to the address. You don't have to worry about taking out the kernel. If you don't have virtual memory support for one reason or another... Cry havoc and let loose the frobweeszleqrrrtxzzt – user4581301 Aug 16 '17 at 20:26
  • Thank you for pointing out! Iam familiarized with logical to physical address conversion and paging, i dont know where I came to it :/ thank you. Fixed. – kocica Aug 16 '17 at 20:34
  • Sorry, but I have miscommunicated my point. Your initial wording was good. It just needed some expansion to cover the virtual memory case. What you described still happens with a great many systems. – user4581301 Aug 16 '17 at 21:00
  • No problem. I think ill leave it as it is, its described enough. Just its a really bad idea to assign integral to pointer. – kocica Aug 16 '17 at 21:03
0

Every call to new needs a matching call to delete. If you have more new's than delete's, you get memory leaks. If the opposite, you get double deletes. In your case, if you never called new, then there's no need for delete!

There are tools out there to help match your new's and deletes. My personal favorite is cppcheck. It's super quick and easy to use, and it runs on the c++ source code! It generally does a good job at catching unmatched new and delete calls.

scohe001
  • 13,879
  • 2
  • 28
  • 47