When the memory needed for an array increases...

This is a discussion on When the memory needed for an array increases... within the C Programming forums, part of the General Programming Boards category; ...at runtime, what are you supposed to do? Obviously appending more memory to the end of the array is imposible ...

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    When the memory needed for an array increases...

    ...at runtime, what are you supposed to do? Obviously appending more memory to the end of the array is imposible since you can't allocate memory at any specific address. Allocating another larger array and then copying the contents over would work, but it would be horribly slow and would limit the size of the array to less than half the availible memory. An array of arrays would also work, but it would be somewhat limitting in the fact that memory would have to be allocated in fixed size blocks. Still, it seems like the only practical thing to do. Does anyone know of any other methods? Also, if an array of arrays is the only solution, is there a code library that can handle the required memory management so you can access data in a contegenous manner? In the past when I needed something like this I wrote it myself, to help me get better at C, but I'm getting to the point where I'm beyond the need for that so I'd like to focus on writing code that doesn't already exist . Plus I'd probobly never get it as optimised as a library created with some ASM code.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    malloc(), realloc(), realloc(), realloc(), free().

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Hmm... well that does seem like a simple soulution, but how exactly does realloc() behave? I assume, based on the description in the MSDN library, that if there is enough room to extend the memory block it does so, and if not then it moves the memory to where there is room to extend it. To be honest, if the latter behavior were to be executed there would be serious problems in terms of speed. If you are constantly needing to allocate new memory in a situation such as reading data in from an input buffer, and allocating memory as neccesary, this would be ridiculously inefficient. Therefore, an array of arrays is still preferable to realloc(). So once again, does anyone know of a code library for this?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Xzyx987X
    Therefore, an array of arrays is still preferable to realloc(). So once again, does anyone know of a code library for this?
    There are no resizable arrays in C. Use malloc as suggested. All "other methods" will be doing this exact same thing in one way or another. You have to. The only way to resize or get dynamic memory is to malloc it. It's just the way it is.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. 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

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