c++ - 为什么此 C++ 成员函数调用会因段错误而失败?

标签 c++ linux function struct segmentation-fault

我关注这个论坛很多年了,发现它非常有用,回答了我目前所有的问题。但是今天我好像卡住了。

作为 C++ 的新手,我尝试编写一些类来帮助我通过代理服务器连接到网站。因此,我调用了一个封装了一些逻辑的类的函数来处理 HTTP 请求。我通过引用将一个结构和另外两个参数传递给该函数。执行失败并出现段错误 (duh)。调试显示调用我的函数时出现了Segmentation Fault。

在我的主函数中,我创建了一个在 httplayer.hpp 中声明的类的实例,并像这样调用其中一个成员:

#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;
}

httplayer.hpp 文件如下所示:

#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;
};

在相应的httplayer.cpp中终于有了这个功能:

int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &proxy){  

gdb 显示以下信息:

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){

我的第一个怀疑是结构,但到目前为止还不能看出我的错误。我是 C++ 的新手,所以我可能会在使用类、函数或头文件的方式上犯一些非常明显的错误,但我自己似乎无法发现。

非常感谢您的帮助,谢谢!

编辑:

当然只有我作为一个 C++ 菜鸟是对的,有经验的社区肯定是错误的。 所以我为了证明我的无限智慧所做的是注释掉 openHttpSocket 的内容,除了 printf()... 但突然它起作用了。所以我开始重新包含部分代码,直到我偶然发现了这一行:

int HttpLayer::openHttpSocket(std::string address,int port,proxy_data &proxy){
    ...
    unsigned char proxyanswer[HTTP_BUF_SIZE];
    ...
}

好吧,所有功劳都归功于@WhozCraig 的 Crystal 球。以及所有其他要求我发布功能内容的人。谢谢!

现在另一个初学者的问题:我的堆栈在这里发生了什么?以及如何创建某种可以在 C++ 的成员函数中使用的 char* 缓冲区?或者在 C++ 中是否有一个完全不同的概念可以用来代替那些好的旧字符数组?

最佳答案

HTTP_BUF_SIZE 为 650 万。这意味着这个函数范围的变量声明:

unsigned char proxyanswer[HTTP_BUF_SIZE];

尝试在堆栈上创建一个大约 6MB 的缓冲区。这将在大多数默认设置下溢出堆栈。

你不想让这么大的对象在栈上,你想让它们在堆上。在 C++ 术语中,您不希望它们在函数中具有自动存储持续时间,您希望它们动态分配。创建动态数组的 C++ 方法是:

std::vector<unsigned char> proxyanswer(HTTP_BUF_SIZE);

这会动态分配内存(“在堆上”)并且几乎可以用作普通数组(支持 [] 下标等)。如果您需要作为指针访问实际缓冲区(例如将其传递给 C API),您可以使用 proxyanswer.data() (C++11及更高版本)或 &proxyanswer.front()(C++03 及以下版本)。

关于c++ - 为什么此 C++ 成员函数调用会因段错误而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28083347/

相关文章:

linux - 提交后忽略 Git 中的文件

linux - 使用带换行符的 Linux

C 程序在函数中获取无效转换

c++ - 字符串/整数 vector - cout

c++ - 使用C++中的模板检测不同的字符串文字

c++ - ubuntu上无法链接protobuf

linux - 通过 Linux bash 读取远程文件并使用远程机器本身的内容

function - solr:使用函数创建过滤器查询

javascript - 函数与对象构造函数

c++ - 当#define 在 #if 中时, token "#"错误之前缺少二元运算符