简体   繁体   English

程序收到信号 SIGSEGV,分段错误。 C++

[英]Program received signal SIGSEGV, Segmentation fault. C++

I'm getting this error (*s = *end; line) during debugging, while trying to reverse string using pointers.我在调试期间遇到此错误(*s = *end; line),同时尝试使用指针反转字符串。 I'm using Windows 10 OS, codeblocks IDE and GDB debugger.我正在使用 Windows 10 操作系统,代码块 IDE 和 GDB 调试器。

#include <stdio.h>
#include <string.h>
#include <limits.h>

void myreverse(char* s);

int main()
{
    char* s1 = "1234";
    myreverse(s1);
    printf("%s", s1);
    return 0;
}

void myreverse(char* s) {
    char tmp;
    char* end = s + strlen(s) - 1;

    for(; s < end; s++, end--) {
        tmp = *s;
        *s = *end;
        *end = tmp;
    }
}

You should change s1 to char s1[] = "1234";您应该将s1更改为char s1[] = "1234"; since you are making changes to the string.因为您正在更改字符串。

Then in your myreverse() function, you never use the tmp variable, which makes your swap block failing.然后在您的myreverse() function 中,您永远不会使用tmp变量,这会使您的交换块失败。

Fixed:固定的:

#include <cstdio>   // use the C++ versions of the header files
#include <cstring>

void myreverse(char* s) {
    char tmp;
    char* end = s + std::strlen(s) - 1;

    for(; s < end; s++, end--) {
        // swap
        tmp = *s;
        *s = *end;
        *end = tmp;   // use tmp
    }
}

int main() {
    char s1[] = "1234";
    myreverse(s1);
    printf("%s", s1);
}

Note that the 3 lines in the swap block can be replaced with std::swap(*s, *end);请注意,交换块中的 3 行可以替换为std::swap(*s, *end); and also that myreverse() can be completely replaced with std::reverse(std::begin(s1), std::end(s1));而且myreverse()可以完全替换为std::reverse(std::begin(s1), std::end(s1)); . .

暂无
暂无

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

相关问题 “程序接收信号SIGSEGV,分段故障。在??? ()()“在Code :: Blocks中调试我的C ++项目时 - “Program received signal SIGSEGV, Segmentation fault. In ?? () ()” when debugging my C++ project in Code::Blocks Python-程序收到信号SIGSEGV,分段错误 - Python - Program received signal SIGSEGV, Segmentation fault 程序收到信号SIGSEGV,分段错误 - program received signal SIGSEGV, segmentation fault 程序接收到信号SIGSEGV,输出出现分段故障 - Program received signal SIGSEGV, Segmentation fault in output 程序接收信号 SIGSEGV,Segmentation fault - Program received signal SIGSEGV, Segmentation fault 程序收到信号SIGSEGV,分段故障。 在al_draw_tinted_bitmap中(位图= 0x0,色调= ...,dx = 0,dy = 0,标志= 0) - Program received signal SIGSEGV, Segmentation fault. in al_draw_tinted_bitmap (bitmap=0x0, tint=…, dx=0, dy=0, flags=0) C++ - 程序收到信号 SIGSEGV,分段错误。在 msvcrt!memcpy () (C:\\Windows\\System32\\msvcrt.dll) - C++ - Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll) strcpy 原因程序收到信号SIGSEGV,分段错误 - strcpy cause Program received signal SIGSEGV, Segmentation fault 程序接收到的信号SIGSEGV,吸气方法中的分段错误 - Program received signal SIGSEGV, Segmentation fault in getter method 编程接收到的信号SIGSEGV,在代码块中调试时出现分段错误 - Program received signal SIGSEGV, Segmentation fault while debugging in codeblocks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM