Hi have defined this struct typdef:
typedef struct{
int pid;
int valor;
}hijo;
But after reserving memory to it, I iterate the array to assign value to each struct, but values are not stored correctly:
hijo *retorno;
retorno=malloc(processes*sizeof(hijo));
while (processes > 0) {
pid = wait(&status);
int valors = WEXITSTATUS(status);
retorno[i].valor=valors;
retorno[i].pid=pid;
--processes; // TODO(pts): Remove pid from the pids array.
}
Thanks.
You need to initialize and increment i
. Otherwise you keep assigning to the same array element.
hijo *retorno;
retorno=malloc(processes*sizeof(hijo));
int i = 0;
while (processes > 0) {
pid = wait(&status);
int valors = WEXITSTATUS(status);
retorno[i].valor=valors;
retorno[i].pid=pid;
--processes; // TODO(pts): Remove pid from the pids array.
i++;
}
Or instead of updating two variables, you could just compare i
to processes
:
for (int i = 0; i < processes; i++) {
pid = wait(&status);
int valors = WEXITSTATUS(status);
retorno[i].valor=valors;
retorno[i].pid=pid;
}