help on variable size array

This is a discussion on help on variable size array within the C++ Programming forums, part of the General Programming Boards category; Hi, all I have some difficulty to realize a variable size array in a function, whose size is given by ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    help on variable size array

    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

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You have to use dynamic memory and the new operator (or malloc or similar function if you use C style syntax)
    Code:
    void createArray(int x)
    {
    variableTypeNameHere arrayNameHere = new variableTypeNameHere[x];
    }
    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.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    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?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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 01:38 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    int a1[10] = {0};
    cout<<a1[9]<<endl; //0

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Quote Originally Posted by 7stud
    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;
    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.
    Last edited by grib; 02-23-2005 at 11:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21