0

As already posted in this question C - fwrite binary file bigger than 4GB I need to write a binary file bigger than 4 GB. As learned through the first answer, I decided to develop my code making multiple calls to fwrite.

My acquisition board has 2 FIFOs with dimension bufferLength12 = bufferLenght34 = 524288 Bytes. Each FIFO memory is linked with different channels, so when I start the acquisition I have data in both of them coming from different channels.

I want to do multiple readings, let's say 4*nacq, from those FIFOs. And I want to use a different pointer for each cycle of acquisition.

So I have allocated the memory in this way:

v12 = (UINT64 *) calloc ( bufferLength12 , 4*nacq);
v34 = (UINT64 *) calloc ( bufferLength34 , 4*nacq);

Then the acquisition starts and I should pass, for each acquisition cycle, a different pointer in order to store the datas in RAM.

I make a cicle on the pointers in this way:

for ( p=v12,q=v34 ; p<v12+4*nacq && q<v34+4*nacq; p++,q++) {
    ReadF(h, 0, (UINT64 *) p , bufferLength12 , NULL, 0);
    ReadF(h, 1, (UINT64 *) q , bufferLength34 , NULL, 0); }

Then I try to write the data from the RAM to a binary file:

for ( p=v12,q=v34 ; p<v12+4*nacq && q<v34+4*nacq; p++,q++) {
    fwrite( (UINT64 *) p, 8 , bufferLength12 , fd12);
    fwrite( (UINT64 *) q, 8 , bufferLength34 , fd34);
     }
fclose(fd12);
fd12=NULL;
fclose(fd34);
fd34=NULL;

If I run the code, I get a size of the binary file which is much bigger than then actual acquisition length. So I think that the pointers aren't been properly initialized.

If I plot the acquired data (just noise), I can see a lot of 0-values due to the allocation made with calloc.

enter image description here

Any help would be greatly appreciated!

Community
  • 1
  • 1
claudiop
  • 130
  • 1
  • 9
  • 3
    [Please don't cast the return value of `malloc()` and friends in C](http://stackoverflow.com/a/605858/28169). – unwind Sep 18 '13 at 09:46
  • 1
    You are closing you file descriptor for every iteration of the write for loop. The next iteration won't be able to write to the file if it's not opened! – Vivek S Sep 18 '13 at 09:50
  • Thank you, but if I don't make the casting the code still runs but I see the `=` underlined in red with the mention: 'Error: a value of type "void *" can't be assigned to an entity of type "ULONGLONG*" – claudiop Sep 18 '13 at 09:52
  • @claudiop That error implies that you are compiling your C code as C++. Don't do that, the two languages are not the same. – unwind Sep 18 '13 at 09:55
  • 1
    @VivekS You are definitely right, thank you. Now the code executes normally and I can save the binary files. But I still have problems with the dimension in these files so I think is there something wrong in the initialization of the pointers – claudiop Sep 18 '13 at 09:55
  • What is h ? Why 8 as the second parameter to fwrite ? How many bytes do you plan to read ? What are you planning to allocate using calloc ? Please explain. – Vivek S Sep 18 '13 at 10:31
  • h is the pointer to the acquisition board. 8 is the size in bytes of each element to be written (because the board saves data as 32-bit word for the I component and 32-bit word for the Q-component of the signal). So I pass 8 Byte to fwrite as size of each element. I want to read the whole buffers (fifo1 and fifo2) and I want to allocate the right amount of memory for `4*nacq` acquisitions. If I make `4*nacq` acquisitions I will have `4*nacq` times the fifo1 and the fifo2 full. – claudiop Sep 18 '13 at 10:37
  • @unwind where can I change the compiling language? – claudiop Sep 18 '13 at 12:58

1 Answers1

0

There are many problems and indicators of ... confusion.

This:

For FIFO #1, I allocated space for 4*nacq UINT64 pointers, each to a memory size of bufferLength12 through the call:

Is very hard to interpret, and certainly doesn't match the code:

v12 = (UINT64 *) calloc ( bufferLength12 , 4*nacq);
v34 = (UINT64 *) calloc ( bufferLength34 , 4*nacq);

You are allocating two blocks of memory, one is bufferLength12 * 4 * nacq bytes, the other is bufferLength34 * 4 * nacq bytes. Nothing in the above code supports your claim that you are allocating space for "4*nacq UINT64 pointers", that's simply not true.

Also, you should remove the cast.

Since you're going to run some data-acquisition into these buffers, there's absolutely no point in using calloc() either, since that spends time clearing the memory before returning it to you.

Assuming you really mean 4 * nacq * bufferLength12 64-bit values and 4 * nacq * bufferLength34 64-bit values, the allocations should be:

UINT64 *v12 = malloc(4 * nacq * bufferLength12 * sizeof *v12);
UINT64 *v34 = malloc(4 * nacq * bufferLength34 * sizeof *v34);

Also, don't close the file inside the loop, that will make it fail immediately on the next write.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • At each cycle I want to save the data contained in FIFO1 and in FIFO2 in two different position in RAM using two different pointers. The dimension of the two FIFOs is the same `bufferLength12=bufferLength34`. I need to do `4*nacq` cycles. – claudiop Sep 18 '13 at 10:08