I\'m working on a small program to help speed up some data analysis for my lab work. It\'s supposed to read in data from a text file, create a bunch of arrays containing thi
You say that you're not comfortable with dynamically-sized arrays, or std::vector
.
I'm afraid that you'll have to figure out how to get comfortable here, because that's precisely what's std::vector
is for.
The only other alternative is to use the gcc
compiler. gcc
does allow you to have variable-sized arrays. It's a compiler extension, it's not standard C or C++:
void method(size_t n)
{
dataPoint array[n];
// Do something with the array.
}
You could either use a C++ container, like std::vector, or use dataPoint *data = malloc(LENGTH * sizeof(dataPoint))
. In C++, it's probably better practice to use std::vector
.
C99 has variable-length arrays, but you're question seems to be C++.
The real problem, though, is that you want to slurp the whole file all at once. Unless you're using a library that requires data to be in memory, it'll probably be easier to iterate over the text of the file using fgetc or whatever C++ has for that. After all, you'll need to go through the text at some point; you might as well save yourself the trouble of allocating memory.
First and foremost, built in arrays in C++ have to have compile-time size. It is not enough to declare your LENGTH
variable const
. It is also important to make it a compile-time constant. Your LENGTH
is not a compile time constant, so declaring an array of size LENGTH
is not possible. This is what the compiler is trying to tell you.
When you need to build an array, whose size is not known in advance, you typically have at least three approaches to choose from:
Two-pass reading. Make a "dry run" over the data source to determine the exact size of the future array. Allocate the array. Make a second pass over the data source to fill the array with data.
Reallocation. Use a reallocatable array. Allocate an array of some fixed size and fill it with data. If the array proves to be too small, reallocate it to bigger size and continue to fill it. Continue to read and reallocate until all data is read.
Conversion. Read the data into a cheaply and easily expandable data structure (like linked list), then convert it to array.
Each of these approaches has it own pros and cons, limitations and areas of applicability.
It looks like in your case you are trying to use the first approach. It is not a very good idea to use it when you are working with files. Firstly, making two passes over a file is not very efficient. Secondly, in general case it might create a race condition: the file might change between the passes. If you still want to go that way, just use std::vector
instead of a built-in array.
However, in your case I would recommend using the second approach. Again, use std::vector
to store your data. Read your data line by line and append it to the vector, item by item. The vector will automatically reallocate itself as necessary.