segmentation fault at end of the program
<p>hey guys,
i've got a bit problem. my program throws segmentation fault when returning zero in main</p>

<p>the main function looks like this</p>

<pre><code>int main(int argc, char* argv[]){
 ifstream fs("test.dat", ios::binary);
 cSendStream sendstr(&amp;fs,20);

 char *zomg=sendstr.data();
//zomg[20]=0;

 sendstr.read(20);

 cout&lt;&lt;"Buffer: "&lt;&lt;sendstr.data()&lt;&lt;endl;
 cout&lt;&lt;"Remaining: "&lt;&lt;sendstr.dataAvailable()&lt;&lt;endl;

 sendstr.read(2);
 cout&lt;&lt;"Buffer: "&lt;&lt;zomg&lt;&lt;endl;
 cout&lt;&lt;"Remaining: "&lt;&lt;sendstr.dataAvailable()&lt;&lt;endl;

 sendstr.read(10);
 cout&lt;&lt;"Buffer: "&lt;&lt;zomg&lt;&lt;endl;
 cout&lt;&lt;"Remaining: "&lt;&lt;sendstr.dataAvailable()&lt;&lt;endl;
 cout&lt;&lt;"end..."&lt;&lt;endl;
return 0;
}
</code></pre>

<p>that commented zomg part is the point which makes the program crash.. zomg is pointing on char[20]. My point on that line is to set the end of the array because somehow if I don't do that the stream reads more data than 20 bytes, but it prints just one unnecessary symbol though.</p>

<p>funny thing is that even if i write there some additional code between this and return 0 it throws fault first when returning..</p>

<p>just for the case you wanted to see the cSendStream class:</p>

<p>cSendStream.h</p>

<pre><code>class cSendStream{
 public:
  cSendStream(std::istream*, int streamsize);
  int read(int);
  int dataAvailable();
  char* data();
 private:
  void shift(int);

  std::istream *source;
  int streamsize;
  char* buffer;

};
</code></pre>

<p>and the cSendStream.cpp</p>

<pre><code>#include "cSendStream.h"

cSendStream::cSendStream(std::istream *src, int size){
 source=src;
 streamsize=size;
 buffer=new char[streamsize];
 memset(buffer,0,streamsize);
}

int cSendStream::read(int i){
 if(dataAvailable()&lt;1 || i&lt;=0){
  return 0;
 }
 if(i&gt;dataAvailable()){
  i=dataAvailable()-1;
 }
 if(i&gt;streamsize){
  i=streamsize;
 }

 shift(i);
 source-&gt;read(&amp;(buffer[streamsize-i]),i);
 return i;
}

int cSendStream::dataAvailable(){
 int current=source-&gt;tellg();
 source-&gt;seekg (0, std::ios::end);
   int available = (int)(source-&gt;tellg())-current;
   source-&gt;seekg (current);

 return available;
}

char* cSendStream::data(){
 return buffer;
}

void cSendStream::shift(int i){
 char tmp[2048];
 memcpy(tmp,buffer,streamsize);
 memcpy(&amp;(buffer[0]),&amp;(tmp[i]),streamsize-i);
}
</code></pre>
