3

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.

/* 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);
      }
Teja
  • 13,214
  • 36
  • 93
  • 155
  • No I am reading each and every line from a file and storing it in line3 buffer and using it in my code. But when I do strcat it always gives me seg fault. – Teja Sep 14 '12 at 21:54
  • Oh, wait, I just saw `pch3 = strtok(line3,",");`. You lose the `malloc`ed memory by that, and you shouldn't `malloc` `pch3` in that case. – Daniel Fischer Sep 14 '12 at 21:54
  • But I see nothing in the second snippet that could cause a segfault. `line3` isn't long enough to cause an overrun of `str1` with the `strcat`s. – Daniel Fischer Sep 14 '12 at 22:01
  • But the commented part is causing the problem. I really don't know why it's throwing error. This entire snippet code is inside a else condition which executes only 16 times.If I comment this snippet rest of my code works perfectly fine. – Teja Sep 14 '12 at 22:04
  • Which commented part? If you have `strcat(str1,pch3); strcat(str1,",");` in the `else` branch, then that would be enough to cause an overrun. Try with a larger `str1`, `char str1[200];` has enough space to accommodate the tokens of `line3` and the commas. – Daniel Fischer Sep 14 '12 at 22:08

4 Answers4

4

You need to allocate the space for pch3 before you copy to it. Use malloc to create a char array large enough to accomodate the elements of your source string before you copy it. What you are currently doing is declaring a char pointer and not initialising it. 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. 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). You cannot just go writing data to random memory locations without getting segfaults and access violations.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
3

pch3 is a char pointer, but it doesn't have any storage associated with it which is the cause of the problem. Call malloc() to allocate some memory that the pch3 pointer can point to and you should be ok.

At this point you have a char pointer that is uninitialized and just pointing somewhere unknown. 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

Community
  • 1
  • 1
Levon
  • 138,105
  • 33
  • 200
  • 191
1

pch3 is an unallocated pointer, so you're writing data to a location that doesn't exist. Did you mean to assign it to str?

char str[100]="";
char *pch3;
pch3 = str;
strcpy(pch3,"Ravi");
ellotheth
  • 4,333
  • 2
  • 17
  • 29
0

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.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173