new and delete, why does this work?
-
Hi, Going over some code in a multithreaded library I came across a snippet that bewilers me, and I was hoping one of you can make sense of it for me. It is part of a multi-threaded application. First there a global structure declared.
struct CustomStruct
{
CWnd* pCwnd;
int* pInt;
long* plong;
}These are the same declarations but I can't recall them. But they are all pointers to some value. Then in one member function part of a class there is this call.
...
CustomStruct* pCS=new CustomStruct;pCS->pCwnd=AfxGetMainWnd();
pCS->Int=&IntVariable;
pCS->plong=&LongVariable;CreateThread(...(Pass the above struct address as LPVOID)
And in the static function for the thread we have
CustomStruct* pCS=(CustomStruct*)wParam;
CWnd* pCwnd=pCS->pCwnd;
int* pInt=pCS->pInt;
long* plong=pCS->plong;delete pCS;
How does the delete portion of the code work? I don't understand how we can delete the structure here and still have access to the new pointers we declared in the function? How can we delete the structure and still have the data stored in the pointer still be valid? Did we not get rid of that data with delete? I'm confused please explain. Thanks in Advance. Sam C ---- Systems Manager Hospitality Marketing Associates
-
Hi, Going over some code in a multithreaded library I came across a snippet that bewilers me, and I was hoping one of you can make sense of it for me. It is part of a multi-threaded application. First there a global structure declared.
struct CustomStruct
{
CWnd* pCwnd;
int* pInt;
long* plong;
}These are the same declarations but I can't recall them. But they are all pointers to some value. Then in one member function part of a class there is this call.
...
CustomStruct* pCS=new CustomStruct;pCS->pCwnd=AfxGetMainWnd();
pCS->Int=&IntVariable;
pCS->plong=&LongVariable;CreateThread(...(Pass the above struct address as LPVOID)
And in the static function for the thread we have
CustomStruct* pCS=(CustomStruct*)wParam;
CWnd* pCwnd=pCS->pCwnd;
int* pInt=pCS->pInt;
long* plong=pCS->plong;delete pCS;
How does the delete portion of the code work? I don't understand how we can delete the structure here and still have access to the new pointers we declared in the function? How can we delete the structure and still have the data stored in the pointer still be valid? Did we not get rid of that data with delete? I'm confused please explain. Thanks in Advance. Sam C ---- Systems Manager Hospitality Marketing Associates
As you noticed, the temporary structure just contains a window handle and pointers to other variables. Once these values are copied from the structure, it can be deleted. The original variables that the pointers referenced still exist. Tim Smith Descartes Systems Sciences, Inc.
-
Hi, Going over some code in a multithreaded library I came across a snippet that bewilers me, and I was hoping one of you can make sense of it for me. It is part of a multi-threaded application. First there a global structure declared.
struct CustomStruct
{
CWnd* pCwnd;
int* pInt;
long* plong;
}These are the same declarations but I can't recall them. But they are all pointers to some value. Then in one member function part of a class there is this call.
...
CustomStruct* pCS=new CustomStruct;pCS->pCwnd=AfxGetMainWnd();
pCS->Int=&IntVariable;
pCS->plong=&LongVariable;CreateThread(...(Pass the above struct address as LPVOID)
And in the static function for the thread we have
CustomStruct* pCS=(CustomStruct*)wParam;
CWnd* pCwnd=pCS->pCwnd;
int* pInt=pCS->pInt;
long* plong=pCS->plong;delete pCS;
How does the delete portion of the code work? I don't understand how we can delete the structure here and still have access to the new pointers we declared in the function? How can we delete the structure and still have the data stored in the pointer still be valid? Did we not get rid of that data with delete? I'm confused please explain. Thanks in Advance. Sam C ---- Systems Manager Hospitality Marketing Associates
Think about CustomStruct as a disposable paper bag. Inside there are three things: a pointer to CWnd, a pointer to int, and a pointer to long. You take these things out of the bag, and get rid of the bag. 'delete' destroys only a memory block that holds three pointers. These three pointers remain perfectly valid. Tomasz Sowinski -- http://www.shooltz.com
-
Think about CustomStruct as a disposable paper bag. Inside there are three things: a pointer to CWnd, a pointer to int, and a pointer to long. You take these things out of the bag, and get rid of the bag. 'delete' destroys only a memory block that holds three pointers. These three pointers remain perfectly valid. Tomasz Sowinski -- http://www.shooltz.com
Thanks for some reason I was thinking it "destroyed" everything. So only the pointers are deleted but not the actual value stored in memory. So if these weren't pointers then we would have a "memory" leak problem? Say for instance we remove the CWnd, but keep int and long we call delete would those values be removed from memory? Sam C ---- Systems Manager Hospitality Marketing Associates
-
As you noticed, the temporary structure just contains a window handle and pointers to other variables. Once these values are copied from the structure, it can be deleted. The original variables that the pointers referenced still exist. Tim Smith Descartes Systems Sciences, Inc.
Thanks. I get it now, but what if we change the pointers to be straight variables. For example, remove the CWnd* and change the other to to long and int (instead of long* and int*) then by deleting the structure this time we remove those variables from memory? And the above example I gave will no longer be valid? Thanks for helping out I didn't have pointer problems or memory allocation problems using VB :-) Sam C ---- Systems Manager Hospitality Marketing Associates
-
Thanks for some reason I was thinking it "destroyed" everything. So only the pointers are deleted but not the actual value stored in memory. So if these weren't pointers then we would have a "memory" leak problem? Say for instance we remove the CWnd, but keep int and long we call delete would those values be removed from memory? Sam C ---- Systems Manager Hospitality Marketing Associates
You're almost there! Yes, when 'delete' is called on the struct/class, the members are destroyed. If those members are pointers, then the pointer is destroyed, but not what is pointed to. If you have no other pointer to the 'pointed to' object, then you have a memory leak (remember - you can have multiple pointers pointing to the same memory address/object). If you want the things that are 'pointed to' to be destroyed when you call 'delete', you write a destructor that does this. As a general rule (there are always exceptions, and the code you are looking at is one of those exceptions) if a class/struct contains pointers, it should also have a destructor that 'cleans up' the pointed to objects. And a copy constructor and an assignment operator - but that's getting a little too complicated for now! So far, so good (I hope). If the members are just 'builtin types' like int, then when delete is called the int is destroyed. However, in your code you copied the int value from the struct just before calling delete. Therefore, the 'int' in the struct is removed from memory by the call to delete, but the copy you made still remains valid, becuase it is a different copy of the int value from the struct. Er... I think that's what I'm trying to say! ----------------------- Reg : "Well, what Jesus blatantly fails to appreciate is that it's the meek who are the problem."
-
Think about CustomStruct as a disposable paper bag. Inside there are three things: a pointer to CWnd, a pointer to int, and a pointer to long. You take these things out of the bag, and get rid of the bag. 'delete' destroys only a memory block that holds three pointers. These three pointers remain perfectly valid. Tomasz Sowinski -- http://www.shooltz.com
I think the easy way to get your head around pointers is to think of them as addresses. the RAM (memory) is a street, and every house on that street has an address (a pointer). If you want to tell your friend to go to a certain house, you write the address on a piece of paper, and you give him the paper (you have just passed a pointer, in a customstruct(on paper)), then he goes out and meets another friend, who also wants the addresses that he writes in his notebook (gets a copy of the pointer). Now your friend decides he doesn't need the address any more so he throws away the paper (deletes the custom struct, but the memory(house) still exists). As long as the house exists the address is valid, but if the house burns down (your object is deleted from memory) the address will point to an empty lot (invalid memory). I hope this helps, this analogy helped me understand pointers:) --- Blessed are those who can laugh at themselves, for they shall never cease to be amused :laugh:
-
I think the easy way to get your head around pointers is to think of them as addresses. the RAM (memory) is a street, and every house on that street has an address (a pointer). If you want to tell your friend to go to a certain house, you write the address on a piece of paper, and you give him the paper (you have just passed a pointer, in a customstruct(on paper)), then he goes out and meets another friend, who also wants the addresses that he writes in his notebook (gets a copy of the pointer). Now your friend decides he doesn't need the address any more so he throws away the paper (deletes the custom struct, but the memory(house) still exists). As long as the house exists the address is valid, but if the house burns down (your object is deleted from memory) the address will point to an empty lot (invalid memory). I hope this helps, this analogy helped me understand pointers:) --- Blessed are those who can laugh at themselves, for they shall never cease to be amused :laugh:
-
Hi, Going over some code in a multithreaded library I came across a snippet that bewilers me, and I was hoping one of you can make sense of it for me. It is part of a multi-threaded application. First there a global structure declared.
struct CustomStruct
{
CWnd* pCwnd;
int* pInt;
long* plong;
}These are the same declarations but I can't recall them. But they are all pointers to some value. Then in one member function part of a class there is this call.
...
CustomStruct* pCS=new CustomStruct;pCS->pCwnd=AfxGetMainWnd();
pCS->Int=&IntVariable;
pCS->plong=&LongVariable;CreateThread(...(Pass the above struct address as LPVOID)
And in the static function for the thread we have
CustomStruct* pCS=(CustomStruct*)wParam;
CWnd* pCwnd=pCS->pCwnd;
int* pInt=pCS->pInt;
long* plong=pCS->plong;delete pCS;
How does the delete portion of the code work? I don't understand how we can delete the structure here and still have access to the new pointers we declared in the function? How can we delete the structure and still have the data stored in the pointer still be valid? Did we not get rid of that data with delete? I'm confused please explain. Thanks in Advance. Sam C ---- Systems Manager Hospitality Marketing Associates
Just wanted to give everyone a blanket THANKS. I finally am getting a good grasp of everything that is being said about pointers. I thought I had a good grasp before, but that creating a structure and then deleting it in another function kind of killed me :-) But thanks to all for being patient and taking the time to help me understand this one concept better. Sam C ---- Systems Manager Hospitality Marketing Associates