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.
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.
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:
#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;
}
the httplayer.hpp file looks like this:
#include <iostream>
#include <cstring>
#include <string>
#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&);
private:
bool endsWith(std::string const & value, std::string const & ending);
CommLayer comm;
};
In the corresponding httplayer.cpp I have finally this function:
int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &proxy){
gdb shows the following information:
14 int n = httplayer.openHttpSocket("google.com",8080,proxy);
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x0804940b in HttpLayer::openHttpSocket (
this=<error reading variable: Cannot access memory at address 0xbf37d81c>,
address=<error reading variable: Cannot access memory at address 0xbf37d818>, port=8080,
proxy=<error reading variable: Cannot access memory at address 0xbf37d814>)
at src/httplayer.cpp:20
20 int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &proxy){
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.
Your help is highly appreciated, Thank you!
EDIT:
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:
int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &proxy){
...
unsigned char proxyanswer[HTTP_BUF_SIZE];
...
}
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!
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?