为什么我的数组前面的元素在变化(C中的字符串数组)
我正在构建一个shell历史函数,以接收字符串,并在程序运行时将其添加到字符串数组中 我的问题是,每当我用新行(字符串)更新数组时,缓存中的前一个元素就会被我的CWD(当前工作目录)填充,但我需要保留我设置的前一个字符串 这是我的主循环,它获取字符串并尝试使用缓存函数存储历史记录:为什么我的数组前面的元素在变化(C中的字符串数组),c,arrays,string,C,Arrays,String,我正在构建一个shell历史函数,以接收字符串,并在程序运行时将其添加到字符串数组中 我的问题是,每当我用新行(字符串)更新数组时,缓存中的前一个元素就会被我的CWD(当前工作目录)填充,但我需要保留我设置的前一个字符串 这是我的主循环,它获取字符串并尝试使用缓存函数存储历史记录: //prints out the cwd; then loops to take in the line, split it up into arguments, and attempt to execute it
//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
}
这是我的程序的输出:
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$
我已经尝试过声明和初始化字符串数组的不同方法,但这种方法似乎效果最好
我做错了什么?缓存中的字符串没有存储空间 类似于
strdup
的东西将创建存储,但稍后需要释放内存
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;
}
字符串有10个插槽,但字符串值没有内存。您需要分配一些内存。我敢打赌,您的lsh\u read\u line()函数始终会为您返回相同的缓冲区和不同的文本。然后将指向该缓冲区的指针存储到数组中的不同单元格中。一旦将文本放入行变量中,就应该将其复制到新分配的字符串中,以后只使用此新副本。可能是一些未定义的行为。使用所有警告和调试信息编译(gcc-Wall-Wextra-g
)。然后使用调试器(gdb
)和它的监视点;strcpy(tmp,生产线);高速缓存[计数器]=tmp;到缓存函数,得到了相同的结果,那么char*tmp=malloc(strlen(line)*sizeof(char))呢;strcpy(tmp,生产线);高速缓存[计数器]=tmp;我不确定你的意思,我用char*cache[10]={NULL}初始化了主循环中的缓存
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;
}