简体   繁体   中英

Do I have to delete memory in this case?

Here is a simple program using dynamic memory.

My question is do I have to delete the memory at the and or the struct will take care of it for me?

#include <iostream>
struct Student {
  int grade;
  char *name;
};

void print(Student name);
int main() {
  Student one;
  one.grade = 34;
  one.name = new char[12];
  int i;
  for (i = 0; i < 11; ++i) {
    one.name[i] = 'a' + i;
  }
  one.name[i] = '\0';
  print(one);

  delete[] one.name;
  return 0;
}
void print(Student name) {
  std::cout << name.name << " has a score of " << name.grade << "\n";
}

There is a simple rule of thumb- for each call of new you should have one call of delete. In this case you should delete one.name like so : delete [] one.name .

Of course you should do that after you no longer need its value. In this case this is immediately before the return.

Memory allocated dynamically using new or malloc must be freed up when you're done with it using delete or free otherwise you'll get Memory leak .

  • Make difference between delete and delete[] : the first without subscript operator is used to deleted dynamic memory allocated with new for a pointer. The latter is used for deleting an array allocated dynamically.

  • So in your case:

     one.name = new char[12]; // an array of 12 elements in the heap delete[] one.name; // freeing up memory char* c = new char('C'); // a single char in the heap delete c; 
  • Don't mix new, delete with malloc, free :

This is undefined behavior, as there's no way to reliably prove that memory behind the pointer was allocated correctly (ie by new for delete or new[] for delete[]). It's your job to ensure things like that don't happen. It's simple when you use right tools, namely smart pointers. Whenever you say delete, you're doing it wrong.

如果您不想使用唯一/共享的指针,则可以使用构造函数进行分配,并使用析构函数自动释放内存。

If the raw pointer in your structure is an observing pointer, you don't have to delete the memory (of course, someone somewhere in the code must release the memory).

But, if the raw pointer is an owning pointer, you must release it.

In general, every call to new[] must have a matching call to delete[] , else you leak memory.

In your code, you invoked new[] , so you must invoke delete[] to properly release the memory.

In C++, you can avoid bug-prone leak-prone raw pointers, and use smart pointers or container classes (eg std::vector , std::string , etc.) instead.

Creating a Structure does not mean it will handle garbage collection in C++ there is no garbage collection so for every memory you allocate by using new you should use delete keyword to free up space. If you write your code in JAVA you wont have to delete as garbage collector will automatically delete unused references.

Actually the structure won't freeup or delete the memory that you have been created. if in case you want to freeup the space you can do as follows.

#include <iostream>
using namespace std;
int main()
{
    char *c = new char[12];
    delete[] c;
    return(0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM