![]() |
|
|
|||||||
![]() |
![]() |
![]() |
![]() |
Mark Forums Read |
| Programming :: Programming related discussions :: |
![]() |
|
|
LinkBack | Thread Tools |
|
#17
|
|
Here's the mistake.
// you can edit this function. cDriver* myFunc() { cDriver D; <- cDriver object D is a local variable and statically allocated, cDriver* pD = &D; therefore the class destructor is called when the function pD->age = 27; goes out of scope. return pD; } Solution. cDriver* myFunc() { cDriver D; cDriver* pD = new cDriver; pD->age = 27; return pD; } Lesson: Never return a pointer to a statically allocated object. It gets destroyed! |
||||
|
#18
|
|
![]() Variables declare inside the function are not always called "STATIC VARIABLES". take note! the keyword here is STACK, and what's the opposite of stack? its called HEAP. so to make it simple.... when you are working with variables, think about STACK and HEAP. STACK = it is automatically release after the end of your function. ![]() HEAP = stays on the program not unless you will use the keyword "delete". ![]() by DEFAULT..take note.. it is DEFAULT that the variable you declare inside the function is consider as Static Variables, placed on the STACK which will be destroyed... think about POP remember? STACK is all about PUSH and POPBUTTTTTTTTT.... you don't want your variable to be destroyed at the end of your function so what you will gonna do is to place them on HEAP 10 QUESTION AND ANSWER: 1. So there two possible solutions here? YES, its either using keyword new and malloc. 2. What pointers do? pointers point the location of your object, so you can get the value. 3. Then why my pointers destroyed? Coz you put them on stack. 4. Is it possible that my Pointer on STACK but it points somewhere on HEAP? In reality, your pointer is just next to your allocated object. if your object is on HEAP your pointer is on HEAP too. ![]() 5. Is it possible that my Pointer place on HEAP and pointing on HEAP? Absolutely Yes. 6. How about my Pointer on HEAP and points on STACK?, Like what I said, pointers is located at the beginning of your allocated object which could be at HEAP or STACK. ![]() 7. Now I'm confuse... so both Pointer and my Objects have memory location? YES! ![]() 8. Can you explain my simple code again. Code:
int * My_Pointer;
My_Pointer = new int(100); //integer object @ Heap with value of 100.
delete My_Pointer;
9. Whaaaaaaaaaat ![]() ![]() Didn't I use the keyword "delete" ? then why my pointer not destroyed? Do I have to wait at the end of the scope? FYI, you didn't destroyed the pointer, you destroyed or deallocate the memory block to where your pointer is pointing. In fact, there are a lot of religious fight for that... programmers tend to assign the pointer to NULL or 0 after they delete the allocated memory. example.Code:
delete My_Pointer; My_Pointer = 0; 11. Can I ask more question? NO, I put 10 only, tired and sleepy... ![]() ![]() SUMMARY: ERROR: cDriver* myFunc() { <---- function should return a pointer cDriver D; <---- object is place on stack. cDriver* pD = &D; <---- pD is pointing to D (D will destroyed at the end of scope) pD->age = 27; <------ you assign 27 to D which is on the STACK. return pD; <------ your return the pointerk which point to D, but D is on the STACK. } D is already destroyed before it reach to another function. SOLUTION: cDriver* myFunc() { cDriver D; cDriver* pD = new cDriver; <------ placed on HEAP. cannot be destroyed unless use "delete". pD->age = 27; <------ set value 27 on age located on HEAP. return pD; <------ pD is never destroyed... } back to our main: int main(int argc, char *argv[]){ cDriver* d; d = myFunc(); .. cout << "Display Age: " << d->age << endl; .. delete d; <---- delete the allocated object on HEAP. } ![]()
|
|||||
|
#19
|
|
I'm talking 'bout the class destuctor that's why I think the object is destroyed outside the function (when it is not dynamically allocated) and you got those undesired output. Thanks for the explanation, very well explained!
|
||||
|
#21
|
|
|
||||
![]() |
| Bookmarks |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A hard headed boy, know how to solve his problem? | mr_kyme | Family Matters | 21 | 09-18-2009 09:27 AM |
| solve weight problem...i can help you through | tabsy | Fitness & Health | 3 | 06-12-2006 07:18 PM |
| VB gurus... I need your expertise... problem solve | afortaliza | Programming | 14 | 05-23-2006 09:38 PM |
| PROBLEM SOLVE ****mods pls close**** | koto | Computer Hardware | 5 | 12-27-2005 08:42 PM |