Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How it works? (About the memory which allocate by function)

How it works? (About the memory which allocate by function)

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performance
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2907
    wrote on last edited by
    #1

    We often can see that some function will return a pointer to a structure or a buffer, it must be aollocated by function itself. The question is how the memory is freed? and when it is freed? Can I aollocate some memory in a function and hope the caller will clean up the mass? I am using C, so the C++ destruction function is not what I want. Thanks

    C E 2 Replies Last reply
    0
    • U User 2907

      We often can see that some function will return a pointer to a structure or a buffer, it must be aollocated by function itself. The question is how the memory is freed? and when it is freed? Can I aollocate some memory in a function and hope the caller will clean up the mass? I am using C, so the C++ destruction function is not what I want. Thanks

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      As long as you tell the programmer calling your function that he/she has to free memory that you are allocating, there's nothing wrong with what you describe. Also, you should tell the caller which function to use when freeing the memory (free, delete, delete [] , GlobalFree, etc.). I do it all the time... -c

      1 Reply Last reply
      0
      • U User 2907

        We often can see that some function will return a pointer to a structure or a buffer, it must be aollocated by function itself. The question is how the memory is freed? and when it is freed? Can I aollocate some memory in a function and hope the caller will clean up the mass? I am using C, so the C++ destruction function is not what I want. Thanks

        E Offline
        E Offline
        Erik Funkenbusch
        wrote on last edited by
        #3

        You often see this? You shouldn't. That's very poor programming style. There are a few cases in the C Runtime library where you see this, but what happens here is that the C runtime library allocates the memory and cleans it up when the program exits. Generally, memory should be copied, or passed to a function through a pointer or reference, and that memory should be used by the function. Additionally, COM utilizes a mechanism where the Caller has to clean up memory when using SysAllocString and IMalloc. These rules are clearly documented though. you can't just assume people will clean up your allocations.

        U 1 Reply Last reply
        0
        • E Erik Funkenbusch

          You often see this? You shouldn't. That's very poor programming style. There are a few cases in the C Runtime library where you see this, but what happens here is that the C runtime library allocates the memory and cleans it up when the program exits. Generally, memory should be copied, or passed to a function through a pointer or reference, and that memory should be used by the function. Additionally, COM utilizes a mechanism where the Caller has to clean up memory when using SysAllocString and IMalloc. These rules are clearly documented though. you can't just assume people will clean up your allocations.

          U Offline
          U Offline
          User 2907
          wrote on last edited by
          #4

          First, I would like to thank both of you for your help. I am programming a set of netbios support function, one of which need return a structure pointer. and I need call it several times, yesterday i find a way to do this, that is use the 'static' modifier. then there will be only one copy of the structure and it will initialize when the program start and clean up when the program exit. although it will keep it's content through function calls, but that is not a matter. It's the solution that the C library used? by the way, I am sorry about my poor english, I am still a student. mask

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups