Different instances of my class uses the same buffer????


I have this class , I show you the constructor:

1
2
3
4
5
W_stream::W_stream{
  char mybuffer [265536];
  the_stream = new stringstream ;
  the_stream->rdbuf()->pubsetbuf(mybuffer,265536);
}

This class has put and get methods to write and read data and has a int loc; to tell me the localization of internal pointer.

Ok, I create 2 instances:
my_wstream1= new W_stream();
my_wstream2= new W_stream();

And I use my_wstream1.put(a int) 234 times and my_wstream2.put(a int) 745.

Later my_wstream1 has loc of 234*4 and 745*4. Up to here all is right.

But later my_wstream1.get gives me values that it be supossed has been written by my_wstream2 ????

The question is mybuffer are the same ?? I dont understand ???
Mybuffer is private to W_stream.(also "the_stream")
Any idea ?


Last edited on
Your buffer is on the stack. In this case it is the same, if you constructed the streams from different places, it would be different. Note that this is is a terrible thing to do. That memory will be used to store local variables and etc., so you won't know what's in there. You should use dynamic memory here. If you want the buffer to be shared, you can make it a static member of the class.
Topic archived. No new replies allowed.