繁体   English   中英

通过引用将字符串数组传递给C函数-无效的指针

[英]Passing an array of strings to a C function by reference - invalid pointer

我在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);

该函数的说明如下:指定参数数组(第一个参数)是否包含重定向或后台执行。

输入:1-参数数组2-通过引用的参数数量:3-用于放置文件名以进行输入重定向的字符串数组4-用于放置文件名的字符串数组输出重定向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的调用时出现以下错误:

[xxxx@xxxx 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-ARCH x86_64下使用gcc 4.7.1

有人知道为什么会这样吗?

非常感谢您提前提供的所有帮助

可能的原因是尝试释放inputRediroutputRedirerrorRedir任何元素( 如果它们不变)。 由于它们被初始化为字符串文字(空字符串),因此将其传递给free()是非法的:

如果ptr是空指针,则不执行任何操作。 否则,如果参数与calloc,malloc或realloc函数先前返回的指针不匹配,或者如果通过调用free或realloc释放了空间,则该行为是不确定的。

一个推测性的解决方法是将这三个数组的元素初始化为NULL指针(将NULL指针传递给free()是安全的):

char *inputRedir[4]  = { NULL, NULL, NULL, NULL };
char *outputRedir[4] = { NULL }; /* Unspecifed initializers will */
char *errorRedir[4]  = { NULL }; /* be NULL by default.          */

请注意, printf()语句中的格式说明符是不正确的,因为inputRedir[0] (和outputRedir[0] )是char*但提供的格式说明符是%c ,用于char类型:将%s用作char*

暂无
暂无

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

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