Author Topic: Problems with fread  (Read 4416 times)

Borde

  • *
  • Posts: 891
    • View Profile
Problems with fread
« on: 2007-05-03 23:48:42 »
Hello everyone.
I'm writting a little program and I found a big problem. When I try to execute this:
fread(Polygons,sizeof(s3DModelPolygon), NumPolys,is);
I get an error (fread returns 0 and errno is set to 22). Polygons is a dinamically allocated array created with malloc (it didn't returned NULL, so it should be correct, right?)
NumPolygons=516
sizeof(s3DModelPolygon)=24
That makes a total of 12384 bytes.
Well, hope someone arround here has an answer. I've been googling for hours and I'm tired.
Must I surrender and read each polygon one by one?

EDIT: Never mind, I'll just stop using that crappy malloc. It works perfectly using new[].
« Last Edit: 2007-05-04 10:53:38 by Borde »

Cyberman

  • *
  • Posts: 1572
    • View Profile
Re: Problems with fread
« Reply #1 on: 2007-05-06 17:40:01 »
LOL. I was going to say you shouldn't be using malloc.  There is a problem with mixing malloc with objects. Objects (IE classes) have a different memory allocation scheme than that of structures using malloc. THIS can and will be a big gotcha.  Also it's best not to use malloc in any C++ program, there are issues with memory allocation methods that new and delete use versus malloc and free. :/  It's been a problem for 12 years :D

Cyb

mirex

  • *
  • Posts: 1645
    • View Profile
    • http://mirex.mypage.sk
Re: Problems with fread
« Reply #2 on: 2007-05-16 08:47:17 »
Hi, is the problem solved or not ?

Anyhow, lets try to clear things up. malloc works also in C++, but it has to be used correctly. You must not use malloc for allocating the C++ classes (objects). Those have to be allocated through 'new' operator, which also calls class constructors. Then they have to be freed through 'delete' call which will call its destructor. If you would have defined some class A, and then you would allocate it through malloc like A* pA = malloc( sizeof( A ) ); it could cause some headaches. I'm not sure what problems does it cause, but it does. It should be alloced like A* pA = new A;

about the fread() call - I can't answer properly when I don't know what are the parameter types, and how are they used. Can you post some code before the fread() call ?

Also, in case that 'Polygons' variable is a array of classes, this could be causing the problems. Classes can have some hidden variables so its not good to overwrite them as a whole, for example if you have class A;  A  a; fread( &a, sizeof( a ), 1, file ); or memset( &a, 0, sizeof( a ) ); could cause overwriting its internal variables.

Borde

  • *
  • Posts: 891
    • View Profile
Re: Problems with fread
« Reply #3 on: 2007-05-18 13:31:32 »
Thanks everyone, but when I used malloc I was allocating structs, not classes, so I still don't understand why crashed. I simply converted the structures to classes and it was fine allocating them with new[] (althought I don't think it's very elegant to use classes this way).
The prototype of fread is: size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ). You pass him the buffer where the obtained contents must be stored, the numbre of elements, the size of every element and (ovbiusly) the stream you want to read from. It returns the number of element that could actually be read. In my case it returned 0 (so it clearly failed). The number of elements, the size an the stream were right (because they are the same right now and it works), so the only thing that could be wrong was the buffer I previously allocated with malloc.
As for overwriting a whole array of objects using fread, it seems to work fine right now. Maybe I'll find problems later on, but it's working perfectly for now.

Izban

  • *
  • Posts: 432
    • View Profile
Re: Problems with fread
« Reply #4 on: 2007-05-19 05:07:40 »
Borde if i may be so rude as to inquire what sort of program you are coding

Borde

  • *
  • Posts: 891
    • View Profile
Re: Problems with fread
« Reply #5 on: 2007-05-19 09:39:45 »
Oh, I'm coding a little game with a friend :-P
It's for the universisty and I must finish it in 8 days. That's problem because there is still quiet a lot of work to do.

Cyberman

  • *
  • Posts: 1572
    • View Profile
Re: Problems with fread
« Reply #6 on: 2007-05-19 15:15:41 »
Borde might I suggest you instead use fstream?  The advantage is kind of significant because you can make objects load and save themselves.  I use to think I would be wasting time with such things but after a while I realized I was reinventing the whee over and overl often with my 'load code'.  If you have an especially complex object to load for example (that contains numerous objects) it is a perfect example why one should use fstream and have the internal methods of the objects handle the loading.
The real thing is learning the hard way is a waste of time (but I suppose it's better than not learning at all). I suppose you are too far along to really 'change the whole' method you are handling your file loading and saving etc.  So perhaps you can get 'kudos' points by saying "we now know" :D  In any case good fortune on your project.

Cyb

Borde

  • *
  • Posts: 891
    • View Profile
Re: Problems with fread
« Reply #7 on: 2007-05-19 15:43:31 »
Thanks for the advice Cyberman. I'll try with fstream next time. But for now I'll stick with my loading code. You know what they say: if it's not broken, don't "fix" it.