Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22
  1. #11

    Quote Originally Posted by MarkCuering View Post
    can you share your complete code? this might open a new topic, maybe you got some idea of using malloc, you're working with JAVA? I thought you will gonna use the one that specify the number of objects to allocate instead of bytesize to allocate. AFAIK, malloc returns a NULL in case there's no room for data to be allocated.

    you almost got it anyway on this kind of solution.
    i dont have one. in fact, i dont have a c++ compiler
    i thought this is a guessing game! lewlz. shame on me.

  2. #12
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Quote Originally Posted by doomsweek View Post
    I don't know C++ but know a little C and Java. Here's my solution.

    With a big help from Stack Overflow.
    There you go... that explains everything anyone could summarize the lesson learned? or anyone can give using malloc?
    Last edited by MarkCuering; 10-15-2009 at 07:21 AM.

  3. #13
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    hmm... it seems nobody is doing C++ project ^^

  4. #14
    hirap talaga ng C++

  5. #15
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    some people don't want to eat apples, but some make it a living

  6. #16
    Most great softwares are written in C/C++.

  7. #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!

  8. #18
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    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.
    }



  9. #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! Naa pud ko nakat-onan hehe...

  10. #20
    woaaah.. heavy nosebleed.
    IT ko peru i hate programming. wahahahahaha

  11.    Advertisement

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

 
  1. Looking For: P.regalis the smaller and cheaper the better
    By kingnat_revivingcenter in forum Pets
    Replies: 4
    Last Post: 04-22-2010, 09:28 PM
  2. ESCAPE THE FATE and BLESS THE FALL
    By achilles127 in forum Music & Radio
    Replies: 5
    Last Post: 12-20-2009, 08:39 PM
  3. What Is The Problem With The Philippines and Your Solution?
    By slimer in forum General Discussions
    Replies: 58
    Last Post: 06-24-2008, 08:09 AM
  4. Laws are imposed upon the poor and not the rich
    By iloveyou4ever in forum Politics & Current Events
    Replies: 9
    Last Post: 06-05-2006, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top