复制内存的时候,为什么段错误 [英] why segmentation fault when copying memory

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

问题描述

我运行Ubuntu上x86_32 ...我不断收到分段错误运行此程序时。

 在此输入code
#包括LT&;&stdio.h中GT;
#包括LT&;&STDDEF.H GT;
字符* memcp(字符* DEST,为const char * SRC,为size_t N)
{    字符* DP = DEST;
    为const char * SP = SRC;
    而(N--)
        * DP ++ = * SP ++;
    返回DEST;}诠释的main()
{    字符* S =ABCDE;
    字符* D;
    字符* R = memcp(D,S,6);
    的printf(%S,R);    返回(0);
}

本code的问题是,它是我的朋友的x86_64的机器上运行在Windows和Ubuntu Linux系统。请帮我..


解决方案

有至少两种方法可以做到这一点:

的malloc 方法:

  INT主要(无效)
{
    字符* S =ABCDE;
    字符* D =的malloc(6);
    字符* R = memcp(D,S,6);
    的printf(%S,R);    免费(D);
    返回(0);
}

阵列方式:

  INT主要(无效)
{
    字符* S =ABCDE;
    字符D [6];
    memcp(D,S,6);    返回0;
}

请注意,这通常不是一个好主意,硬code缓冲区长度到code(例如,你是硬编码6)。如果你输入的大小变化,你忘记更新6号,会出现问题。

为什么你得到一个分割故障的原因是因为指针 D 不指向任何地方。在你的 memcp 功能,你试图写这个指针,但因为它没有任何地方点有意义的程序崩溃。在C标准,这就是所谓的未定义行为的,基本上就意味着任何事情都可能发生。

此外,它可能会感兴趣,目前已经有两个可用的功能标准C库中,的 memmove与 和的 的memcpy memmove与如果源和目标区域重叠是非常有用的。如果你知道他们永远不会重叠,的memcpy 可能会更快。

最后,我想指出的是,你不应该采取从阿图尔·关于未初始化的指针使用的建议。你永远不应该依赖于未初始化的指针的值,这样做意味着你的程序的行为的的明确界定。 C语言规范的附录J提到,就是未定义行为以下内容:


  

J.2未定义行为


  
  

      
  1. 的行为是未定义在以下情况:
      

        
    • …

    •   
    • 具有自动存储持续时间的对象的值被使用,而这是不确定的。

    •   
    • …

    •   

  2.   

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天全站免登陆