【问题标题】:I have a segmentation fault (core dumped) when using strcpy, malloc, and struct [duplicate]使用 strcpy、malloc 和 struct 时出现分段错误(核心转储)[重复]
【发布时间】:2013-10-29 03:40:33
【问题描述】:

好的,当我运行这段代码时,我遇到了分段错误:

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
#define MAX 64

struct example {
    char *name;
};

int main()
{
    struct example *s = malloc (MAX); 
    strcpy(s->name ,"Hello World!!");
    return !printf("%s\n", s->name);
}

终端输出:

alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ make q1
cc -Wall -g    q1.c   -o q1
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ ./q1
Segmentation fault (core dumped)
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ gedit q1.c

有人可以解释发生了什么吗?

【问题讨论】:

  • 您希望字符串的大小为 MAX?

标签: c segmentation-fault


【解决方案1】:

你可能已经为你的结构分配了内存,但没有为它的字符指针分配内存。

您不能对未分配的内存执行 strcpy。你可以说

s->name = "Hello World"; 

改为。

或者,为您的 char 分配内存,然后执行复制。 注意:我绝不认可以下代码是好的,只是它会起作用。

int main()
{
  struct example *s = malloc(MAX);
  s->name = malloc(MAX);
  strcpy(s->name ,"Hello World!!");
  return !printf("%s\n", s->name);
}

编辑:这可能是一个更简洁的实现,但我仍然讨厌 C 风格的字符串

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define KNOWN_GOOD_BUFFER_SIZE 64

typedef struct example {
  char *name;
} MyExample;

int main()
{
  MyExample *s = (MyExample*) malloc( sizeof(MyExample) );
  s->name = (char*) malloc(KNOWN_GOOD_BUFFER_SIZE);
  strcpy(s->name ,"Hello World!!");
  printf("%s\n", s->name);
  free(s->name);
  free(s);
  return 0;
}

【讨论】:

    【解决方案2】:

    您正在为结构分配内存,但 char *name 仍指向未初始化的内存。您还需要为 char * 分配内存。如果你希望它是一个最大大小为 64 的字符串,你可以在创建后更改,试试这个:

    #include<stdio.h> 
    #include<stdlib.h>
    #include<string.h>
    #define MAX 64
    
    struct example {
        char *name;
    };
    
    int main()
    {
        struct example *s = malloc(sizeof(struct example)); 
        s->name = malloc(MAX * sizeof(char));
        strcpy(s->name ,"Hello World!!");
        return !printf("%s\n", s->name);
    }
    

    请注意,我只将 MAX 分配给 char *。示例结构只需为 sizeof(struct example)),因此将其设为 MAX 是没有意义的。这是一种更好的方法,因为即使您更改示例结构的成员,您的 malloc 也会不断为您提供正确的大小。

    【讨论】:

      【解决方案3】:

      问题是您正在为s 分配内存,而不是为s-&gt;name 分配内存,因此您正在对未初始化的指针undefined behavior 执行间接寻址。假设您真的想为一个struct example 分配空间,并且您希望您的字符串大小为MAX,那么您需要以下分配:

      struct example *s = malloc (sizeof(struct example)); 
      s->name = malloc(MAX*sizeof(char)) ;
      

      注意使用operator sizeof 来确定要为其分配内存的数据类型的正确大小。

      【讨论】:

        【解决方案4】:
        struct example *s = malloc (MAX); 
        

        那条线指向一个能够容纳 64 个示例结构的内存。每个示例结构只有足够的内存来保存一个指针(称为名称)。

        strcpy(s->name ,"Hello World!!");
        

        这是无效的,因为 s->name 没有指向任何地方,它需要指向分配的内存。

        你可能想要:

        struct example *s = malloc(sizeof(struct example)); //Allocate one example structure
        s->name = malloc(MAX); //allocate 64 bytes for name 
        strcpy(s->name,"Hello world!"); //This is ok now.
        

        这与:

        struct example s;  //Declare example structure
        s.name = malloc(MAX);  //allocate 64 bytes to name
        strcpy(s.name,"Hello world!");
        

        【讨论】:

          猜你喜欢
          • 2018-10-13
          • 1970-01-01
          • 2013-12-27
          • 2020-02-28
          • 1970-01-01
          • 2021-03-17
          • 1970-01-01
          • 1970-01-01
          • 2015-08-10
          相关资源
          最近更新 更多