gpt4 book ai didi

c++ - 应用程序中的运行时错误 - 分段失败

转载 作者:行者123 更新时间:2023-11-30 01:20:39 25 4
gpt4 key购买 nike

我正在处理在偶数位置显示字符的简单应用程序,但是当我输入时抛出运行时错误“应用程序收到 SIGSEGV 信号。分段失败”。我不知道哪里错了。我多次分析了我的代码,但找不到任何相关的解决方案。

我的代码:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string s;
scanf("%s",&s);
int length = s.length();
for(int i = 0; i < length; i++)
{
if(i%2==0)printf("%s",s[i]);
}
return 0;
}

GDB 调试器信息:

**
->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Reading symbols from C:\Users\user\Desktop\par.exe...done.

->->breakpoints-invalid

->->breakpoints-invalid

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Source directories searched: C:/Program Files/Dev-Cpp/MinGW32/lib;$cdir;$cwd

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Source directories searched: C:/Program Files/Dev-Cpp/MinGW32/include;C:/Program Files/Dev-Cpp/MinGW32/lib;$cdir;$cwd

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Source directories searched: C:/Program Files/Dev-Cpp/MinGW32/include;C:/Program Files/Dev-Cpp/MinGW32/include;C:/Program Files/Dev-Cpp/MinGW32/lib;$cdir;$cwd

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Working directory C:\Users\user\Desktop.

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Starting program: C:\Users\user\Desktop\par.exe
[New Thread 5328.0x13c0]

->->new-thread

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->frames-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->breakpoints-invalid

->->starting

->->signal

Program received signal
->->signal-name
SIGSEGV
->->signal-name-end
,
->->signal-string
Segmentation fault
->->signal-string-end
.

->->frame-begin 0 0x41f036

->->frame-address
0x0041f036
->->frame-address-end
in
->->frame-function-name
std::string::length() const
->->frame-args
()

->->frame-end

->->stopped

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->error

->->pre-prompt
(gdb)
->->prompt

->->error-begin
No symbol "include" in current context.

->->post-prompt
eax 0x64616441 1684104257
ecx 0x22ff04 2293508
edx 0x77a60958 2007370072
ebx 0x7ffdf000 2147348480
esp 0x22feec 0x22feec
ebp 0x22ff18 0x22ff18
esi 0x0 0
edi 0x0 0
eip 0x41f036 0x41f036 <std::string::length() const+2>
eflags 0x10206 [ PF IF RF ]
cs 0x1b 27
ss 0x23 35
ds 0x23 35
es 0x23 35
fs 0x3b 59
gs 0x0 0

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Dump of assembler code for function _ZNKSs6lengthEv:
0x0041f034 <+0>: mov (%ecx),%eax
=> 0x0041f036 <+2>: mov -0xc(%eax),%eax
0x0041f039 <+5>: ret
0x0041f03a <+6>: nop
0x0041f03b <+7>: nop
End of assembler dump.

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->frame-begin 0 0x41f036
#0
->->frame-address
0x0041f036
->->frame-address-end
in
->->frame-function-name
std::string::length() const
->->frame-args
()

->->frame-end

->->frame-begin 1 0x4013ee
#1
->->frame-address
0x004013ee
->->frame-address-end
in
->->frame-function-name
main
->->frame-args
()
->->frame-source-begin
at
->->frame-source-file
C:\Users\user\Desktop\par.cpp
->->frame-source-file-end
:
->->frame-source-line
11
->->frame-source-end


->->frame-end

->->pre-prompt
(gdb)
->->prompt

->->post-prompt

->->error

->->pre-prompt
(gdb)
->->prompt

->->error-begin
No symbol "length" in current context.
**

CPU 信息:

   0x0041f034 <+0>: mov    (%ecx),%eax
=> 0x0041f036 <+2>: mov -0xc(%eax),%eax
0x0041f039 <+5>: ret
0x0041f03a <+6>: nop
0x0041f03b <+7>: nop

最佳答案

scanf 设计用于 char 数组而不是 std::string,并且要打印一个字符,您必须使用 %c 而不是 %s:

string s;
scanf("%s", &s);
^^^^
//Undefined behavior invokes!

...

printf("%s",s[i]);
^^^^
//Undefined behavior again!

如果你真的想使用scanf,你应该使用中间的char数组:

char str[BIG_ENOUGHT];
scanf("%s", &str);

std::string s(str);

最好改用cincout:

cin >> s;

...

cout << s[i];

关于c++ - 应用程序中的运行时错误 - 分段失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19078002/

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