Dynamic Memory Allocation-Urgent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuvanand1
    New Member
    • Oct 2007
    • 22

    #1

    Dynamic Memory Allocation-Urgent

    I am writing a program for portable devices hence memory management is a big constraint.

    To my knowledge only if u free the dynamically allocated memory it can be used further..

    My doubt is
    I have a function which is returing a pointer.
    pointer is dynamically allocated .....if I free the pointer before returning then it is useless or dangling pointer problem arises....or if I free after returning the ptr the control is lost.........

    So when to free the memory......... ...


    eg
    :
    char* example (int )
    {
    char *filepath,*file name;
    filepath=(char* )malloc(100));


    return filepath; ///Where to free the filepath memory
    }
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    .if I free the pointer before returning then it is useless or dangling pointer problem arises....or if I free after returning the ptr the control is lost
    Yes, very good, you see why passing pointers are a pain. You have a pointer that indicates no ownership semantics. This is how C is. Maybe you could come up with some GC or memory management system, but...either way you still have ugly pointers lying about.

    Really, this is how C is. You're stuck passing around pointers. Rely on some good programming practices and a good design and you'll survive the pointer mess.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by oler1s
      Yes, very good, you see why passing pointers are a pain. You have a pointer that indicates no ownership semantics. This is how C is. Maybe you could come up with some GC or memory management system, but...either way you still have ugly pointers lying about.

      Really, this is how C is. You're stuck passing around pointers. Rely on some good programming practices and a good design and you'll survive the pointer mess.
      ... as they attempt to do in Objective C: define an owner for an allocated piece
      of memory. The owner is responsible for the memory, i.e. it should delete/free
      it when the memory is no longer needed.

      Allocated memory can change ownership (as in the OP's original example) and
      the new owner takes the responsibility for the memory. This 'conceptual'
      ownership helps you out in that tangled mess of pointers and allocated memory
      except for circular references; a third conceptual owner has to be defined for
      the entire circular shebang.

      kind regards,

      Jos

      Comment

      • anuvanand1
        New Member
        • Oct 2007
        • 22

        #4
        Originally posted by JosAH
        ... as they attempt to do in Objective C: define an owner for an allocated piece
        of memory. The owner is responsible for the memory, i.e. it should delete/free
        it when the memory is no longer needed.

        Allocated memory can change ownership (as in the OP's original example) and
        the new owner takes the responsibility for the memory. This 'conceptual'
        ownership helps you out in that tangled mess of pointers and allocated memory
        except for circular references; a third conceptual owner has to be defined for
        the entire circular shebang.

        kind regards,

        Jos

        I am new to c...so kindly help me in defining a owner for a memory

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by anuvanand1
          I am new to c...so kindly help me in defining a owner for a memory
          It's all a conceptual notion so you can blurr or ignore it whatever you want. An owner
          creates and/or deletes allocated memory when it doesn't expose it to others or
          when the others just use the memory, as in:

          [code=c]
          char* f() {
          char* cp= malloc(42);
          /* do something with cp[0], cp[1], ... */
          g(cp); * expose it to something else */
          /* but keep ownership */
          /* do something with cp[0], cp[1], ... */
          return cp; /* pass ownership to something else */
          }
          void g(char* cp) {
          /* do something with the memory but don't release it */
          /* do something with cp[0], cp[1], ... */
          /* g() is not the owner of it */
          }
          void h() {
          char* cp= f();
          /* now h() is the owner of the memory */
          /* do something with cp[0], cp[1], ... */
          /* so if it's done with it, release it */
          free(cp);
          }
          [/code]

          In this little scenario f() was the owner of the memory but it gave ownership of it
          to function h() when it returned; h() frees the memory at the end because it was
          the new owner of it; function g() just used the memory so it doesn't free it because
          it wasn't the owner.

          In C you have to keep track of the 'ownership' yourself; C allows you to shoot
          yourself in the foot big times when you don't take care. In the example above it
          may seem trivial but in larger real world code things might get tricky. Keeping
          track of that ownership might help you to keep things organized.

          kind regards,

          Jos

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Why is C a requirement? If you can use C++, then you can use handles and avoid this mess.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by weaknessforcats
              Why is C a requirement? If you can use C++, then you can use handles and avoid this mess.
              Yep, and if the OP had used Java the garbage scavenger had taken care of it all.

              kind regards,

              Jos ;-)

              Comment

              Working...