|
-
Aug 22nd, 2001, 01:52 AM
#1
Thread Starter
PowerPoster
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,
-
Aug 22nd, 2001, 04:50 AM
#2
transcendental analytic
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.
-
Aug 22nd, 2001, 11:57 AM
#3
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.
-
Aug 22nd, 2001, 12:04 PM
#4
Thread Starter
PowerPoster
is there a way I can copy a array or structure with size 50 at one go instead of loop through each element?
-
Aug 22nd, 2001, 12:17 PM
#5
You could do something like:
PHP Code:
int* theArray = new int[50];
int* tempArray;
// fill the array with something.....
int newSize = 70;
tempArray = theArray;
theArray = new int[newSize];
for (int i = 0; i < newSize; i++)
theArray[i] = tempArray[i];
delete [] tempArray;
This should work. I don't think there is a way out of looping.
-
Aug 22nd, 2001, 12:23 PM
#6
Found a way without looping! 
PHP Code:
#include <iostream>
using namespace std;
int main()
{
int newSize;
int curSize = 50;
int* theArray = new int[curSize];
int* tempArray;
// fill the array with something.....
newSize = 70;
tempArray = theArray;
theArray = new int[newSize];
memcpy(theArray, tempArray, curSize * sizeof(int));
newSize = curSize;
delete [] tempArray;
return 0;
}
-
Aug 22nd, 2001, 12:56 PM
#7
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
-
Aug 22nd, 2001, 01:02 PM
#8
No, atually it's the exact same speed because what memcpy does is loop and copy.
-
Aug 22nd, 2001, 01:36 PM
#9
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;
}
-
Aug 22nd, 2001, 01:49 PM
#10
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|