写入字符串时出现分段错误 [英] Segmentation Fault when writing to a string

查看:26
本文介绍了写入字符串时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个就地反向函数,并且几乎完全遵循在线代码,但运行以下程序会引发总线错误.我是否向 reverse() 传递了错误的参数?

I am trying to write an in-place reverse function and have followed online code pretty much exactly, yet running the following program throws a bus error. Am I passing the wrong kind of argument to reverse()?

void reverse(char *str) {
    char * end = str;
    char tmp;
    if (str) {
        while (*end) {
            ++end;
        }
        --end;
        while (str < end) {
            tmp = *str;
            *str++ = *end;
            *end-- = tmp;
        }
    }
}

int main() {
    char *s = "sample";
    reverse(s);
    printf("%s
");
    return 1;
}

推荐答案

要知道发生了什么,您必须了解 C 程序的内存布局.

To know what is happening, you have to understand the memory layout of a C program.

char *s = "sample";    // Here the "sample" string is placed in 
                        // the read only memory of the Initialized Data segment. 

在这里,您不能修改数据."s" 是一个指向 char const("sample") 的指针,您正试图修改 char const.这就是您收到 bus error 错误的原因.

Here, you cannot modify the data. "s" is a pointer to a char const("sample") and you are trying to modify the char const. That is why you are getting the bus error error.

                        |Stack frame of main()          |
                        |char *s                        |
                        |-------------------------------|
                        |Stack frame of reverse()       |
                        |char *end                      |
                        |char tmp                       |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |                               |
                        |                               |
                        |                               |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |           HEAP                |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |   UNINITIALIZED DATA (BSS)    |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |      INITIALIZED DATA         |
                        |                               |
                        |"sample"   |                   |
                        |           |                   |
                        |(Read Only)| (Read/Write)      |
                        |-------------------------------|
                        |   Text or Code Segment        |
                        |                               |
                        |-------------------------------|

更新以下帖子与您的问题无关.但是,如果您知道 C 中为所有变量分配的内存在哪里,那么您可以更好地编写代码.下面的程序可以更好地理解 C 程序的内存布局.我没有在图中包含函数的命令行参数、函数参数和返回值.想要更新这个帖子的人可以将命令行参数、函数参数和函数的返回值添加到图表中.

UPDATE Below post is not related to your question. But if you know where are the memory allocated for all the variables in C, then you can code better. The below program gives a better understanding of the memory layout of a C Program. I have not included the command line arguments, function argument and return values of function in the diagram. People who want to update this post can add the command line arguments, function argument and return values of function to the diagram.

|Stack frame of main()              |               
|local_To_Main                      |
|                                   |   #include <stdio.h>
|-----------------------------------|   #include <stdlib.h>
|Stack frame of function1()         |   int gVariable1 = 100;
|local_To_Function1                 |   int gVariable2;
|iptr                               |   char cstring[10] = "Hello";
|                    STACK         |   char* cptr = "Hello World";
|---------------------|------------|   void function1(void)
|                    |/           |   {
|                                  |       static int j = 5;
|                                  |       int local_To_Function1;
|                           ^      |       int *iptr;
|                           |      |       iptr = (int *) malloc(sizeof(int));
|---------------------------|------|       free(iptr);
|   HEAP             ---           |   }
|              ---> |int|          |   
|                     ---           |   int main(void)
|-----------------------------------|   {
|                                   |       static int i;
|   UNINITIALIZED DATA (BSS)        |       int local_To_Main;
|gVariable2(initialized to 0)       |   
|i (initialized to 0)               |
|-----------------------------------|       function1();
|                                   |       return 0;
|      INITIALIZED DATA             |   }
|                                   |
|"Hello World"  |gVariable1 =100    |
|       ^       |cstring="Hello"    |
|       |       |j=5                |
|       |---<---<---- cptr          |
|(Read Only)    | (Read/Write)      |
|-----------------------------------|
|   Text or Code Segment            |
|                                   |
|-----------------------------------|

这篇关于写入字符串时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆