A simple question on heap objects.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebouh
    New Member
    • Feb 2007
    • 77

    A simple question on heap objects.

    Hi all. I'd like to know something. This may be silly but, if there is an array inside a heap object, where will the array reside?
    If in heap too, then how does its mem location get freed?

    I can't get my mind worked around it.
    Thanks!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Sebouh
    Hi all. I'd like to know something. This may be silly but, if there is an array inside a heap object, where will the array reside?
    If in heap too, then how does its mem location get freed?

    I can't get my mind worked around it.
    Thanks!
    If an array is a member of a structure/class object it is just part of the memory
    allocated for the entire structure/class object. If the entire thing was stored on
    the stack (think of local variables), the array will be part of it and also stored on
    the stack. If the thing resides somewhere in the heap (think of malloc or new)
    the array will be somewhere in there too.

    Here's an extremely simple example in C; consider the following struct:
    Code:
    typedef struct {
       int a;
       char b[4];
       int c;
    } simple_t;
    ...
    simple_t p*= malloc(sizeof(simple_t));
    Suppose malloc returned memory at location 0x1000. Then the struct is stored
    at location 0x1000 and p contains that value. The structure layout is:

    0x1000: a
    0x1004: 1st elem of b
    0x1005: 2nd elem of b
    0x1006: 3rd elem of b
    0x1007: 4th elem of b
    0x1008: c
    0x100C: other memory

    kind regards,

    Jos

    ps. I ignored internal/trailing padding for simplicity.

    Comment

    • Sebouh
      New Member
      • Feb 2007
      • 77

      #3
      Yes, now i remember. But how does it get freed?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Sebouh
        Yes, now i remember. But how does it get freed?
        given the example from my previous reply:
        Code:
        free(p);
        kind regards,

        Jos

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          What Jos is saying is that the array is located in the object, you delete the object, you delete the array.


          Adrian

          Comment

          • Sebouh
            New Member
            • Feb 2007
            • 77

            #6
            Originally posted by JosAH
            given the example from my previous reply:
            Code:
            free(p);
            kind regards,

            Jos
            Not p, the array.
            So what Adrian said.
            What about:
            Code:
            struct p {
                int *i = (int*)malloc(8);
            }
            Does i get freed if i free p (p being a pointer to a struct p) or that works only for arrays?
            I'm asking cause i is defined and declared at the same time like with an array.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by Sebouh
              What about:
              Code:
              struct p {
                  int *i = (int*)malloc(8);
              }
              Does i get freed if i free p (p being a pointer to a struct p) or that works only for arrays?
              I'm asking cause i is defined and declared at the same time like with an array.
              The code you posted is not valid, so I cannot answer your question. Perhaps restate your question?


              Adrian

              Comment

              • Sebouh
                New Member
                • Feb 2007
                • 77

                #8
                Originally posted by AdrianH
                The code you posted is not valid, so I cannot answer your question. Perhaps restate your question?


                Adrian
                Sorry, you're right that's invalid.
                But if i malloc in constructor, must i create a destructor to free it?

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  You should not be using malloc/free in constructors. Instead, use the new/delete of C++. malloc/free are functions that do not call constructors ordestructors on the members they are allocate or delete. Use malloc/free only in C.

                  The short answer to your question is: If you allocated, then you must delete. A destructor is a convenient place to delete because you don't have to remember to call it when your object is destroyed.

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by weaknessforcats
                    You should not be using malloc/free in constructors. Instead, use the new/delete of C++. malloc/free are functions that do not call constructors ordestructors on the members they are allocate or delete. Use malloc/free only in C.

                    The short answer to your question is: If you allocated, then you must delete. A destructor is a convenient place to delete because you don't have to remember to call it when your object is destroyed.
                    What weakness said. :)


                    Adrian

                    Comment

                    • Sebouh
                      New Member
                      • Feb 2007
                      • 77

                      #11
                      OK, thank you guys.:)

                      Comment

                      Working...