Why do we need dynamic arrays?
•Example: reading information from file
•File has the following layout:
• DWORD: number of elements
• DWORD[]: an array of integers
•
•File: 2, 0, 1
•File: 10, 0, 1, 0, 1 , 0, 2, 2, 2, 2, 2
•
•const int MAXDATA = 1000;
•int data[MAX_DATA];
•int numElements;
•
•void ReadData()
•{
• ReadFile(…, &numElements, sizeof(numElements), …);
• if (numElements > sizeof(data)/sizeof(data[0]))
• {
• numElements = sizeof(data)/sizeof(data[0]);
• }
• ReadFile(…, data, numElements*sizeof(data[0]), …); // reading bytes
•}
•
•So we have a dilemma:
•Small file – waste of memory
•Big file (more than 1000 elements) – not enough space, data is truncated