Results 1 to 10 of 10

Thread: Dynamic Array

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Dynamic Array

    can someone teach me how to create a dynamic array in C/C++/VC++ Juz like what we use in VB ReDim

    regards,

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Dynamic arrays must be allocated on the heap, which means you need to use pointers, new and delete[]

    The pointer is used to store the position where the array is allocated, new [datatypename] allocates data and returns a pointer to it for instance

    int* a= new int[3];

    allocates and array of 3 integers and stores the pointer in a.
    then you use a as if it was an array;

    a[2]=4;
    cout << a[2] << endl;

    and when you're done with the array you need to delete it, because since it's not on the stack, it won't be automatically deleted either.

    delete[] a;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Sc0rp
    Guest
    Note that if you wish to resize the array you need to copy its contents to a temporary array, delete[] the array, reallocate the memory for it with the new size and copy the contents of the temporary array to the newly allocated memory.

  4. #4

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    is there a way I can copy a array or structure with size 50 at one go instead of loop through each element?

  5. #5
    Sc0rp
    Guest
    You could do something like:
    PHP Code:
    inttheArray = new int[50];
    inttempArray;

    // fill the array with something.....

    int newSize 70;

    tempArray theArray;
    theArray = new int[newSize];

    for (
    int i 0newSizei++)
        
    theArray[i] = tempArray[i];

    delete [] tempArray
    This should work. I don't think there is a way out of looping.

  6. #6
    Sc0rp
    Guest
    Found a way without looping!

    PHP Code:
    #include <iostream>
    using namespace std;

    int main()
    {
        
    int newSize;
        
    int curSize 50;
        
    inttheArray = new int[curSize];
        
    inttempArray;

        
    // fill the array with something.....

        
    newSize 70;

        
    tempArray theArray;
        
    theArray = new int[newSize];

        
    memcpy(theArraytempArraycurSize sizeof(int));
        
    newSize curSize;

        
    delete [] tempArray;
        return 
    0;


  7. #7
    Destined Soul
    Guest
    I guess that could be called without looping, but if you look at a lower level, every value has to be copied.

    Does anyone know if memcpy actually faster than an element by element copy?

    Destined

  8. #8
    Sc0rp
    Guest
    No, atually it's the exact same speed because what memcpy does is loop and copy.

  9. #9
    Megatron
    Guest
    If you want something similar to ReDim Preserve, then you could use a vector.
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	// Creates an array of integers (replace int with
    	// the type of data you want to use)
    	vector<int> var;
    
    	// myval = amount to resize to
    	var.resize(myval);
    
    	// Element = element you want to change
    	var[element] = 343 
    	return 0;
    }

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    A vector is freaking 16 bytes
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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