gpt4 book ai didi

c++ - 从类函数中显示动态 C 字符串的段错误 11

转载 作者:太空宇宙 更新时间:2023-11-04 15:31:09 24 4
gpt4 key购买 nike

动态 c 字符串“textString”在由要显示的类成员函数使用时导致段错误 11。

#include "Text.h"
#include <iostream>
#include <cstring>
using namespace std;

Text::Text(const char* textArray)
{
textLength = strlen(textArray);
char* textString = new char[textLength];
strcpy(textString, textArray);
cout << textString << endl;
}

Text::~Text()
{
delete[] textString;
}

void Text::displayText() const
{
cout << textString << endl;
}

我提供的驱动程序:

#include <iostream>
#include "Text.h"

using namespace std;

int main()
{
Text *myString1;
char tvshow[1000];

cout << "\n\nWhat is the most recent TV show you watched?\n";
cin.getline(tvshow, 1000);
myString1 = new Text(tvshow);

cout << "\n\nTV SHOW:\t";
myString1->displayText();
cout << "\t# CHARACTERS:\t" << myString1->getLength() << endl;

return 0;
}

在 Text 类的构造函数中,行 cout << textString << endl;按预期工作。然而,当主函数调用myString1->displayText();时, bash 以 Segmentation fault: 11 结束程序。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

what Algirdas says 外,你的另一个问题是,你实际上没有分配类成员 textString (假设你有一个),而是在你的构造函数中隐藏它。将您的构造函数更改为如下内容:

Text::Text(const char* textArray)
{
textLength = strlen(textArray);
textString = new char[textLength + 1];
strcpy(textString, textArray);
cout << textString << endl;
}

注意 textString 前面缺少的 char *

我是怎么发现这个的?

与任何其他段错误一样,您应该学习如何调试。您最好的工具是 gdbvalgrind。用 valgrind 运行它给我:

$ echo franz | valgrind ./a.out
==6222== Memcheck, a memory error detector
==6222== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6222== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6222== Command: ./a.out
==6222==


What is the most recent TV show you watched?
franz


TV SHOW: ==6222== Invalid read of size 1
==6222== at 0x4C32CF2: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6222== by 0x108DB6: Text::getLength() const (a.cpp:37)
==6222== by 0x108E70: main (a.cpp:51)
==6222== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==6222==
==6222==
==6222== Process terminating with default action of signal 11 (SIGSEGV)
==6222== Access not within mapped region at address 0x0
==6222== at 0x4C32CF2: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6222== by 0x108DB6: Text::getLength() const (a.cpp:37)
==6222== by 0x108E70: main (a.cpp:51)
==6222== If you believe this happened as a result of a stack
==6222== overflow in your program's main thread (unlikely but
==6222== possible), you can try to increase the size of the
==6222== main thread stack using the --main-stacksize= flag.
==6222== The main thread stack size used in this run was 8388608.
==6222==
==6222== HEAP SUMMARY:
==6222== in use at exit: 14 bytes in 2 blocks
==6222== total heap usage: 5 allocs, 3 frees, 77,838 bytes allocated
==6222==
==6222== LEAK SUMMARY:
==6222== definitely lost: 6 bytes in 1 blocks
==6222== indirectly lost: 0 bytes in 0 blocks
==6222== possibly lost: 0 bytes in 0 blocks
==6222== still reachable: 8 bytes in 1 blocks
==6222== suppressed: 0 bytes in 0 blocks
==6222== Rerun with --leak-check=full to see details of leaked memory
==6222==
==6222== For counts of detected and suppressed errors, rerun with: -v
==6222== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
fish: Process 6222, “valgrind” “echo franz | valgrind ./a.out” terminated by signal SIGSEGV (Address boundary error)

您可以清楚地看到,崩溃发生在 getLength 中,因为它试图访问地址 0x0。当您使用 gdb 运行它时,您将恰好在那个点停止并且可以看到 print textString,它是 0 并且没有正确初始化。

请阅读What is a debugger and how can it help me diagnose problems?了解更多信息!

关于c++ - 从类函数中显示动态 C 字符串的段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52277563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com