简体   繁体   English

为什么我的数组的先前元素发生变化(C中的字符串数组)

[英]Why are the previous elements of my array changing (array of strings in C)

I am building a shell history function to take in a string and add it to an array of strings as the program runs. 我正在构建一个外壳历史记录功能,以接收一个字符串,并在程序运行时将其添加到字符串数组中。

My problem is that whenever I update the array with a new line (string) the previous element in cache gets filled with my CWD (current working directory), but I need to to keep the previous string I set it to. 我的问题是,每当我用新行(字符串)更新数组时,缓存中的前一个元素就会被CWD(当前工作目录)填充,但是我需要保留设置它的前一个字符串。

This is my main loop that gets the string and attempts to store the history with the cache function: 这是我的主循环,获取字符串并尝试使用缓存功能存储历史记录:

//prints out the cwd; then loops to take in the line, split it up into arguments, and attempt to execute it
//while lsh_execute returns 0, then frees up the allocated space
void lsh_loop(void)
{
  char *line;               //pointer to a char (the beg. of an array of chars)
  char *cache[10] = {NULL};     //history array
  char **args;              //pointer to a pointer of a char...
  int status, counter = 0, i, j;

  do {
    printf("%s>", getcwd(0,0));     //print cwd
    line = lsh_read_line();     //call read line
    counter = lsh_cache_line(counter,line, cache);
    printf("This is counter:%i\n", counter);        

    for(i=0; i<10; i++){
      printf("This is cache[%i]:%s\n", i, cache[i]);
    }

    args = lsh_split_line(line);    //split line
    status = lsh_execute(args);     //execute the split args

    free(line);             //free memory
    free(args);
  } while (status);         //continue as long as execute returns 1

}

In this function, I am copying the input string line to the array of strings: 在此函数中,我将输入字符串行复制到字符串数组:

int lsh_cache_line(int counter,char *line, char *cache[10]){

  (cache[counter]) = line;
  printf("This is cache[%i]:%s\n", counter, cache[counter]);
  counter++;
  counter = counter % 10;
  return counter; 

}

This is the output of my program: 这是我程序的输出:

paul@paul-VirtualBox:~/Desktop$ gcc shell.c
paul@paul-VirtualBox:~/Desktop$ ./a.out
/home/paul/Desktop>HI
This is cache[0]:HI
This is counter:1
This is cache[0]:HI
This is cache[1]:(null)
This is cache[2]:(null)
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>this is my problem
This is cache[1]:this is my problem
This is counter:2
This is cache[0]:/home/paul/Desktop
This is cache[1]:this is my problem
This is cache[2]:(null)
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>it overwrites my previous string with the cwd
This is cache[2]:it overwrites my previous string with the cwd
This is counter:3
This is cache[0]:/home/paul/Desktop
This is cache[1]:/home/paul/Desktop
This is cache[2]:it overwrites my previous string with the cwd
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>^C
paul@paul-VirtualBox:~/Desktop$ 

I have experimented with different ways of declaring and initializing the array of strings but this way seems to work the best. 我尝试了各种声明和初始化字符串数组的方法,但是这种方法似乎效果最好。

What am I doing wrong? 我究竟做错了什么?

There is no storage for the strings in cache . cache没有存储字符串的空间。

Something like strdup would create storage, but you would need to free the memory later. 诸如strdup东西会创建存储,但是您稍后需要free内存。

int lsh_cache_line(int counter,char *line, char *cache[10]){

 (cache[counter]) = strdup(line);
 printf("This is cache[%i]:%s\n", counter, cache[counter]);
 counter++;
 counter = counter % 10;
 return counter; 

}

There are 10 slots for strings, but no memory for the string values. 字符串有10个插槽,但是字符串值没有存储空间。 You need to allocate some memory. 您需要分配一些内存。

I would bet that your lsh_read_line() function returns for you always the same buffer with different texts. 我敢打赌,您的lsh_read_line()函数将始终为您返回具有不同文本的相同缓冲区。 You store then the pointer to this buffer into different cells in your array. 然后,将指向此缓冲区的指针存储到数组中的不同单元格中。 You should copy the text to newly allocated string once you get it into your line variable and work later only with this new copy. 一旦将其输入到行变量中,就应该将文本复制到新分配的字符串中,并且以后只能使用此新副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM