Reuse allocated memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Santh

    #1

    Reuse allocated memory

    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

  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Reuse allocated memory

    Santh <bsanthu@gmail. com> wrote:[color=blue]
    > 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.[/color]

    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"?
    [color=blue]
    > 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.[/color]

    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).
    [color=blue]
    > 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.[/color]

    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?
    [color=blue]
    > Is there any functions or api's or your idea available? Please let me
    > know.[/color]

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

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Santh

      #3
      Re: Reuse allocated memory

      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::CopySafe ArrayData(SAFEA RRAY** psa)
      {
      ///.....

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

      long* pdata;

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

      m_dataptr = malloc(num*m_da tatypesize);

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

      ATLTRACE("CDspW ave::CopySafeAr rayData() Error :
      E_DSPALGS_FAILE D_ALLOCATE_MEMO RY");
      SafeArrayUnacce ssData(*psa);
      return E_DSPALGS_FAILE D_ALLOCATE_MEMO RY;
      }

      memcpy(m_datapt r, pdata, num*m_datatypes ize);
      SafeArrayUnacce ssData(*psa);

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

      }
      //////////////End of
      code///////////////////////////////////////////////////

      -Thanks
      Santhakumar

      Comment

      • pete

        #4
        Re: Reuse allocated memory

        Santh wrote:[color=blue]
        >
        > This is my sample code for your reference.[/color]
        [color=blue]
        > /////////////code////////////////////////////
        > HRESULT CWave::CopySafe ArrayData(SAFEA RRAY** psa)
        > {
        > ///.....[/color]

        news:comp.lang. c++

        --
        pete

        Comment

        Working...