title: Why does this C++ member function call fail with a segmentation fault?
body: <p>I have followed this forum for years and found it extremely helpful, answered all of my questions so far. But today I seem to be stuck.</p>
  
  <p>Being new to C++ I attempted to code some classes that help me to connect to a website through a proxy server. I therefore call a function of a class that encapsulates some logic to process HTTP requests. I pass a struct and two more parameters to this function by reference. The execution fails with a Segmentation Fault (duh). Debugging shows that the Segmentation Fault occurs when I call my function.</p>
  
  <p>In my main function I create an instance of a class which is declared in httplayer.hpp and call one of it's members like this:</p>
  
  <pre><code>#include "../include/httplayer.hpp"
  
  int main(int argc,char** argv){
      HttpLayer httplayer;
      proxy_data proxy;
      proxy.username="name";
      proxy.password="pwd";
      proxy.address="some.address";
      proxy.port="8080";
  
      int n = httplayer.openHttpSocket("google.com",8080,proxy); //Here the catasprohy happens
      //more stuff
      return 0;
  }
  </code></pre>
  
  <p>the httplayer.hpp file looks like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  
  #include "../include/commlayer.hpp"
  
  
  struct proxy_data{
          std::string username, password, address, port;
  };
  
  class HttpLayer{
      public:
          static const int HTTP_BUF_SIZE = 6555648;
  
          int closeHttpSocket();
          int requestHttp(std::string, char*);
          int openHttpSocket(std::string, int, struct proxy_data&amp;);
  
      private:
          bool endsWith(std::string const &amp; value, std::string const &amp; ending);
          CommLayer comm;
  };
  </code></pre>
  
  <p>In the corresponding httplayer.cpp I have finally this function:</p>
  
  <pre><code>int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &amp;proxy){  
  </code></pre>
  
  <p>gdb shows the following information:</p>
  
  <pre><code>14 int n = httplayer.openHttpSocket("google.com",8080,proxy);
  (gdb) s
  Program received signal SIGSEGV, Segmentation fault.
  0x0804940b in HttpLayer::openHttpSocket (
      this=&lt;error reading variable: Cannot access memory at address 0xbf37d81c&gt;, 
  address=&lt;error reading variable: Cannot access memory at address 0xbf37d818&gt;, port=8080, 
  proxy=&lt;error reading variable: Cannot access memory at address 0xbf37d814&gt;)
  at src/httplayer.cpp:20
  20  int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &amp;proxy){
  </code></pre>
  
  <p>My first suspect was the struct, but wasn't so far able to see my mistake. I am new to C++, so I might do some horribly obvious mistake in the way I use classes or functions or header files, but I seem to be unable to find out myself.</p>
  
  <p>Your help is highly appreciated, Thank you!</p>
  
  <p>EDIT:</p>
  
  <p>Of course only I as a C++ noob can be right and the experienced community has to be mistaken.
  So what I did to prove my infinite wisdom is to comment out the content of openHttpSocket except a printf()... but suddendly it worked. So I started to re-include part by part of the code again until I stumbled accross this line:</p>
  
  <pre><code>int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &amp;proxy){
      ...
      unsigned char proxyanswer[HTTP_BUF_SIZE];
      ...
  }
  </code></pre>
  
  <p>Well, all credit goes to @WhozCraig 's crystal ball here. And to everyone else asking me to post the content of the function. Thank you!</p>
  
  <p>Now another beginner's question: What happens to my stack here? And how do I create some kind of char* buffer that I can use in a member function in C++? Or is there a complete different concept in C++ that is to be used instead of those good old char arrays?</p>