我有通過引用傳遞字符串數組給一個函數在C.傳遞引用字符串數組到C函數 - 無效指針
問題上下文:我試圖使用非常簡單的殼一些命令。當命令在命令的每個參數中鍵入時,都會保存在一個字符串數組的插槽中。用來解析命令函數被調用lineParsing(我沒有這樣做的來源 - 我只有的.o和.h)
在有問題的功能有以下標題:
void lineParsing (char **, int, char **, char **, char **, int *);
void lineParsing (char **item, int nargs, char **inputRedir, char **outputRedir, char **errorRedir, int *background);
該函數的描述如下: 指定參數數組(第一個參數)是否包含重定向或後臺執行。
輸入: 3-一個字符串在其中 4-一個字符串數組放置文件的名稱爲輸入重定向中的陣列: 1-的參數 2-數量的參數 通過參考陣列其上放置文件的名稱輸出重定向 5-字符串數組,在其中放置所述文件的名稱爲錯誤重定向 如果使用&在背景6-保存1,如果不爲0
我我在臨時主程序中調用這個函數如下:
int main (int argc, char *argv[]){
char **parrayArgumentos=NULL;
int i,numargs,background;
char *inputRedir[4]={"","","",""};
char *outputRedir[4]={"","","",""};
char *errorRedir[4]={"","","",""};
parrayArgumentos = lineInput (&numargs); //Asks for command via standard input.
printf ("You have typed in: %d arguments \n",numargs); //displays number of arguments
i=0;
while(i<=numargs-1){
printf ("%s \n",parrayArgumentos[i]);
i++;
}
lineParsing(parrayArgumentos,numargs,inputRedir,outputRedir,errorRedir,&background);
//This call is problematic
printf ("The command you have introduced has:\n%c for input redirection\n%c for output Redirection\n%s For error Ridirection\n%d background\n",inputRedir[0],outputRedir[0],errorRedir[0],background);
freeLineInput(parrayArgumentos);
return 0;
}
這將編譯通過我已經做了makefile文件沒有錯誤或警告:
mishell: mishell.o parser64.o
gcc mishell.o parser64.o -o mishell
mishell.o: mishell.c
clean:
rm -f mishell.o mishell
當我執行二進制我得到以下錯誤的點中調用lineParsing發生:
[[email protected] src]$ ./mishell
ls -la > listoffiles
You have typed in: 4 arguments
ls
-la
>
listoffiles
*** glibc detected *** ./mishell: free(): invalid pointer: 0x0000000000401078 ***
======= Backtrace: =========
/lib/libc.so.6(+0x784a6)[0x7f8a5f76b4a6]
./mishell[0x400d99]
./mishell[0x4007b6]
/lib/libc.so.6(__libc_start_main+0xf5)[0x7f8a5f714725]
./mishell[0x4005e9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:04 7865560 /home/xxxxx/Desktop/xxxx/xxxx/xxxxx/src/mishell
00601000-00602000 rw-p 00001000 08:04 7865560 /home/xxxxx/Desktop/xxxxx/xxxxx/xxxxx/src/mishell
00e25000-00e46000 rw-p 00000000 00:00 0 [heap]
7f8a5f4de000-7f8a5f4f3000 r-xp 00000000 08:03 1185891 /usr/lib/libgcc_s.so.1
7f8a5f4f3000-7f8a5f6f2000 ---p 00015000 08:03 1185891 /usr/lib/libgcc_s.so.1
7f8a5f6f2000-7f8a5f6f3000 rw-p 00014000 08:03 1185891 /usr/lib/libgcc_s.so.1
7f8a5f6f3000-7f8a5f88e000 r-xp 00000000 08:03 2490393 /lib/libc-2.16.so
7f8a5f88e000-7f8a5fa8d000 ---p 0019b000 08:03 2490393 /lib/libc-2.16.so
7f8a5fa8d000-7f8a5fa91000 r--p 0019a000 08:03 2490393 /lib/libc-2.16.so
7f8a5fa91000-7f8a5fa93000 rw-p 0019e000 08:03 2490393 /lib/libc-2.16.so
7f8a5fa93000-7f8a5fa97000 rw-p 00000000 00:00 0
7f8a5fa97000-7f8a5fab8000 r-xp 00000000 08:03 2490410 /lib/ld-2.16.so
7f8a5fc8a000-7f8a5fc8d000 rw-p 00000000 00:00 0
7f8a5fcb4000-7f8a5fcb8000 rw-p 00000000 00:00 0
7f8a5fcb8000-7f8a5fcb9000 r--p 00021000 08:03 2490410 /lib/ld-2.16.so
7f8a5fcb9000-7f8a5fcba000 rw-p 00022000 08:03 2490410 /lib/ld-2.16.so
7f8a5fcba000-7f8a5fcbb000 rw-p 00000000 00:00 0
7fff8533d000-7fff8535e000 rw-p 00000000 00:00 0 [stack]
7fff853ff000-7fff85400000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
我使用下的ArchLinux 3.4.4-2拱x86_64的GCC 4.7.1
有誰知道爲什麼發生這種情況?
非常感謝您的所有幫助提前
您沒有發佈'lineParsing','lineInput'和'freeLineInput'的源代碼。這可能與您的問題有關。 – netcoder 2012-08-13 15:37:34
恐怕我沒有來源。我所擁有的就是這些函數和頭文件的目標文件。 – user1595723 2012-08-13 15:42:48
如果沒有它,我恐怕不能提供很多幫助。您的圖書館存在問題,或者您使用方式存在問題。很難說。有沒有關於它的文檔? – netcoder 2012-08-13 15:46:41