[C++-sig] Strange Behavior w/File Pointers

Michael Wieher michael.wieher at gmail.com
Fri Mar 7 19:32:42 CET 2008


Yes I'm sure.

I've written functions to write out from inside the C++ file to a text file
to check this, as well as getPtr functions that will export the pointer's
location to the python, and the results are the same.

It doesn't happen EVERY call, but seemingly randomly. (1 in 15, 3 in 10,
etc.)

Could there be strange binary characters that are read in that could cause
it to shift like that?

2008/3/7, Raoul Gough <RaoulGough at yahoo.co.uk>:
>
> Michael Wieher wrote:
> > mYmodule.cpp
> > ---------------------------
> >
> > istream globalFile;
> > struct dataStruct {
> >    int a;
> >    int b;
> >    short c;
> > } __attribute__(align(2));
> >
> > PyObject *
> > mYmodule_setFile() {
> >     --parseTuple--
> >     globalFile.open(pathToFile,ios::in|ios::binary);
> > }
> >
> > PyObject *
> > mYmodule_getNBytesAtATime() {
> >   --parseTuple--
> >   int N;  //the size of a dataStruct
> >   globalFile.read((char*)&dataStruct,N);
> >   Py_BuildValue('ii',dataStruct->a, dataStruct->b);
> > }
> >
> > ==============
> > Given the above rough pseudocode, I find that each time my Python
> > module calls myModule.getNBytesAtATime() the file-pointer shifts
> > strangely.  Most of the time it shifts N bytes.  But, sometimes, on
> > the same points but with no definable reason that I can see, it shifts
> > N*x bytes, where x is some integer.
>
>
> Are you sure this happens in a single call to the C++ function? If I
> suspected something like what you describe I would probably "instrument"
> the C++ function by having it log something to stdout or stderr, e.g.
>
> std::cout << "Getting " << N << " bytes starting at " <<
> globalFile.tellg() << std::endl;
> ....
> std::cout << "Got " << N << " bytes, now at " << globalFile.tellg() <<
> std::endl;
>
> (not compiled or tested!). This should show you if the function itself
> is moving the pointer unexpectedly, or if it's happening somewhere else...
>
>
> --
> Cheers,
> Raoul.
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080307/e4dee82e/attachment.htm>


More information about the Cplusplus-sig mailing list