Segmentation error when using strcpy ()?

I have a global structure:

struct thread_data{
   char *incall[10];
   int syscall arg_no;
   int client_socket;
};

and in main ()

char buffer[256];
char *incall[10];
struct thread_data arg_to_thread;

strcpy(incall[0],buffer);   /*works fine*/
strcpy(arg_to_thread.incall[0],buffer); /*causes segmentation fault*/

Why is this happening and suggest a way out.

thank

0
source share
3 answers

Sefaft means that something is wrong. But segfault does not mean that something is wrong. If two situations are basically the same, but one is segfaults and the other is not, it usually means that they are both wrong, but only one of them causes segfault to fire.

char* incall[10], , 10 char. . strcpying into incall [0] . , segfault! incall [0] ( malloc).

, : segfault? , , , , , . , strcpy segfault, - , . .

( ) , strcpy - , 0 , , , ( strlen ). , strncpy , , .

+5

incall[0], , strcpy(). , .

arg_to_thread.incall[0], , strcpy(). , , .

; , .

+1
  • Make sure you have enough memory for your line buffers.
  • Stay away from strcpy. Use instead strncpy. strcpy- This is a well-known source of buffer overflow vulnerabilities - a nightmare of security and maintenance, for which this is actually not an excuse.
0
source

All Articles