分段错误 - char 指针

发布于 2024-07-22 18:44:38 字数 886 浏览 5 评论 0原文

在下面的代码中,行:

*end = *front;

给出了分段错误。 我在此处提出了类似的问题,但我不确定这是否是因为我有两份 num 的副本。 请解释一下为什么会出现段错误。 谢谢。

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

char* getPalin(char* num);

int main()
{
    char* num = (char*)malloc(100);

    num = "123456";

    printf("%s\n", getPalin(num) );

    return 0;
}

char* getPalin(char* num)
{
    int length = strlen(num);

    if ( length % 2 == 0 )
    {
        char* front = num;
        char* end = num + strlen(num) - 1;  //pointer to end

        while( front != num + (length/2) )  //pointers not middle yet
        {
            *end = *front;

            printf("%c", *end);

            front++;
            end--;
        }
    }

    return num;
}

In the code below, the line:

*end = *front;

gives a segmentation fault. I asked a similar question here but I'm not sure if this is because I have two copies of num. Please explain why it's seg-faulting. Thank you.

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

char* getPalin(char* num);

int main()
{
    char* num = (char*)malloc(100);

    num = "123456";

    printf("%s\n", getPalin(num) );

    return 0;
}

char* getPalin(char* num)
{
    int length = strlen(num);

    if ( length % 2 == 0 )
    {
        char* front = num;
        char* end = num + strlen(num) - 1;  //pointer to end

        while( front != num + (length/2) )  //pointers not middle yet
        {
            *end = *front;

            printf("%c", *end);

            front++;
            end--;
        }
    }

    return num;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

¢好甜 2024-07-29 18:44:38

这两行:

char* num = (char*)malloc(100);
num = "123456";

具有以下效果。

第一个分配 100 个字节并将 num 设置为指向这些字节。

第二个将 num 更改为指向字符串“123456”,该字符串几乎肯定位于只读内存中。

任何更改只读内存内容的尝试都会导致分段违规。 在尝试更改字符串之前,您需要将字符串复制到 mallocnum 中,其中:

strcpy (num, "123456");

这是您当前应该拥有的行:

num = "123456";

These two lines:

char* num = (char*)malloc(100);
num = "123456";

have the following effect.

The first allocates 100 bytes and sets num to point to those bytes.

The second changes num to point to the string "123456" which is almost certainly in read-only memory.

Any attempt to change the contents of read-only memory will result in a segmentation violation. You need to copy the string into the malloc'd num before attempting to change it, with:

strcpy (num, "123456");

That's the line you should have where you currently have:

num = "123456";
风蛊 2024-07-29 18:44:38

使用

strncpy(num, "123456", 100);

而不是

num = "123456";

Use

strncpy(num, "123456", 100);

instead of

num = "123456";
祁梦 2024-07-29 18:44:38

根据康斯坦丁的回答。

您已经使用 malloc 语句为 num 分配了内存。

如果没有,那么您可以逃脱:

char* num = "123456";

这将动态定义和分配内存,但它很可能被分配为常量,因此是只读的。

使用 strncpy 而不是 strcpy 复制“123456”将确保字符串 null 终止符末尾之外的任何额外空格也初始化为 null,只要您将 n 指定为 100(对于您的示例)。 否则,如果不将 malloc 分配的内存初始化为 null (memset(num, 0, 100)),则可以想象您可能会超出字符串末尾。

哦差点忘了。 建议使用 strcpy_s 或 strncpy_s 因为它们更安全,尽管对于您的代码来说这并不重要。

As per Konstantin's answer.

You have already allocated memory for num with the malloc statement.

If you hadn't then you could get away with:

char* num = "123456";

Which would define and allocate memory on the fly but it would most likely be allocated as a constant and thus read only.

Using strncpy rather than strcpy to copy "123456" will ensure that any extra space beyond the end of string null terminator is also initialised to null, as long as you specify n as being 100 (for your example). Otherwise without initialising the memory allocated by malloc to null (memset(num, 0, 100)) then it is conceivable that you could step beyond the end of the string.

Oh almost forgot. It is advisable to use strcpy_s or strncpy_s as they are more secure, although for your code it won't matter.

云淡风轻 2024-07-29 18:44:38

错误的原因是:

  char* num = (char*)malloc(100);

在这一行中,您已将 num 声明为指向数组的指针或指向其第一个元素的指针,而不是字符串。

 num = "123456";

这一行您使用了 num,因为您将其声明为字符串。 这是违反分段的,因此会出现分段错误。 您的代码的首选(正确)语法是:

   char num[100];
   strcpy(num,"123456"); //Even if you use num="123456"; here it would still be wrong

OR

  char* num = (char*)malloc(100);
  strcpy(num,"123456");

OR

  char num[100]={'1','2','3','4','5','6'};

其中任何一个都可以完成您的工作。

The reason for the error is :

  char* num = (char*)malloc(100);

In this line you have declared num as a pointer to an array or pointer to its first element not as a string.

 num = "123456";

This line you have used num as you declared it as a string. This is violation of segmentation and hence the seg fault. The preferrable(correct) syntax for your code is :

   char num[100];
   strcpy(num,"123456"); //Even if you use num="123456"; here it would still be wrong

OR

  char* num = (char*)malloc(100);
  strcpy(num,"123456");

OR

  char num[100]={'1','2','3','4','5','6'};

Any of these would do your work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文