Reputation:
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";
}
Upvotes: 0
Views: 129
Reputation: 11
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);
}
Upvotes: -2
Reputation: 3
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.
Upvotes: 0
Reputation: 3911
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 (i.e. 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.
Upvotes: 1
Reputation: 70931
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.
Upvotes: 5
Reputation: 42924
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 (e.g. std::vector
, std::string
, etc.) instead.
Upvotes: 0
Reputation: 108
If you don't want to use unique/shared pointers, you could use a constructor for allocating and a destructor for automatically freeing memory.
Upvotes: 0