Thread: C++ equivalent of realloc ?

  1. #1
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226

    C++ equivalent of realloc ?

    How do i re-allocate an array in C++ ? equivalent of realloc ?

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Either create a new array with the new total of elements, copy the elements from the first and then delete the first, or use STL vector.
    zen

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I saw your other post and i would suggest that if speed is of the essence that you overload new[] and delete[].In your new new operator use global new to give you a largish chunk of memory and then you can yourself split that into chunks maybe stored in a list or a map and do the memory allocations yourself. I think a call to a function over a call to global new will give you the efficiency you are after.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    If this is in regards to your array question you posted in Gaming, I suggest you take my earlier advice and use a doubly linked list, not an array -- an array is just not a good move for what you're doing. It's much slower than a doubly linked list in all resizing, no matter how you implement it.

  5. #5
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    Hey,
    I know using arrays this way, would slow down the loading of data (world,objects etc.). But that's only done once in the beginning.

    After the data has been loaded into the arrays it can be traversed directly.
    However, on the other hand, if i use a linked list, it would require linear traversal causing a major slowdown. (traversing all entries before the required one).

    right ???

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah, but do you need random access?

    If you are just going to process a list of objects every frame or something of that nature, it's not as though you need to process the 31st object before you process the 16th, so if you traverse in order and process each in that order, any container you use will give you O(N) access.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    SilentStrike is exactly correct.

    Arrays and linked lists (properly implemented) are equally fast for sequential access.

    Think of them like a pawn and queen on a chessboard, moving in one column from one side of the board to the other. The pawn, in one step, can only move one square, while the queen can move to any of them in a single move. If you need to hit the squares in a random order (like, 7, 3, 1, 6, 4, 5, 8, 2) the queen is much faster than the pawn. If you plan to hit them in the order 1, 2, 3, 4, 5, 6, 7, 8, each is equally fast. And so it is with arrays and linked lists.

    LLs have the advantage of constant time resizing, instead of O(N).

    All it means is that instead of:
    Code:
    for (int i = 0; i < arraySize; i++){
    	array[i].DoSomething();
    }
    you use something like:
    Code:
    for (list.Start();list.IsValid();list.Next()){
    	list.Current().DoSomething();
    }
    and both are equally fast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM