Prev: Optimize integer program in speed
Next: VisualC++ vs GCC: read in a number in exponential notation
From: Paul Bibbings on 8 Jun 2010 14:19 LiloLilo <danilobrambilla(a)tiscali.it> writes: > > please try this code derived from the one you provided above. It > produces two different files. Compiler is Visual C++ 2010 > > // #include "stdafx.h" // removed non-standard header for GCC build > > #include <fstream> > > unsigned TotalBitCounter = 0; > > void WriteBitsToFile(unsigned Data, > unsigned Length, > std::ofstream & OutFile) > { > static unsigned BitBuffer = 0; > static unsigned BitCounter = 0; // ... > } > > > template <typename T2> > void WriteBitsToFile(T2 Data, > unsigned Length, > std::ofstream & OutFile) > { > static unsigned BitBuffer = 0; > static unsigned BitCounter = 0; // ... > } > > > int main() > { > std::ofstream out1("Out1.txt"); > if (out1) > { > WriteBitsToFile(12345U, 2, out1); > WriteBitsToFile(12345U, 5, out1); // ... > } > > > std::ofstream out2("Out2.txt"); > if (out1) // my error! > { > WriteBitsToFile(12345U, 2, out2); > WriteBitsToFile(12345U, 5, out2); // ... > } > } For this example you will see that you are not actually calling your templated function at all, where my understanding of the original problem lay in your wanting to claim a difference between the two as to when writes occured (and, in particular, something to do with your static BitBuffer being "flushed to 0" between calls for the template function). In your code here the difference in the files arises merely because the total number of bytes added to the buffer written to Out1.txt is not divisible by 32 (being 102) so that the accumulation for write to Out2.txt begins with a non-empty buffer. Is this just an error on your part in putting together this example, or does it perhaps reflect the nature of the problem you were having originally? If you correct the code for your example here (abridged above), using: std::ofstream out2("Out2.txt"); if (out2) // corrects an error in my example, duplicated in yours { WriteBitsToFile<>(12345U, 2, out2); WriteBitsToFile<>(12345U, 2, out2); // ... } so that the function template is instantiated and used for the second (otherwise identical) series of invocations then I am sure you will find - as I do, building with gcc-4.4.3 - that the two files will again be identical. Note: I don't have access to VS 2010, so I had to remove stdafx.h. Regards Paul Bibbings -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 Prev: Optimize integer program in speed Next: VisualC++ vs GCC: read in a number in exponential notation |