Question regarding malloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #1

    Question regarding malloc

    consider following function

    f(char *p)
    {
    if p has allocated the memory previously then
    free p;
    else
    return;
    }

    I guess You understood the problem.
    How do I know that any pointer has allocated the memory?
  • Firecore
    New Member
    • Jul 2007
    • 114

    #2
    wouldnt something like

    [CODE]
    if(pointer != NULL)
    {
    do stuff;
    }

    work?

    Im a noob @ C++ but give it a go.
    Cuz i think if a pointer points to null its not pointin at anything.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by ashitpro
      consider following function

      f(char *p)
      {
      if p has allocated the memory previously then
      free p;
      else
      return;
      }

      I guess You understood the problem.
      How do I know that any pointer has allocated the memory?
      You can't do that in an implementation independent way. You could fiddle diddle
      a bit with the brk() or sbrk() system calls (if present) but that's about it.

      kind regards,

      Jos

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Originally posted by JosAH
        You can't do that in an implementation independent way. You could fiddle diddle
        a bit with the brk() or sbrk() system calls (if present) but that's about it.

        kind regards,

        Jos
        How can I use these system calls in this context?
        example?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          When you have a pointer in a function, keep in mind:

          1) if you free it you may crash because the pointer was not allocated using malloc().
          2) if it was allocated by malloc() and you don't free it, then the memory never gets released and you have a memory leak
          3) it it was allocated by malloc() and you free it, then if there is another copy of the pointer elswhere in the program, then you will crash over there trying yo use the pointer to deleted memory.

          Memory management requires tools far beyond simple malloc() and free() calls.

          Comment

          • Firecore
            New Member
            • Jul 2007
            • 114

            #6
            Originally posted by weaknessforcats
            Memory management requires tools far beyond simple malloc() and free() calls.
            Could you elaborate on these tools?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by ashitpro
              How can I use these system calls in this context?
              example?
              You might try to use this:

              [code=c]
              char* lowm= sbrk(0); // low water mark;

              void* anyPointer= ... // pointer value to be checked.

              if ((char*)anyPoin ter >= lowm) // it's an allocated value
              [/code]

              kind regards,

              Jos

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Firecore
                Could you elaborate on these tools?
                In C++ you use handle classes. See the article about this in the C/C++ Articles section.

                In C you would write a function to create a count on the heap. Then you would make a copy of the pointer (like a function call), you boos this count and when a copy disappers to delete this count. If the count is zero, you free the pointer.

                In C++ this is automatic. In C this is a manual operation and just one slip and the count is off. This is very hard to to in C and is always unrelaible.

                Issues also occur with multi-threading where the pointers are copied on separate threads. More code in C but in C++ critical sections can be used in the handle class where the counts are bieng adjusted.

                Comment

                Working...