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.

Is there any functions or api's or your idea available? Please let me
know.

-Thanks in advance
santhakumar
 
J

Jens.Toerring

Santh said:
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.

Sorry, but that sentences don't make sense. First of all, what is a
"safearray pointer"? And what means "copy that data to another poin-
ter"? Do you mean "copy to a location a different pointer points to"?
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.

Why then copy at all? Just make your variable (a pointer, I guess) point
to where "safearray" is or what it points to (whatever it is).
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.

Is "safearray" an array or a pointer? And means "can be cleared" that
you e.g. zero out all elements of an array or that you set a pointer
to NULL or deallocate memory it points to?
Is there any functions or api's or your idea available? Please let me
know.

Sorry, but please try to rephrase this a bit, perhaps with some
code for illustration, I find it nearly incomprehensible.

Regards, Jens
 
S

Santh

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(SAFEARRAY** psa)
{
///.....

if (!*psa)
return S_OK;
// check the number of dimensions
if ((*psa)->cDims != 1)
{
ATLTRACE("CWave::CopySafeArrayData() 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::CopySafeArrayData() Error :
E_DSPALGS_FAILED_ALLOCATE_MEMORY");
SafeArrayUnaccessData(*psa);
return E_DSPALGS_FAILED_ALLOCATE_MEMORY;
}

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,813
Messages
2,569,696
Members
45,479
Latest member
QFZErin313

Latest Threads

Top