-1

Assume the following functions

void func (int* p);

int main(){
   int* arr = new Object[10];
   int* obj = new Object;

   func(arr);
   func(obj);
   
   return 0;
}

When you call the function for arr, does func() create a new pointer, p, that is a pointer to the first element of arr? And in that case, does p need to be deleted at the end of the function to prevent a memory leak?

When you call the function for obj, does func() create a new pointer, p, that needs to be deleted?

If func is now defined as void func(int*& p), will it still work for arrays?

I want to ensure that I don't create accidental memory leaks when I write code

Adam Smith
  • 39
  • 3
  • 1
    _"does func() create a new pointer, p, that needs to be deleted?"_ No, it just takes a copy of the existing pointer variable (not the values behind it!). – πάντα ῥεῖ Jun 22 '23 at 17:55
  • 1
    Q#1: The `p` parameter is pass-by-value from the callsite's provided `arr` argument. Q#2: Assuming the policy of "raw pointers are non-owning pointers", then **no**, the `func` routine does not `delete` anything — as *ownership* is **not** being transferred. Q#3: Same policy assumption as Q#2, and same answer: **no**. Q#4: **Yes**, but since the length is not being passed in as parameter, `func` will have no way to determine how big the array is, and if the `p` alias is modified, there's a leak. – Eljay Jun 22 '23 at 17:57
  • When we say "delete a pointer", that is technically incorrect. The statement `delete ptr;` actually deletes what `ptr` points to, no the pointer itself. And when you pass a parameter, you just get a second pointer pointing at the same place, not a second set of heap memory. – BoP Jun 22 '23 at 17:57
  • 2
    This is not really how we do arrays anymore ("C" style pointer decay). In C++ use `std::vector arr(10);` and pass the dynamically allocated array to func like this `void func(std::vector& values)`. I think you should move to more current C++ teaching material. – Pepijn Kramer Jun 22 '23 at 17:58
  • `func` gets a copy of the pointer that it's called with. A pointer points at memory; copying a pointer doesn't copy the memory. So, no, you don't need to do anything extra just because you copied the pointer. – Pete Becker Jun 22 '23 at 17:58
  • Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jun 22 '23 at 17:58
  • 1
    Just to give you a sneak peek of the "C++ core guidelines" : [avoid calling new/delete explicitly](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly). And [prefer using stl array or vector instead of "C" array](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array) – Pepijn Kramer Jun 22 '23 at 18:04
  • you seem to confuse deleting the pointer with deleting the pointee. Colloquially when we say "delete the pointer" we actually mean "delete the pointee". When you copy a pointer no copy of the pointee is made. No matter how often you copy the pointer, you still need to `delete` the pointee only once – 463035818_is_not_an_ai Jun 22 '23 at 18:07
  • 1
    Um, using `new Object` for a integer pointer is going to cause some issues. – Chipster Jun 22 '23 at 18:45
  • 1
    @drescherjm "*Avoid using `new`, Instead use `std::vector` and **shared** pointers from the standard library*" - *shared* should be *smart* instead :-) – Remy Lebeau Jun 22 '23 at 19:39
  • A function receiving a pointer can only guarantee that the pointer is pointing to a single item, provided the content of the pointer is valid. If you need to pass consecutive values to a function, use a container like `std::vector`. – Thomas Matthews Jun 22 '23 at 20:21

1 Answers1

0

There is no memory leak, since the function takes a pointer as an argument, inside the function you are working with the same memory area.

You should use delete only paired with new

               // here just creating pointer on stack which just points to the same memory block
               // but on this pointer not creating(copy) new C-array 
void func (int* p)  
{
    delete p; // If you delete an object inside the function.
              // then writing delete in main you will get an error
}


void func2 (int* p)  
{
    /* some code where we not deleting p */
}


int main(){
   int* arr = new Object[10];
   int* obj = new Object;

   func(arr);
   delete arr // ERROR: error for object 0x1f5c5080: pointer being freed was not allocated

   func2(obj);
   delete obj; // OK
   
   return 0;
}
Akri
  • 1
  • 1