hmm... it seems nobody is doing C++ project ^^
some people don't want to eat apples, but some make it a living![]()
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!
I think I have to add this for the sake of those who are still not clear.
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 DEFAULTthat 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 POP
BUTTTTTTTTT.... you don't want your variable to be destroyedat the end of your function so what you will gonna do is to place them on HEAP
It is commonly known as Dynamically allocated objects, and that is using keyword new and malloc.
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.
My_Pointer is a pointer to a integer value of 100 located on STACK (by default ok?), it will only be destroyed when your function reach at the end of the scope.Code:int * My_Pointer; My_Pointer = new int(100); //integer object @ Heap with value of 100. delete My_Pointer;
9. WhaaaaaaaaaatDidn'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.
10. Why like that? it is by design, ask bjarne or read his article: http://www.research.att.com/~bs/bs_faq2.html#delete-zeroCode: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.
}
![]()
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!Naa pud ko nakat-onan hehe...
woaaah.. heavy nosebleed.
IT ko peru i hate programming. wahahahahaha
Similar Threads |
|