简体   繁体   English

如何解决此C ++访问冲突问题?

[英]how do I solve this C++ access violation problem?

I'm getting an error in the following code. 我在以下代码中遇到错误。 Visual Studio throws an access violation error when writing to _buf . 写入_buf时,Visual Studio引发访问冲突错误。 How can I fix this? 我怎样才能解决这个问题?

The Sendn function is a socket sending function. Sendn函数是套接字发送函数。 It's not the problem, you can ignore it. 这不是问题,您可以忽略它。

It looks like _buf points at 0x00000000 看起来_buf指向0x00000000

The error message I'm seeing is 我看到的错误消息是

 0xC0000005: 0x00000000 : access violation 
void ?????::?????(int number, string title)
{

    int titlesize = sizeof(title);
    int bufsize = 4 + 4 + 4 + titlesize;

    char *_buf = new char[bufsize];

    _buf = { 0 };

    // char _buf[bufsize] = { 0 }; (수정 내용)

    int commands = 3;

    int index = 0;
    memcpy(_buf, &commands, sizeof(int));
    index += sizeof(int);

    memcpy(_buf + index, &number, sizeof(int));
    index += sizeof(int);

    memcpy(_buf + index, &titlesize, sizeof(int));
    index += sizeof(int);
    for (int i = 0; i < titlesize; i++)
    {
        memcpy(_buf + index, &title[i], sizeof(char));
        index += sizeof(char);
    }

    Sendn(_buf, bufsize);

    delete[] _buf;

    return;
}
char *_buf = new char[bufsize];
_buf = { 0 };

This does not zero-fill the dynamically-allocated array pointed to by _buf . 这不会对_buf指向的动态分配数组进行零填充。 It sets the pointer _buf to be a null pointer. 它将指针_buf设置为空指针。 Since _buf is a null pointer, later attempts to dereference it lead to undefined behavior. 由于_buf是空指针,因此以后尝试取消引用它会导致未定义的行为。

There's no need to zero-fill the array pointed to by _buf in this case, so you can simply remove the _buf = { 0 }; 在这种情况下,无需对_buf指向的数组进行零填充,因此只需删除_buf = { 0 }; line. 线。


Once you've fixed that problem, you also aren't allocating the right amount of memory. 解决此问题后,您也不会分配适当的内存量。 sizeof(title) will not give you the number of characters that title holds. sizeof(title)不会给您提供title保留的字符数。 It just gives you the static size of a std::string object, which is usually only a pointer and two integers. 它只是为您提供了std::string对象的静态大小,该对象通常只是一个指针和两个整数。 Use title.size() instead. 使用title.size()代替。

You're trying to copy the content of title together with 3 other integer numbers into _buf right? 您正在尝试将title的内容以及其他3个整数复制到_buf对吗? The problem is that sizeof(title) is not the length of the string stored in title . 问题在于sizeof(title)不是存储在title中的字符串的长度。 In order to get the length of title , you need to call the member function length on type std::string like this: 为了获得title的长度,您需要像这样在std::string类型上调用成员函数的length

auto titlesize = title.length();

The sizeof operator only gives you the size of your std::string object on stack (in comparison, the actual string is stored on heap) and sizeof expressions are always constant expressions. sizeof运算符只为您提供std::string对象在堆栈上的大小(相比之下,实际的字符串存储在堆上),而sizeof表达式始终是常量表达式。 On my computer, sizeof(std::string) is 24 regardless of what the actual string is. 在我的计算机上,无论实际字符串是什么, sizeof(std::string)均为24。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM