This link has been bookmarked by 19 people . It was first bookmarked on 23 Apr 2008, by Toto RoToto.
-
09 Oct 16
-
17 Aug 10
-
07 Jun 10
-
01 Aug 09
-
08 Jul 09
-
06 Jun 09
-
Allocate an array with code>new
When the desired size of an array is known, allocate memory for it with the
newoperator and save the address of that memory in the pointer. Remember: Pointers may be subscripted just as arrays are. The example below reads in a number and allocates that size array.int* a = NULL; // Pointer to int, initialize to nothing. int n; // Size needed for array cin >> n; // Read in the size a = new int[n]; // Allocate n ints and save ptr in a. for (int i=0; i<n; i++) { a[i] = 0; // Initialize all elements to zero. } . . . // Use a as a normal array delete [] a; // When done, free memory pointed to by a. a = NULL; // Clear a to prevent using invalid memory reference.
-
-
25 Mar 09
-
22 May 08
-
23 Apr 08
-
Allocate an array with code>new
When the desired size of an array is known, allocate memory for it with the
newoperator and save the address of that memory in the pointer. Remember: Pointers may be subscripted just as arrays are. The example below reads in a number and allocates that size array.int* a = NULL; // Pointer to int, initialize to nothing. int n; // Size needed for array cin >> n; // Read in the size a = new int[n]; // Allocate n ints and save ptr in a. for (int i=0; i<n; i++) { a[i] = 0; // Initialize all elements to zero. } . . . // Use a as a normal array delete [] a; // When done, free memory pointed to by a. a = NULL; // Clear a to prevent using invalid memory reference.
-
-
08 Feb 08
-
09 Oct 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.