为什么复制内存时出现分段错误 [英] why segmentation fault when copying memory

查看:29
本文介绍了为什么复制内存时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 x86_32 上运行 ubuntu...并且在运行此程序时不断出现分段错误.

在此输入代码#include<stdio.h>#include<stddef.h>char *memcp(char *dest, const char *src, size_t n){字符 *dp = 目标;常量字符 *sp = src;而(n--)*dp++ = *sp++;返回目的地;}主函数(){字符 *s = "abcde";字符 *d;字符 *r = memcp(d,s,6);printf("%s",r);返回(0);}

这段代码的问题是它运行在我朋友的 x86_64 机器上的 windows 和 ubuntu 上.请帮帮我..

解决方案

至少有两种方法:

malloc方法:

int main(void){字符 *s = "abcde";字符 *d = malloc(6);char *r = memcp(d, s, 6);printf("%s",r);免费(d);返回(0);}

数组方法:

int main(void){字符 *s = "abcde";字符 d[6];memcp(d, s, 6);返回0;}

请注意,将缓冲区长度硬编码到代码中通常不是一个好主意(例如,您正在硬编码 6).如果输入的大小发生变化而忘记更新数字 6,就会出现问题.

出现分段错误的原因是指针 d 没有指向任何地方.在您的 memcp 函数中,您正在尝试编写此指针,但因为它没有指向任何有意义的程序崩溃的地方.在 C 标准中,这称为未定义的行为,基本上它意味着任何事情都可能发生.

另外,您可能会感兴趣,标准 C 库中已经有两个函数可用,memmovememcpy.如果源和目标区域重叠,memmove 很有用.如果你知道它们永远不会重叠,memcpy 可能会更快.

最后我想指出,你不应该接受 Artur 关于未初始化指针使用的建议.您永远不应该依赖未初始化指针的值,这样做意味着您的程序的行为是没有明确定义的.C 语言规范的附件 J 提到了以下未定义的行为:

<块引用>

J.2 未定义行为

  1. 在以下情况下行为未定义:
    • 具有自动存储期限的对象的值在不确定时使用.

I'm running ubuntu on x86_32...and I keep getting segmentation fault while running this program.

enter code here
#include<stdio.h>
#include<stddef.h>
char *memcp(char *dest, const char *src, size_t n)
{

    char *dp = dest;
    const char *sp = src;
    while(n--)
        *dp++ = *sp++;
    return dest;

}

int main()
{

    char *s = "abcde";
    char *d;
    char *r = memcp(d,s,6);
    printf("%s",r);

    return(0);
}

The problem with this code is that it is running on my friend's x86_64 machine on windows as well as ubuntu. Please help me out ..

解决方案

There's at least two ways to do this:

malloc method:

int main(void)
{
    char *s = "abcde";
    char *d = malloc(6);
    char *r = memcp(d, s, 6);
    printf("%s",r);

    free(d);
    return(0);
}

Array method:

int main(void)
{
    char *s = "abcde";
    char d[6];
    memcp(d, s, 6);

    return 0;
}

Note that it is generally not a good idea to hard code buffer lengths into your code (for example, you are hardcoding 6). If the size of your input changes and you forget to update the number 6, problems will occur.

The reason why you are getting a segmentation fault is because the pointer d does not point to anywhere. In your memcp function, you are trying to write this pointer but because it does not point anywhere meaningful your program crashes. In the C standard, this is called undefined behaviour, and basically it means anything can happen.

Also, it may interest you that there are already two functions available in the Standard C Library, memmove and memcpy. memmove is useful if the source and destination areas overlap. If you know they won't ever overlap, memcpy may be faster.

Lastly I would like to point out that you should not take the advice from Artur regarding uninitialised pointer usage. You should never rely on the value of an uninitialised pointer and doing so means your program's behaviour is not well-defined. Annex J of the C language specification mentions the following that is undefined behaviour:

J.2 Undefined Behaviour

  1. The behavior is undefined in the following circumstances:
    • The value of an object with automatic storage duration is used while it is indeterminate.

这篇关于为什么复制内存时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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