0

Possible Duplicate:
C dynamically growing array

I have a program and I need to read floats from a file. Each line is one float number. The problem is that this file may be very large

 float tab[1000];     
 
 f = fopen ("data.txt", "r");
 i=0;   
 while (feof(f) == 0) {        
   fscanf (f, "%f\n", &tab[i]);                
   i++;    
 }

How may I change size of array dynamicaly if it's too small?

Community
  • 1
  • 1
ciembor
  • 7,189
  • 13
  • 59
  • 100
  • 1
    This should definitely be closed for a duplicate: what are general idioms for handling growing arrays in C? –  Jan 03 '12 at 21:10
  • 1
    the conversion specifier to read `float` is `f` not `d` – ouah Jan 03 '12 at 21:12
  • http://stackoverflow.com/questions/3536153/c-dynamically-growing-array , http://stackoverflow.com/questions/3294396/reallocating-an-array-c99 , http://stackoverflow.com/questions/8338431/dynamic-array-using-ansi-c –  Jan 03 '12 at 21:13

4 Answers4

6

Just start with a decent size, malloc, then realloc as you go if needed.

double *tab;
int num = 1000;

tab = malloc(num * sizeof *tab);

while (..) {
    if (i >= num)
        num *= 2;

    tab = realloc(tab, num * sizeof *tab);
    /* ... */
}
  • You should try an initial size that covers most inputs without requiring too much memory
  • You can try different realloc strategies, doubling the size is just one
  • You should probably check the result of malloc and realloc
cnicutar
  • 178,505
  • 25
  • 365
  • 392
3

If you need them all in memory at the same time, the malloc and realloc, as @cnicutar suggests, is probably the best solution.

But for many problems, you only really need to process one value at a time. Just continue reading until you reach the end of the file. It depends on the nature of the problem.

And you should correct several problems in your code:

  • "%d" expects an int* argument; use "%f" to read float data.
  • Don't use feof(f) to detect the end of the file. Check the value returned by fscanf() (or whatever input routine you're using). feof() is used to distinguish between a normal end-of-file condition and an error condition, and should be used only after an input function has already told you that it's run out of data.
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

You need to allocate the memory manually using malloc. If you buffer gets to small call realloc.

pmr
  • 58,701
  • 10
  • 113
  • 156
0

You cannot change the size of an array dynamically if it is allocated on the stack. You need something like this:

tab = calloc(1000, sizeof(float));

...

// need to resize
tmp = realloc(tab, (1000 + NUM_NEW_ELEMENTS) * sizeof(float));
if (tmp != NULL)
  tab = tmp;
dbeer
  • 6,963
  • 3
  • 31
  • 47
  • `calloc()` sets the allocated space to all-bits-zero, which is often wasteful. The standard doesn't guarantee that a floating-point 0.0 is represented as all-bits-zero, and if your code is written properly you won't be referring to any elements of the array until you've assigned values to them yourself. `float *tab; tab = malloc(1000 * sizeof *tab);` is cleaner. (Or you can just initialize `tab` to `NULL` and use `realloc()` for *every* allocation.) – Keith Thompson Jan 03 '12 at 22:18