iSTORYA.NET

Go Back   iSTORYA.NET > Science & Technology > Software & Games > Programming
: :

Register FAQ Members List Calendar Mark Forums Read

Programming :: Programming related discussions ::

Reply
LinkBack Thread Tools
  #16  
Old 10-20-2009, 05:41 PM
Junior Member
nullpointer is offline
nullpointer's Avatar
Join Date: Sep 2008
Posts: 83
Default

Most great softwares are written in C/C++.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17  
Old 11-01-2009, 05:42 AM
Newbie
sydcanem is offline
Join Date: Apr 2009
Gender: Male
Posts: 3
Arrow

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18  
Old 11-01-2009, 03:06 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

Quote:
Originally Posted by sydcanem View Post
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 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 POP

BUTTTTTTTTT.... 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 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.

Code:
            int * My_Pointer;
            My_Pointer = new int(100); //integer object @ Heap with value of 100.
            delete My_Pointer;
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.

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;
10. Why like that? it is by design, ask bjarne or read his article: http://www.research.att.com/~bs/bs_faq2.html#delete-zero

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.
}



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19  
Old 11-02-2009, 12:29 AM
Newbie
sydcanem is offline
Join Date: Apr 2009
Gender: Male
Posts: 3
Default

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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20  
Old 11-05-2009, 11:40 PM
Newbie
greenpanda is offline
Join Date: Nov 2008
Posts: 21
Default

woaaah.. heavy nosebleed.
IT ko peru i hate programming. wahahahahaha
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #21  
Old Today, 02:23 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

Quote:
Originally Posted by sydcanem View Post
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...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT +8. The time now is 05:08 PM.


Powered by vBulletin® Version 3.7.6
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
(c) 2002-2009 iSTORYA.NET | Design by DrE | Modifications by BeoR