简体   繁体   English

Strcpy分段故障C.

[英]Strcpy Segmentation Fault C

I am learning some new things and get stuck on a simple strcpy operation. 我正在学习一些新东西并陷入简单的strcpy操作。 I don't understand why first time when I print works but second time it doesn't. 我不明白为什么我第一次打印作品,但第二次不打印。

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

int main()
{
    char *name;
    char *altname;

    name=(char *)malloc(60*sizeof(char));
    name="Hello World!";
    altname=name;
    printf("%s  \n", altname);
    altname=NULL;
    strcpy(altname,name);
    printf("%s  \n", altname);
    return 1;
}

The problems start here: 问题从这里开始:

name=(char *)malloc(60*sizeof(char));
name="Hello World!";

You replaced the value returned by malloc with a string literal. 您使用字符串文字替换了malloc返回的值。

  1. You leaked memory (since you can't regain the pointer value returned by malloc ). 你泄露了内存(因为你无法重新获得malloc返回的指针值)。 All calls to malloc are matched with a corresponding call to free . malloc所有调用都与相应的free调用相匹配。 Since that pointer value is gone, the opportunity to call free with that pointer value is also gone. 由于该指针值消失了,因此使用该指针值调用free的机会也消失了。

  2. You further on write to a NULL pointer, which is undefined behavior (which in your case, produced a segmentation fault). 您还要写入NULL指针,这是未定义的行为(在您的情况下,会产生分段错误)。

You need to allocate memory for altname : 您需要为altname分配内存:

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

int main()
{
    char *name;
    char *altname;

    name=(char *)malloc(60*sizeof(char));
    name="Hello World!";
    altname=name;
    printf("%s  \n", altname);
    altname=NULL;
    // allocate memory, so strcpy has space to write on ;)
    altname=(char *)malloc(60*sizeof(char));
    strcpy(altname,name);
    printf("%s  \n", altname);
    return 1;
}

The first time, you are making altname point to the same place as name . 第一次,您将altname指向与name相同的位置。 This is OK, because name points to a valid char* (the first element ofthe "Hello World!" literal) 这没关系,因为name指向一个有效的char*"Hello World!"文字的第一个元素)

// both point to beginning of "Hello World!" literal
altname=name;

The second time, you attempt to copy the data pointed at by name into the place pointed at by altname , which at this stage points to NULL. 第二次,您尝试将name指向的数据复制到altname指向的altname ,该位置在此阶段指向NULL。 So you attempt to write to NULL, which is the source of the error. 所以你试图写入NULL,这是错误的来源。

strncpy requires that the destination buffer be writable, and large enough to copy the source string's data into. strncpy要求目标缓冲区是可写的,并且足够大以将源字符串的数据复制到其中。 You need to make altname point to a buffer that is large enough for the contents of the string name points to. 您需要使altname指向一个足够大的缓冲区,以便字符串name的内容指向。

altname = (char*)malloc(60*strlen(name)+1); // +1 for nul terminator
strcpy(altname, name);

Also note that when you set name = "Hello World!" 还要注意,当你设置name = "Hello World!" , you leak the memory it originally pointed to. ,你泄漏它最初指向的内存。 You need to free that first: 你需要先释放它:

free(name);
name = "Hello World!";

You are trying to assign value to altname which has no space to store. 您正在尝试为没有空间存储的altname分配值。 First allocate memory to altname then assign 首先将内存分配给altname然后分配

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

int main()
{
    char *name;
    char *altname;

    name=(char *)malloc(60*sizeof(char));
    name="Hello World!";
    altname=name;
    printf("%s  \n", altname);
    altname=NULL;
    altname=(char *)malloc(sizeof(name)); // allocate memory
    strcpy(altname,name);                 // Now assign 
    printf("%s  \n", altname);
    return 1;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM