Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to dynamically allocate an array c++

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.
Comment

PREVIOUS NEXT
Code Example
Cpp :: cstring to string 
Cpp :: c++ set comparator 
Cpp :: C++ Vector Iterator Syntax 
Cpp :: cpp pushfront vector 
Cpp :: int max c++ 
Cpp :: stringstream stream number to string 
Cpp :: c++ array size 
Cpp :: char size length c++ 
Cpp :: sort a vector c++ 
Cpp :: c++ int to char* 
Cpp :: how to find last character of string in c++ 
Cpp :: untitled goose game 
Cpp :: min heap priority queue c++ 
Cpp :: reverse function in cpp array 
Cpp :: c++ get maximum value unsigned int 
Cpp :: cpp func as const 
Cpp :: c++ 14 for sublime windoes build system 
Cpp :: c #define 
Cpp :: print two dimensional array c++ 
Cpp :: slice a vector c++ 
Cpp :: c++ output current timestamp 
Cpp :: Visual studio code include path not working c++ 
Cpp :: std::count() in C++ STL 
Cpp :: c++ class template 
Cpp :: long pi in c++ 
Cpp :: lambda function in c++ 
Cpp :: c++ variable types 
Cpp :: trie code cpp 
Cpp :: max pooling in c++ 
Cpp :: matrix c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =