CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Preallocate memory for CByteArray

  1. #1
    Join Date
    Nov 2010
    Posts
    54

    Preallocate memory for CByteArray

    Hi,

    I am using CByteArray as a buffer in Visual C++. I want to preallocate nSize memory for for CByteArray, so that later when I try to change the buffer, by calling SetSize, Add, Remove, etc., as long as all these operations are within nSize, CByteArray will not try to release the memory or reallocate the memory, so to eliminate the possibility of memory fragments in heap. Is that possible?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,578

    Re: Preallocate memory for CByteArray

    Quote Originally Posted by AlanCCC View Post
    Hi,

    I am using CByteArray as a buffer in Visual C++. I want to preallocate nSize memory for for CByteArray, so that later when I try to change the buffer, by calling SetSize, Add, Remove, etc., as long as all these operations are within nSize, CByteArray will not try to release the memory or reallocate the memory, so to eliminate the possibility of memory fragments in heap. Is that possible?
    Isn't that what SetSize does?

  3. #3
    Join Date
    Nov 2010
    Posts
    54

    Re: Preallocate memory for CByteArray

    Quote Originally Posted by GCDEF View Post
    Isn't that what SetSize does?
    If I using the following codes:

    CByteArray Buf;

    Buf.SetSize(65536); // preallocate buffer

    ...

    ...

    for (i = 1; i < 65536; i++)

    {

    Buf.SetSize(i); // will that free the preallocated memory and reallocte a new buffer of size 1?

    for (j = 0; j < i; j++)

    Buf[j] = j;

    }

    In the above Buf.SetSize(i) code, will it free the preallocated memory and reallocate a new block of memory from heap?

    Also if Buf.SetSize(0) after calling Buf.SetSize(65536), then will that free the preallocated memory completed?


    Thanks

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,280

    Re: Preallocate memory for CByteArray

    Can't you step into the source code and find out?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,578

    Re: Preallocate memory for CByteArray

    What's the point of calling SetSize inside your loop?

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Preallocate memory for CByteArray

    SetSize doesn't just "preallocate", it changes the size of the actual array so it contains that many accessible elements.
    CByteArray doesn't provide for preallocation without actually changing the array.

    The documentation on SetSize states:
    "If the new size is smaller than the old size, then the array is truncated and all unused memory is released. "
    However, this isn't the current implemented behaviour.
    * SetSize(0) will cause a deallocation
    * SetSize(x) with x > maxsize will cause a realloc to increase the databuffer.
    * SetSize(x) where x!=0 and x<maxsize, will currently retain the databuffer. Considering the documentation however, don't assume that this will always be the case (or has always been the case) if you expect your code to still work on other versions of MFC.

    If you need a specific behaviour... you may want to create your own array class.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)