繁体   English   中英

函数C中的char **和valgrind条件跳转错误

[英]char** in function C and valgrind conditional jump erro

首先,我的问题仅是C,而不是C ++。 我已经在SO和google上搜索了我的问题,但是我认为这是由于对valgrind和C中函数参数的某些指针缺乏了解,所以如果有人可以对我解释一下,我将不胜感激。

这是我的主要代码。 函数getOrder仅应在';'之后剪切字符串。 并将第二部分(此处为“ on”)发送回bu变量缓冲区。

char na[] = "2;on";
char* bu="on";

//This part Do conditionnal jump
getOrder(na, 4, &bu);

//This part Doesn't do conditionnal jump
//char copy[strlen(na)];
//strcpy(copy, na);

//bu=strtok(na,";");

bu[2]='\0';

printf("ending : %s\n",bu); //(<- this is the line 54 of the valgrind error)

这是getOrder函数:

//TODO conditionnal jump here ! Only whn returning from the function
char* getOrder(char order[], int size, char* res[]){
int i=0;
char copy[size];
strcpy(copy, order);

printf("\nstr: %s\n", copy);
*res=strtok(copy,";");
printf("tok : %s\n", *res);
*res=strtok(NULL, ";");
printf("tok : %s\n", *res); 
printf("%d and order is %s\n",strcmp(*res, "on"), *res);

return *res;
}

我不明白的原因是,当我使用getOrder函数时,Valgrind抱怨所有这些:

==7072== Conditional jump or move depends on uninitialised value(s)
==7072==    at 0x4E7EC15: vfprintf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E88748: printf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x400A0D: main (test.c:54)
==7072== 
==7072== Conditional jump or move depends on uninitialised value(s)
==7072==    at 0x4EAEBC9: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E7EBB5: vfprintf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E88748: printf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x400A0D: main (test.c:54)
==7072== 
==7072== Conditional jump or move depends on uninitialised value(s)
==7072==    at 0x4EAEBD7: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E7EBB5: vfprintf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E88748: printf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x400A0D: main (test.c:54)
==7072== 
==7072== Syscall param write(buf) points to uninitialised byte(s)
==7072==    at 0x4F25E90: __write_nocancel (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4EAE49C: _IO_file_write@@GLIBC_2.2.5 (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4EAF948: _IO_do_write@@GLIBC_2.2.5 (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4EAEB5C: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E7EB55: vfprintf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x4E88748: printf (in /usr/lib64/libc-2.20.so)
==7072==    by 0x400A0D: main (test.c:54)
==7072==  Address 0x4022009 is not stack'd, malloc'd or (recently) free'd
==7072== 

但是当我直接在主体中直接使用相同的东西时却不是。 基本上,这些错误来自我使用函数getOrder的方式(因为它在参数中,是我发送的指针),但我无法找到我的错误。

非常感谢您的帮助。

因为res最终是copy的指针,并且copy是使用getOrder函数的堆栈框架删除的,因为它是局部变量。

尝试这个

//TODO conditionnal jump here ! Only whn returning from the function
char* getOrder(char order[], int size)
{
    int i=0;
    char copy[size];
    char *res;

    strcpy(copy, order);

    printf("\nstr: %s\n", copy);

    res = strtok(copy, ";");
    if (res == NULL) /* always check the return value from strtok */
        return NULL;
    printf("tok : %s\n", res);

    res = strtok(NULL, ";");
    if (res == NULL) /* always check the return value from strtok */
        return NULL;
    printf("tok : %s\n", res);
    printf("%d and order is %s\n", strcmp(res, "on"), res);

    return order + (res - copy);
}

并且getOrder调用中的size参数应该为5而不是4 ,因为有4个字符+终止的空字节'\\0'

char *na = "2;on";
char *bu;

//This part Do conditionnal jump
bu = getOrder(na, 5);

//This part Doesn't do conditionnal jump
//char copy[strlen(na)];
//strcpy(copy, na);

//bu=strtok(na,";");

//bu[2] = '\0';

if (bu != NULL)
    printf("ending : %s\n", bu); //(<- this is the line 54 of the valgrind error)
return 0;

bu只是只读的。 您不能执行以下操作:

bu[2] = '\0';

这会崩溃。

如果您想让字符串的第二部分是另一个数组,则会遇到问题,那么可以这样做,而无需使用函数。

PS: strtok()将修改传递的实际字符串,因此最好保留它的副本并传递该副本。

#include <stdio.h>

int main(void) {
    char na[]="2;on";
    char bu[3];
    char *tok;
    tok = strtok(na,";");
    if(tok != NULL)
    printf("%s\n",tok);
    tok = strtok(NULL,";");
    if(tok != NULL)
    strcpy(bu,tok);
    printf("%s\n",bu);
    return 0;
}

暂无
暂无

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

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