Is there a way to create dynamic arrays in C++?? If so.. how do i use them? A simple code example would be great.
Thanx, HeadExplodes.
Printable View
Is there a way to create dynamic arrays in C++?? If so.. how do i use them? A simple code example would be great.
Thanx, HeadExplodes.
Jupp there is, with pointers. It could look something like this.
#include <iostream.h>
int main()
{
int *pInt = new int[10];
int *pIncre = pInt;
for (int i = 0; i < 10; i++)
{
*pIncre = 2*i;
pIncre++; // "jumps" to the next element
}
pIncre = pInt;
for (int i = 0; i < 10; i++)
{
cout << "Value pInt[" << i << "] is " << *pIncre << endl;
pIncre++;
}
delete [] pInt; //free memory(heap)
//dont use delete pInt because it only frees the
//fist element and you get a memory leak :-(
return 0;
}
This should work good (I havent test it yet). Please reply and tell how it went.
Jupp there is, with pointers. It could look something like this.
#include <iostream.h>
int main()
{
int *pInt = new int[10];
int *pIncre = pInt;
for (int i = 0; i < 10; i++)
{
*pIncre = 2*i;
pIncre++; // "jumps" to the next element
}
pIncre = pInt;
for (int i = 0; i < 10; i++)
{
cout << "Value pInt[" << i << "] is " << *pIncre << endl;
pIncre++;
}
delete [] pInt; //free memory(heap)
//dont use delete pInt because it only frees the
//fist element and you get a memory leak :-(
return 0;
}
This should work good (I havent test it yet). Please reply and tell how it went.
Or a simpler way to assign values to array in this case would be:
for (int i = 0; i < 10; i++) pInt[i] = i*2;
This does exactly same thing as:
for (int i = 0; i < 10; i++)
{
*pIncre = 2*i;
pIncre++;
}
I totaly agree Kitten. Using vector is better in this case
(I just didnt want to mix vectors and pointers).
Okay, all of that stuff is hard coding, people. Arrays are naturally dynamic, because arrays are pointers. I highly suggest not using a pre-made vector class because you will not always have that vector class on you. Use your own knowledge.
I don't see any vectors, where?
Arrays are not naturally dynamic. An array has local scope, it is allocated on the function stack, and has to be passed into functions as an arguement, but it is passed as a pointer parameter. To dynamically allocate an array, use the 'new' function, than the array will be stored in 'heap' memory. This means that if the array is dynamically allocated in a called function, when the function terminates the memory on the heap will be reserved and can be passed safely back to the caller. On the other hand, if you define an automatic array in a function and try to pass it back to the caller, that memory will become unreserved because it was allocated on the function stack. When the function ends the stack memory is discared into the free store.
God forbid one uses standard classes, for if they must use a compiler that is 5 years old, they might not be supported.
Code re-use should be encourged IMO. No need to reinvent the wheel.
Use the vector class.
Fooling around with different data structures like arrays and linked lists etc is very good for learning but when it's time to do something serious you should use what's in the standard library.
// And btw...a little example of using the vector class
// and it's not hard to use either
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
while ( 1 )
{
cout << "Enter a number (negative to quit)\n";
int temp = 0;
cin >> temp;
if ( temp < 0 ) break;
v.push_back(temp);
}
for ( int i = 0; i < v.size(); ++i )
{
cout << v[i] << ' ';
}
v.clear();
return 0;
}
Hey somebody's posing as me....
I don't know how that happend but I'm not taylorguitarman =P.
Probably a bug tayloyrman, I didn't post as you on purpose anyway.
Testing.
Well, when i tried to login a while ago i was mysteriously logged in already as someone who i wasn't.
Also, when i tried to register, it logged me in as someone who i wasnt.
Very strange, very strange indeed.