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 to free the memory if memory is allocation using memset

How to free the memory if memory is allocation using memset

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 5 Posters 0 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.
  • H Offline
    H Offline
    hema_soft
    wrote on last edited by
    #1

    Hi, How to free the memory if memory is allocation using memset. suppose, ulong m_ularr[]; //declaration in header file //In constructor we allocate memory as memset(m_ularr, 0, sizeof(m_ularr)); Kindly help me how to free the memory in destructor for this one.

    Freda

    N C S L 4 Replies Last reply
    0
    • H hema_soft

      Hi, How to free the memory if memory is allocation using memset. suppose, ulong m_ularr[]; //declaration in header file //In constructor we allocate memory as memset(m_ularr, 0, sizeof(m_ularr)); Kindly help me how to free the memory in destructor for this one.

      Freda

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      memset() doesn't do any allocations. Your array is allocated on the stack and will not require any deallocations. Only when allocating memory with an allocation method such as new/alloc/malloc/or other platform specific allocation methods, you will have to deallocate accordingly. memset() only writes data to a piece of memory that should allready have been allocated by you if you've done it properly.

      1 Reply Last reply
      0
      • H hema_soft

        Hi, How to free the memory if memory is allocation using memset. suppose, ulong m_ularr[]; //declaration in header file //In constructor we allocate memory as memset(m_ularr, 0, sizeof(m_ularr)); Kindly help me how to free the memory in destructor for this one.

        Freda

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Before worrying about memory deallocation, you should make sure you're writing (memset just writes) on previously allocated memory (in your code memory is never allocated). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • H hema_soft

          Hi, How to free the memory if memory is allocation using memset. suppose, ulong m_ularr[]; //declaration in header file //In constructor we allocate memory as memset(m_ularr, 0, sizeof(m_ularr)); Kindly help me how to free the memory in destructor for this one.

          Freda

          S Offline
          S Offline
          Software_Developer
          wrote on last edited by
          #4

          The free function deallocates a memory block that was previously allocated by a call to malloc(). memset() Sets buffers to a specified character http://msdn.microsoft.com/en-us/library/aa246471(v=VS.60).aspx[^] free() Deallocates or frees a memory block. http://msdn.microsoft.com/en-us/library/we1whae7(VS.71).aspx[^]

          #include
          #include

          using namespace std;

          int main()
          {
          char *tmp = (char*)malloc(16);
          memset(tmp, 1, 16);
          free(tmp);
          return 0;
          }

          1 Reply Last reply
          0
          • H hema_soft

            Hi, How to free the memory if memory is allocation using memset. suppose, ulong m_ularr[]; //declaration in header file //In constructor we allocate memory as memset(m_ularr, 0, sizeof(m_ularr)); Kindly help me how to free the memory in destructor for this one.

            Freda

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You dont need to free this memory, it is static. You only need to free heap memory which you allocate with a new, or malloc, or alloc. (Use delete free and free respectively). Or of course if you use smart pointers, which automatically free when they stack container goes out of scope, there isnt a need for a free, but thats getting a bit beyond your question.

            ============================== Nothing to say.

            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