append dynamic memory or something

This is a discussion on append dynamic memory or something within the C++ Programming forums, part of the General Programming Boards category; hi... I was just wondering if there are ways to append or withdraw dynamically allocated memory. Say I've got an ...

  1. #1
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161

    append dynamic memory or something

    hi...
    I was just wondering if there are ways to append or withdraw dynamically allocated memory.
    Say I've got an array:

    int *array;
    array = new int[20000];

    and assign values to the array. If I now want the array to contain
    21000 elements with the first 20000 elements being the same, do I have to allocate a buffer to contain the 20000 elements, then delete the original buffer, allocate a new:

    array = new int[21000];

    and assign the first 20000 elements, delete the buffer and assign values to the 1000 last elements, or(I guess you see where this is going) can I just append 1000 elemnts to the original array.

    The buffer technique, for large arrays, can cause considerable memory problems! although I guess an append function would also run across some problems.
    Anyway, are there such functions?

    thanks
    /aronn

  2. #2
    Unregistered
    Guest
    this is the func for reallocating the memory use it.

    void *realloc( void *memblock, size_t size );

    for further doubts refer msdn

    or contact memory expert:
    vimalrathore@myrealbox.com

  3. #3
    TK
    Guest
    Try using realloc().

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Except C++ new/delete do not really mix with C malloc/realloc/free.

    Allocating a new block, copying all the elements you want to keep and deleting the old block is the straight-forward way, and is just the long hand version of realloc anyway (C++ has no equivalent of realloc)

    The better way is to use a data structure which expands more naturally, and far less expensively.
    You can do this yourself with a home grown linked list.

    Or, if you have the C++ STL, consider using one of the containers like <list> or <vector>

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Salem, a linked list of this size would use far more memory than he probably wants. Remember you're talking about 21,000 pointers also. Das not gud! but you are absolutely right about vector

    Here's a lame alternative
    Code:
    int *MyRealloc(int *original, int originalsize, int newsize)
       {
       int sizeofint = sizeof(int);
       int *newbuffer = new int[newsize];
       memset(newbuffer,0,sizeofint*newsize);
       memcpy(newbuffer, original, sizeofint*((newsize>originalsize)?originalsize:newsize));
       delete [] original;
       return newbuffer;
       }

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Why would a list use much more memory?

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Originally posted by endo
    Why would a list use much more memory?
    because for each node you have a pointer. in 32 bit you are looking at uh.... 4*21000 = 84000 bytes for his list just for pointers! Plus, depending on what he's doing, allocation of a single buffer is MUCH faster than individual allocations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 10:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21