How to free the memory if memory is allocation using memset
-
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
-
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
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.
-
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
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] -
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
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
#includeusing namespace std;
int main()
{
char *tmp = (char*)malloc(16);
memset(tmp, 1, 16);
free(tmp);
return 0;
} -
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
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.