Strange sprintf bug in C

I call a function that will branch and execute the code of another process for me. There are several processes designated B, C, and D. I have almost identical code for creating different processes. For some bizarre reason that I cannot reveal, one of the functions of creating a process causes a segmentation error. They look like me. Any chance anyone can weigh can give me some idea of ​​what the problem is?

void spawn_process_b(int fileID[]){
    int pid;
    char * argv[2];

    sprintf(argv[0], "%d", fileID[0]);
    sprintf(argv[1], "%d", fileID[1]);

    pid = fork();

    if (pid == 0)
    {
       execv("PipeW1", argv);
    }
}

void spawn_process_c(int fileID[]){
    int pid;
    char * argv[2];

    sprintf(argv[0], "%d", fileID[0]);
    sprintf(argv[1], "%d", fileID[1]);

    pid = fork();

    if (pid == 0)
    {
       execv("PipeW2", argv);
    }
}

cout < "bp1" < ; , , spawn_process_b . spawn_process_c , segfault sprintf. - ? spawn_process_b , ... ... , . , ?

+3
3

argv[0] undefined , sprintf .

, , , , .

auto argv char *. , , ( , ), .

-

char *argv[3];
for (int i = 0; i < 2; i++)
    argv[i] = malloc(space enough for my integer);
argv[2] = 0;

execv

+9

, . , , - undefined , , .

char *argv[2] 2 , . , , :

char argv[2][100]; // 2 strings of 100 characters each

:

char *argv[2];
int i;
for (i = 0; i < 2; i++)
  argv[i] = (char *)malloc(100 * sizeof(char)); // 100 characters
+4

Pointers argv[0]and argv[1]char must point to something and be initialized.

+1
source

Source: https://habr.com/ru/post/1760183/


All Articles