C-我的char矢量在每个位置都节省相同的值



我试图在linux中制作外壳。我需要保存用户键入的每个命令,当用户想查看历史记录时,请向他显示最后10个命令。我正在使用char* Historial [10];但是,当用户键入第二个命令时,它将保存在Historial [0]和Historial [1]同一件事中。因此,如果我键入10个不同的命令,它将保存最后一个命令10次。我不知道我在做什么错。PS:我在DO中的病情无法正常工作。

int ejecutarCom(char* comando)
{ //executes the command
    pid_t pid;
    pid = fork();
    int aux = 0;
    if (pid < 0)
    {
        perror("Error");
        return -1;
    }
    else if (pid == 0)
    {
        aux = execlp(comando, comando, NULL);
    }
    else
    {
        wait(NULL);
    }
    return aux;
}
char* historial[10]; //history vector
int cantidad = 0, tamano = 10; //quantity & size
void guardarEnHistorial(char* c)
{ //saves every command
    if (cantidad < tamano)
    {
        historial[cantidad] = c;
    }
    if (cantidad == tamano)
    {
        cantidad = 0;
        historial[cantidad] = c;
    }
    cantidad++;
}
void verHistorial()
{ //shows history
    for (int i = 9; i >= 0; i--)
    {
        if (historial[0] == NULL)
            break;
        else if (historial[i] == NULL)
        {
            printf(" ");
        }
        else
            printf("%i: %sn", i, historial[i]);
    }
}
int main()
{
    char* comando = (char*) malloc(1024);
    int x = 1;
    do
    {
        printf("%s ", prompt);
        fflush(stdout);
        fgets(comando, sizeof(comando), stdin);
        comando[strcspn(comando, "n")] = '';
        printf("%sn", comando);
        if (strcmp(comando, "hist") == 0)
        {
            verHistorial();
        }
        else
        {
            x = ejecutarCom(comando);
            guardarEnHistorial(comando);
            printf("%in", x);
        }
    } while (x != -1);

如下所示。您的历史记录数组的每个位置都指向相同的内存位置,因此您在每个位置获得相同的信息。您必须为每个命令分配以下命令的内存: -

int main(){
 char* comando=(char*) malloc(1024);
 int x=1;
do{
   printf("%s ",prompt);
   fflush (stdout);
   fgets(comando,sizeof(comando),stdin); 
   comando[strcspn(comando, "n")] = '';
   printf("%sn",comando);
   if(strcmp(comando,"hist")==0){
     verHistorial();}
   else{
     x=ejecutarCom(comando);
     guardarEnHistorial(comando);
     comando=(char*) malloc(1024);// You need to allocate memory to store 
//each history records. Also consider whether you really need 1024 bytes of memory make it small
    printf("%in",x);
 }}while(x!=-1);

您将A Pointer 存储到命令缓冲区,而不是命令本身。由于命令缓冲区的地址没有更改,但其内容确实可以,因此每个历史记录条目都将指向相同的命令缓冲区,因此将显示相同的命令:

char* historial[10];          // history vector has only pointers
//...
    historial[cantidad] = c;  // stores a pointer

以下分配并释放了内存:

void guardarEnHistorial(char* c)
{ //saves every command
    if (cantidad == tamano) cantidad = 0;
    if (historial[cantidad]) free(historial[cantidad]);
    historial[cantidad]= malloc(strlen(c)+1);
    strcpy(historial[cantidad], c);
    cantidad++;
}

我不知道我在做什么错。

您正在观察此行为,因为historial数组的char指针指向同一内存位置comando。检查您的程序的说明:

guardarEnHistorial(comando);

以及在guardarEnHistorial ()函数中,您正在做:

historial[cantidad] = c;

您对comando进行的任何更改都会反映给您保存保存的historial数组的所有成员

您可以使用strdup()解决此问题。更换此:

guardarEnHistorial(comando);

与此:

guardarEnHistorial(strdup(comando));
                    ^^^^

strdup((函数将指针返回到一个新字符串的指针,这是传递给它的字符串的重复。strdup()使用malloc分配新字符串的内存并返回其指针。完成后,请确保使用free()释放它。就像插入historial数组时一样,如果条件cantidad == tamano相遇,则旋转插入。在这里,您需要先释放元素,然后将新元素插入数组中,否则程序中的内存泄漏。

最新更新