0

I did this program, where I declared

long double *M;

Then I read the data from a file

   i=1;
   M = (long double *) calloc ( i , sizeof(long double) );
   while ( !feof( arq_matriz ) ){ 
       fscanf ( arq_matriz , "%Lf" , (M+i-1));
       i++;
       M = (long double *) realloc ( M , i * sizeof(long double) );
   }
   fclose(arq_matriz);

So, to test I print it on the screen

for (i=0;i<=44;i++){
    printf("\n %Lf",*(M+i));
}

all perfect. But when I passed it for a function or even when I debug it I've got *M=0 while the truth *M = 0.25 and *(M+1) = 'invalid float number'. I don't know why this is happening since everything was printed right on the screen. Any hints? Thanks!

Rodolfo
  • 35
  • 3
  • 4
    Note, that in C it is not required to type-cast return value of `malloc` / `calloc` / `realloc`. Check [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – LihO May 30 '12 at 06:26
  • @LihO you should have said it is not a good practice to type-cast rather than it is not required, I think – Krishnabhadra May 30 '12 at 06:28
  • @Krishnabhadra: It is not required because `void *` can be implicitly cast to any data types in C(unlike in C++). Whether omitting the return type casting is an Good practice or an Yoda condition, is perception based. – Alok Save May 30 '12 at 06:31
  • In the `fscanf` call, `M+i-1` might be a problem, try `M+(i-1)` instead. – Some programmer dude May 30 '12 at 06:33
  • 1
    Can you show us the failing code? You show the code that prints `M` successfully, but not the code that fails. – ugoren May 30 '12 at 06:38
  • Thanks guys for the answers. Well the problem is when I'm debugging the value of *M are wrong. When I ask to print it, with the code above, it show the corrected values. Maybe could be a problem of the debug? I use code::blocks. All the other pointers are working just fine. – Rodolfo May 30 '12 at 07:53
  • Perhaps, this is a bug in the compiler that you use. – BLUEPIXY May 30 '12 at 09:27

1 Answers1

1

Although you're attempting to realloc in the loop, you're not doing it correctly -- when/if realloc fails, it returns a null pointer. Since you assign that directly back to M, at that point you've lost your original pointer (leaking the memory) and since you don't check whether it was a null pointer, you continue writing to it, causing undefined behavior if it ever fails.

Although it's probably unrelated to the problem at hand, a loop like while (!feof(somefile)) is almost guaranteed to misbehave (typically seeming to read the last item from the file twice).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • He seems to be doing a realloc there. Won't it help? Am I missing something? – Jay May 30 '12 at 06:29
  • I thought that the unique problem that could happens with realloc is a lack of memory. Since the file will be tipically small, not much bigger than 100~300 items, i thought that wouldn't be necessary to check the return pointer in realloc. But I'll change it. Thanks. – Rodolfo May 30 '12 at 07:51