代码之家  ›  专栏  ›  技术社区  ›  David Geismar

尝试从一个内存位置复制到另一个内存位置时出现分段错误

  •  0
  • David Geismar  · 技术社区  · 6 年前

    我必须编写一个函数来重现c中strcpy的行为 https://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm

    如果我正确理解这个函数,它会将字符串从源内存位置复制到目标内存位置,并返回指向目标内存位置的指针。

    下面是我生成的代码:

    #include <stdio.h>
    
    char *ft_strcpy(char *dest, char *src)
    {
      char *mem = dest;
      while (*src != '\0')
      {
        *dest = *src;
        src++;
        dest++;
      }
      *dest = '\0';
      return mem;
    }
    
    int main()
    {
      char word[5] = "word";
      printf("%s \n", word);
      printf("word address is %p \n", word);
      char *dest;
      printf("dest address is %p", dest);
      ft_strcpy(dest, word);
      while (*dest != '\0'){
        printf("%c", *dest);
        dest++;
      }
    }
    

    为了测试我的代码,我声明了一个字符数组word,其中包含“word”和一个指针*dest。

    然而,当我运行我的代码,我得到一个59873分段错误。我想是这条线导致了这个错误:

    *dest = *src;
    

    有人能解释一下这个代码出了什么问题吗

    3 回复  |  直到 6 年前
        1
  •  4
  •   John3136    6 年前

    你从不放弃 dest 一个值,所以它的值是未定义的。因此,您的程序具有未定义的行为。更具体地说: char* dest; 只是给你一个“指向一个字符的指针”,但实际上并没有设置值。

    char c = 'A';
    char *dest = &c;
    

    尽管如此)。你需要的是指出 有足够大的内存。您可以使用动态内存分配:

    dest = malloc(32); // 32 is a randomly selected value
    // don't forget a NULL check and to free() once done.
    

    char block[10]; // 10 is randomly selected to work in your example
    char *dest = block;
    

    char dest[10] = { 0 }; // initializes all to 0
    // but now you can't do "dest++" etc.
    

    比如:

    int main()
    {
      char word[5] = "word";
      char block[10]; // 10 is randomly selected to work in your example
      char *dest = block;
    
      printf("%s \n", word);
      printf("word address is %p \n", word);
    
      printf("dest address is %p", dest)
      ft_strcpy(dest, word);
      ...
    
        2
  •  1
  •   Abigail    6 年前
    char *dest;
    printf("dest address is %p", dest);
    ft_strcpy(dest, word);
    

    你的第一个问题是 dest ft_strcpy printf

    目的地 需要一个指向足够大的内存的指针 word

    char *dest = malloc(strlen(word) + 1)
    

    如果我们分配 单词 +空终止符为1字节, 将正常工作。

    那你只需要记住免费使用

    free(dest);
    

    你唯一的问题是 ft\ U结构 *dest 什么时候 目的地

        3
  •  0
  •   new_learner    6 年前
    1 . Your *dest is dangling pointer in main so first you need to store some valid address into it by using malloc() or other method .
    2 . Storing address of string from code section in array is bad programming practice .
    3 . Check the modified code .
    
    #include <stdio.h>
    #include <string.h>  /* for strcpy */
    #include <stdlib.h> /* for malloc */ 
    char *ft_strcpy(char *dest, char *src)  
    {  
      char *mem = dest;  
      while (*src != '\0')  
      {  
        *dest = *src;  
        src++;
        dest++;
      }
      *dest = '\0';
      return mem;
    }
    
    int main()
    {
      char word[5]; strcpy(word,"word");
      printf("%s \n", word);
      printf("word address is %p \n", word);
      char *dest=malloc(strlen(word)+1); //+1 for null terminating character .
      printf("dest address is %p", dest);
      char *temp=dest;
      ft_strcpy(dest, word);
      while (*dest != '\0'){
        printf("%c", *dest);
        dest++;
      }
    free(temp);
    }