Reuse allocated memory

S

Santh

Hi,
I have a bulk of data available in safearray pointer. I want to
copy that data to another pointer. I tried using malloc for new
variable and memcopy. But due to virtual memory restrictions the
allocation failed. This is because the system memory can not allocate
another volume of memory.
My requirement is , without allocating the new memory, how can I copy
the data from safe array to my variable. Since after copying I dont
need the safearray data anyway. so during memcopy process itself it can

be cleared. So that with less free memory i should be able to copy the

whole content from safe array to my long pointer variable. Or even I
can use the same data in safearray memory to my new pointer and
safearray poiner can be cleared.


This is my sample code for your reference. if u can help, more
appriciated.
The malloc statement fails because there is not enough memory to
allocate. But my thought is, the safearray holding a data can be copied

to my m_Dataptr and cleared with the help of available memory. Hope you

got my requirement.


/////////////code/////////////­///////////////
HRESULT CWave::CopySafeArrayData(SAFEA­RRAY** psa)
{
///.....


if (!*psa)
return S_OK;
// check the number of dimensions
if ((*psa)->cDims != 1)
{
ATLTRACE("CWave::CopySafeArray­Data() Error :
E_DSPALGS_INVALID_ARGS_TYPE");
return E_DSPALGS_INVALID_ARGS_TYPE;
}
// check the number of elements
long num = (*psa)->rgsabound[0].cElements­;
if (num == 0)
return S_OK;


long* pdata;


HRESULT hr = SafeArrayAccessData(*psa, (void**)&pdata);
if (FAILED(hr)) {
return hr;
}


m_dataptr = malloc(num*m_datatypesize);


if (!m_dataptr)
{
::MessageBox(0, "Mem Not allocated", "error",MB_OK);


ATLTRACE("CDspWave::CopySafeAr­rayData() Error :
E_DSPALGS_FAILED_ALLOCATE_MEMO­RY");
SafeArrayUnaccessData(*psa);
return E_DSPALGS_FAILED_ALLOCATE_MEMO­RY;
}


memcpy(m_dataptr, pdata, num*m_datatypesize);
SafeArrayUnaccessData(*psa);


//....
//....



}


//////////////End of
code//////////////////////////­/////////////////////////

-Thanks
Santhakumar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top