Hi, all
I have some difficulty to realize a variable size array in
a function, whose size is given by one of the parameters in the
fuction call; Can anyone help me out? thank you.
like this
Hi, all
I have some difficulty to realize a variable size array in
a function, whose size is given by one of the parameters in the
fuction call; Can anyone help me out? thank you.
like this
You have to use dynamic memory and the new operator (or malloc or similar function if you use C style syntax)
That code creates an array of variableTypeNameHere objects called arrayNameHere using the new[] operator. The array is never used and the memory assigned on the heap isn't freed up so there is a memory leak that can bite you later on, so basically the code is worthless except to see the syntax of declaring an array of size x where x isn't known at compile time, but heh, that's what you wanted to know, so there it is. Please read up on dynamic memory, how to declare it properly using both new and new[] and how to release the memory properly with delete and delete[] in your favorite textbook. If you don't have a textbook, get one. But until you do, there may well be more on dynamic memory in the FAQ section of this site.Code:void createArray(int x) { variableTypeNameHere arrayNameHere = new variableTypeNameHere[x]; }
You're only born perfect.
thank elad's reply;
actually, I think the size of the array is known at the compile time;
I want to write a function like you told me above; then I am going to call this function with a
definite number (the size of the array) in the main function, for it's easier to change to other size.
because I heard from other people dynamically allocated memory runs slowly, since the size
of the array is known , is there any way to avoid using new operator?
If the memory used by the array is allocated at compile time then you don't use dynamic memory with new[] (or malloc()). However if the memory to be used by the array isn't allocated until run time, then you must use new[] (or malloc()), AFAIK.
You're only born perfect.
Here's another explanation:
1) An array size has to be a constant, unless...
2) you dynamically allocate memory for the array using the new operator:
int x;
cin>>x;
int* parray;
parray = new int[x];
Unlike normal objects, which are destroyed when the block they are declared in terminates, an array created with the new operator remains in existence until it is destroyed using delete:
delete parray;
It is important to note that the delete operator does not delete the variable parray--it deletes the memory that parray points to. parray can be assigned another address after using delete:
int i = 30;
parray = &i;
Last edited by 7stud; 02-23-2005 at 02:38 AM.
Are you talking about this?
Code:void func ( int array[], size_t num ) { for ( size_t i = 0 ; i < num ; i++ ) { array[i] = 0; } } int main ( ) { int a1[10]; int a2[20]; func( a1, 10 ); func( a2, 20 ); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Code:int a1[10] = {0}; cout<<a1[9]<<endl; //0
Todays adventure in pendantry is to point out that new typename[n]; must be paired with delete[]pointer_value; to do otherwise invoces our friend undefinded behavior. If the elements of your array do not have destructors then the most likely behavior is to free the memory as normal. However the compiler is permited to use what amount to seperate heaps for (presumibly small) single objects and multiple contiguous objects.Originally Posted by 7stud
Last edited by grib; 02-23-2005 at 12:05 PM.