use a void pointer to point at an fstream object in c++ -


i new c++ , object oriented programming in general. objective use void pointer point fstream object. have structure contains void pointer. want use pointer reference fstream object can use in other functions write file using protocol buffer apis (serializetocodedstream).i want able write same file through repeated function calls (and read it). way i'm using protocol buffers serialize data , write file.

my structure is:

    typedef struct store     {       void *obj;       } st; 

my code create fstream object , use pointer point object

    st *entry1;     fstream output;     output.open("file1.txt", ios::out | ios::trunc | ios::binary);     entry1->obj=&output; 

this i'm using pointer

     int addtofile( st *entry)      {      ostreamoutputstream *_ostreamoutputstream;      codedoutputstream *_codedoutputstream;   _ostreamoutputstream = new ostreamoutputstream(entry->obj);      _codedoutputstream = new codedoutputstream(_ostreamoutputstream);        file.serializetocodedstream(_codedoutputstream); //file object of protocol buffer class       delete _ostreamoutputstream; //this line seems causing segmentation fault when run program       delete _codedoutputstream;       } 

edit: here function prototypes ostreamoutputstream , codedoutputstream

    ostreamoutputstream (ostream *stream, int block_size=-1);     codedoutputstream (ostreamoutputstream *output); 

the code compiles without errors segmentation fault when run program. there nothing being written file either. answers on i'm doing wrong or suggestions of alternate methods appreciated. thank you.

you deleting ostreamoutputstream while still being used codedoutputstream. have delete objects in reverse of order created, no object deleted while other object still using it.

i suspect have second problem: creating fstream local variable, means destroyed function returns, making pointer invalid.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -