简体   繁体   English

C中的字符串副本导致分段错误

[英]String copy in C causing segmentation fault

I am trying to copy a string into another char pointer variable using strcpy function.But I always get segmentation fault.Here is my code. 我试图使用strcpy函数将字符串复制到另一个char指针变量。但我总是得到分段错误。这是我的代码。

/* strcat example */
#include <stdio.h>
#include <string.h>

int main()
{
  char str[100]="";
  char *pch3;
  strcpy(pch3,"Ravi");
  //strcat(str,pch3); //Segmnetation fault.
  puts(pch3);
  return 0;
}

If I do the same thing in this one I still get segmentation fault. 如果我在这个中做同样的事情,我仍然会遇到分段错误。

 else
      {
         misc_rec_cnt++;
         fp1=fopen("breast-cancer-wisconsin-miscellaneous.data","a");
         fprintf(fp1,"%s",line2);
         fclose(fp1);
         fp2=fopen("missingSCNs.data","a");
         pch2=strtok(line2,",");
         fprintf(fp2,"%s\n",pch2);
         fclose(fp2);

         //pch3=(char *)malloc(sizeof(char)*strlen(line3));
         pch3 = strtok(line3,",");
         while(pch3!=NULL)
         {
             if(strcmp(pch3,"?") == 0)
             {
                strcat(str1,"0");
                strcat(str1,",");
             }
             else
             {
                //strcat(str1,pch3);
                strcat(str1,",");
             }
             pch3 = strtok(NULL,",");
         }
         strlen1=strlen(str1);
         memcpy(str2,str1,strlen1-1);
         fp3=fopen("breast-cancer-wisconsin-miscellaneous-cleansed.data","a");
         fprintf(fp3,"%s\n",str2);
         fclose(fp3);
      }

You need to allocate the space for pch3 before you copy to it. 在复制到pch3之前,需要为pch3分配空间。 Use malloc to create a char array large enough to accomodate the elements of your source string before you copy it. 使用malloc创建一个足够大的char数组,以便复制之前容纳源字符串的元素。 What you are currently doing is declaring a char pointer and not initialising it. 你目前正在做的是声明一个char指针而不是初始化它。 Therefore the memory location that it points to could be anywhere - and that means that you should probably not be attempting to write to it - which is why you are getting the segfault. 因此,它指向的内存位置可以是任何位置 - 这意味着您可能不应该尝试写入它 - 这就是您获得segfault的原因。 Using malloc will allow you to allocate a region of memory that you are safe to write to and this will solve your problem (assuming the call to malloc succeeds). 使用malloc将允许您分配一个可以安全写入的内存区域,这将解决您的问题(假设对malloc的调用成功)。 You cannot just go writing data to random memory locations without getting segfaults and access violations. 您不能只是将数据写入随机内存位置而不会遇到段错误和访问冲突。

pch3 is a char pointer, but it doesn't have any storage associated with it which is the cause of the problem. pch3是一个char指针,但它没有与之关联的任何存储,这是导致问题的原因。 Call malloc() to allocate some memory that the pch3 pointer can point to and you should be ok. 调用malloc()来分配一些pch3指针可以指向的内存,你应该没问题。

At this point you have a char pointer that is uninitialized and just pointing somewhere unknown. 此时你有一个未初始化的char指针,只是指向一个未知的地方。 So try this: 试试这个:

  pch3 = (char *)malloc(sizeof(char) * 100); /* 100 just as an example */

This tutorial might be helpful or this SO question: Allocating char array using malloc 教程可能会有所帮助,或者这个问题: 使用malloc分配char数组

pch3 is an unallocated pointer, so you're writing data to a location that doesn't exist. pch3是一个未分配的指针,因此您将数据写入不存在的位置。 Did you mean to assign it to str ? 你的意思是把它分配给str吗?

char str[100]="";
char *pch3;
pch3 = str;
strcpy(pch3,"Ravi");

I'd recommend that you first allocate memory before copying data to a random place. 我建议您在将数据复制到随机位置之前先分配内存。

strcpy(pch3=(char*)malloc(sizeof("Ravi")),"Ravi");

but better check if it didn't return null pointer. 但最好检查一下它是否没有返回空指针。

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

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