Use of delete

Hi,

I'm trying to free some memory taken by structures.

In short, the structure is define like this:
1
2
3
struct GenericStructure {
double x,y,z;
}

Pretty simple.

Then I create a
GenericStructure *temp = new GenericStructure(x,y,z);
and delete it with
delete temp;

It fails because it's supposedly an illegal access. I was under the impression that a delete on a structure pointer always succeeds unless it doesn't point to the structure.

It's supposedly a write to adress 00000008?

Edit: So it appears to delete the structure after all, because in the debugger the value of the structure is indeed ??? after the error. For some reason delete wants to write as well?
Last edited on
It sounds like you're messing up your pointers at some point. Post more code.
Messing up my pointers? In such that they no longer point towards the structure? Unfortunately, I'm 100% sure that each pointer I try to delete points to a valid structure, because before deleting it I actually use one of its values.
Apparently this piece of code changes loc_ptr:
&(*new GenericStructure(0,0,-1)+*loc_ptr)
(not always though, just sometimes)
Last edited on
That line is hideous. Also, it has a memory leak. Though I don't see how that would change loc_ptr, unless there is a "loc_ptr = " on the left side of it.
Last edited on
I just rewrote the line by not using any structure at all (just doing x+x, y+y and z+z and saving the results in a new structure) which worked.

However, it seems my "delete loc_ptr" isn't actually freeing up much at all.
Was it allocating much? Either way, I don't think you can expect the deleted memory to immediately return to OS.
All right. So apparently I was making a lot of ill use of '*new'.

It caused 3 doubles to be allocated (and not deleted) about 6 times for about 1000000 times. About 1GB of frozen memory. Great.

I'm going to return to trying the proper use of delete, etc... but as for now there still seems to be a leak.

BTW: if the deleted memory is not immediately returned, are we speaking clock cycles or several seconds before it can be used again?
Last edited on
I don't know how exactly memory allocation works. I'm certain it varies between library implementations and operating systems. My point was that memory might be already free and reusable even if task manager says that you're still using it. Memory allocation may have a more complex process than "take from OS" and "give to OS". To write good code it is enough to trust the intelligence of whoever wrote your system. If you really want to know more, you'll have to find yourself an article on that somewhere. http://en.wikipedia.org/wiki/C_dynamic_memory_allocation#Implementations might be a good place to start.
I note you are not defining an assignment constructor?

The automatically generated member functions are:
- default constructor
- copy constructor
- destructor
- operator=

The compiler does not generate any other constructors, so I am unclear about what your code is doing.

Is one of x,y,z = 8?
Last edited on
¿why are you using dynamic allocation?
¿why are you using dynamic allocation?

Because I have to run calculations on a dynamic amount of structures.

I note you are not defining an assignment constructor?

I was making wrong use of an existing structure. I don't really understand your question, but there is a constructor available to initialize GenericStructure with given values x,y,z.

Just another question, say I have:
1
2
3
4
5
void method() {
  GenericStructure temp = new GenericStructure();
  submethod(&temp);
  GenericStructure temp2 = new GenericStructure();
}


Does temp need to be deleted explicitly at the end of the method? And what about temp2?

Do they count as local variables, or is it always necessary to delete all local structures?
Last edited on
All memory allocated with new needs to be freed with delete. That is, there must be one delete for each new.

The pointer variables are local and the memory they use (4 bytes, for 32-bit pointers) is on the stack and is freed automatically. But the memory they point to is on the heap and is not automatically freed.
Last edited on
Thank you, that means that both temp as temp2 need to be deleted, but if I had made a variable
GenericStructure* temp3 = &temp;
I can ignore that one?

That ought to free up a lot of memory ;)

Hold on, if I do:
1
2
3
GenericStructure *temp = new GenericStructure();
GenericStructure *temp1 = new GenericStructure();
GenericStructure temp2 = *temp+*temp1;


Do I need to delete temp2? Because I need a pointer to that structure. Is it fine if I delete &temp2?
Last edited on
temp2 was never allocated with new, so it can't be deleted.
or is it always necessary to delete all local structures?
If you want local structures, then use local structures.
GenericStructure temp; that's an object, it will die at the end of its scope.

Because I have to run calculations on a dynamic amount of structures.
¿so you need a variable size array? try std::vector

If you show your actual code, maybe we could help
The memory leak has been solved :)

If you show your actual code, maybe we could help
I prefer to keep things general. So I can actually learn it myself and apply in several specific cases. In this case the vague questions and code examples led to a solution.
Topic archived. No new replies allowed.