What happened to strcpy ()? (Segmentation error)

What is wrong with strcpy() in this code?

 void process_filedata(char *filename) { void* content; const char * buffer; char * temp; char * row; char * col; int lsize,buflen,tmp,num_scan; //num_scan - number of characters scanned int m=0,p=0,d=0,j=0; //m - machine, p - phase, d- delimiter, j - job FILE *file_pointer = fopen("machinetimesnew.csv","r"); if(file_pointer == NULL) { error_flag = print_error("Error opening file"); if(error_flag) exit(1); } fseek(file_pointer, 0 ,SEEK_END); lsize = ftell(file_pointer); buflen = lsize; rewind(file_pointer); // content = (char*) malloc(sizeof(char)*lsize); fread(content,1,lsize,file_pointer); buffer = (const char*) content; strcpy(temp,buffer); row = strtok(temp,"\n"); ............... ............... 

I get a segmentation error.

+2
source share
7 answers

There are actually three segmentations here:

 fread(content,1,lsize,file_pointer); strcpy(temp,buffer); row = strtok(temp,"\n"); 

The first is fread() , which is trying to write to memory that does not yet exist in relation to your process.

The second is strcpy() , (expounding the first), which you are trying to copy into a pointer that points to nothing. For temp , no memory is statically or dynamically allocated (other than a pointer reference).

Fix this by changing temp to look like this (assigning it statically):

 char temp[1024]; 

Or use malloc() to dynamically allocate memory for it (as well as for other pointers, so they actually point to something), similarly for content . If you know the required buffer size at compile time, use static allocation. If not, use malloc() . β€œKnowing” is the subject of another question.

The third is strtok() , which will modify temp en situ (in place), which, obviously, cannot do, since temp never stood out. In any case, do not expect temp be the same once strtok() is executed with it. By the name of the variable, I assume you know that.

In addition, initializing a pointer is not the same as allocating memory for it:

 char *temp = NULL; // temp is initialized char *temp = (char *) malloc(size); // temp is allocated if malloc returns agreeably, cast return to not break c++ 

Finally, please get used to using strncpy() over strcpy() , its much safer.

+9
source

You do not allocate space for temp. This is a wild signpost .

+13
source

Nothing wrong with strcpy. You did not initialize temp .

+8
source

There is another mistake. fread does not add a null character to the end of the buffer. This is because it only applies to byte arrays, not null-terminated strings. So you need to do something like this:

 content = malloc(lsize + 1); fread(content,1,lsize,file_pointer); content[lsize] = 0; temp = malloc(lsize + 1); strcpy(temp, content); 

or that:

 content = malloc(lsize); fread(content,1,lsize,file_pointer); temp = malloc(lsize + 1); memcpy(temp, content, lsize); temp[lsize] = 0; 

(Also in real code, you should check the results of fread and malloc .)

+3
source

you have not allocated memory for temp

+2
source

char * temp not initialized, and therefore you did not allocate any memory for it.

to try:

temp = (char *)malloc(SIZE);

where SIZE , however, is a lot of the memory you want to allocate for temp

+1
source

This piece of code intrigues me:

 if(file_pointer == NULL) { error_flag = print_error("Error opening file"); if(error_flag) exit(1); } 

Should you unconditionally exit if file_pointer is NULL?

+1
source

All Articles