Results 1 to 11 of 11

Thread: dynamic arrays

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    dynamic arrays

    How do you add or subtract elements from an array dynamically while preserving the other elements?
    (In VB it was Redim Preserve)
    thanks

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't really. There are workarounds however.

    The first and easiest is to use a dynamic array class in the first place. This only works in C++ as C doesn't have classes (duh!). A very good one comes with every C++ compiler and is called vector.
    Code:
    // Usage of vector:
    #include <vector>
    using namespace std;
    
    // declare an initially empty dynamic array of ints:
    vector<int> arInts;
    // declare a dynamic array of floats with an initial size of 5:
    vector<float> arFloats(5);
    
    // general:
    // vector<type> name[(initial size)];
    Look up your compiler's reference to see what member functions vector offers.
    vector grows as necessary to accomodate more elements.

    There's another way, but it is more complicated. You can allocate memory on the heap to get an array:
    C++:
    Code:
    int *array = new int[size];
    C:
    Code:
    int *array = (int*)malloc(size*sizeof(int));
    To enlargen or shrink this array without losing your values you must first save them, allocate a new array and copy the values.
    oldsize is the current size of the array, newsize is the new wanted size of the array.
    C++:
    Code:
    int *temp = array;
    array = new int[newsize];
    memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
    delete[] temp;
    C:
    Code:
    int *temp = array;
    array = (int*)malloc(newsize*sizeof(int));
    memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
    free(temp);
    _min might not be defined, it can be defined like this:
    C++:
    Code:
    template <typename T>
    inline T _min(T a, T b)
    {
      return a < b ? a : b;
    }
    C:
    Code:
    #define min(a,b) \
      (((a) < (b)) ? (a) : (b))
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Simple One:

    Code:
    #include "stdafx.h"
    #include <malloc.h>
    
    int main(int argc, char* argv[])
    {
    	int* z = (int*)calloc(10, sizeof(int));
    	z[1] = 10;
    	z = (int*)realloc(z, sizeof(int) * 12);
    	z[11] = 15;
    	printf("Z[1] = %d\nZ[11] = %d\n", z[1], z[11]);
    	free(z);
    return 0;
    }
    VS.NET 2003

    Need to email me?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh yeah, forgot realloc. realloc is simply a wrapper for this part:
    int *temp = array;
    array = (int*)malloc(newsize*sizeof(int));
    memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
    free(temp);

    except that it determines oldsize programmatically.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    i don't think your methods would work with me, though, because my arrays are arrays of a custom class.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use vector<> then.

    Your classes each need to support various things in order to use them with vector<>.

    You need to define operator=, operator==, and the constructor, default constructor, and copy-constructor.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    == for vector? Why?


    i don't think your methods would work with me, though, because my arrays are arrays of a custom class.
    Maybe. That depends on what the default constructor does.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh, ignore that one. If you try and do much *with* the vector afterwards, it'll probably try and use it.

    And VC6 has some bugs RE operator use.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And quite a lot of bugs RE templates...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They're the same ones. It has difficulty deciding which implicit operators are needed for which template functions.

    For example, with vector, you don't need operator< unless you actually use any of the sorting functionality.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    i'm unfamiliar with vector. could someone point to a good tutorial?
    thanks,
    jmiller

Posting Permissions

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



Click Here to Expand Forum to Full Width