条件跳转或移动取决于未初始化的值内存泄漏结构统计

时间:2016-04-30 09:47:52

标签: c memory-leaks valgrind

我的作业由一个C程序组成,对于命令行中的每个参数创建一个子进程并在管道中写入以下内容:如果是文件,则大小(如果是目录),文本数文件,否则是5到15之间的随机数。它工作正常,但是当使用valgrind运行时,我会为每个进程获得一些内存泄漏(32字节)。

以下是代码:

typedef struct{    //defining a structure that holds the results organized.
    int value;
    char argument[100];
}result;
int main(int argc, char* argv[]){

int a2b[2],b2a[2],pid[argc-1];

pipe(a2b);pipe(b2a);


for (int i=1;i<argc;i++)
{
    pid[i]=fork(); //create argc - 1 processes 
    if(pid[i]==0){
       close(b2a[0]); //close the unnecessary ends of pipes
       close(a2b[0]);  
       close(a2b[1]);

       struct stat buf; 
       char *filename = malloc(sizeof(char)*100); 
       strcpy(filename, argv[i]);
       stat(filename,&buf);    

       if((buf.st_mode & S_IFREG) == S_IFREG) { //checks if argument is a file
            //printf("%s is a file \n",argv[i]);
            int size= buf.st_size; //obtains the size
            printf("Writing size of file %s to pipe: %d\n",argv[i],size);
            result *r1 = malloc(sizeof(result));
            memset(r1,0,sizeof(result));
            strcpy(r1->argument,argv[i]);
            r1->value=size;
            write(b2a[1],r1,sizeof(result));
            free(r1);            //write the size to the pipe
         }

        else if((buf.st_mode & S_IFDIR) == S_IFDIR) { /// check if argument is a directory
            //printf("Dir: %s\n",argv[i]);
            char cmd[100];
            strcpy(cmd,"./files.sh "); //construct the shell command which is used
            strcat(cmd,argv[i]);
            FILE *f= popen(cmd,"r"); //run it and store the result
            char r[100];
            fgets(r,sizeof(int),f); // read the result and store it into a string 
            //printf("%s in child\n",r);
            int pp=atoi(r);  // convert the string to an int
            printf("Writing number of text files in directory %s to pipe: %d\n",argv[i],pp);
            result *r2 = malloc(sizeof(result));
            memset(r2,0,sizeof(result));
            strcpy(r2->argument,argv[i]);
            r2->value=pp;
            write(b2a[1],r2,sizeof(result)); 
            // write(b2a[1],&pp,sizeof(int)); //write the number of text files to the pipe
            free(r2);
            fclose(f);
        }
        else {                                          //if the arg is neither a file or a directory
            srand (pid[i] ); //assign time as the seed of rand 
            int random_number = 5 + rand()%11; 
            printf("%s is neither, writing a random number in [5,15] to pipe: %d\n",argv[i],random_number);
            result *r3 = malloc(sizeof(result));
            memset(r3,0,sizeof(result));
            strcpy(r3->argument,argv[i]);
            r3->value=random_number;
            write(b2a[1],r3,sizeof(result)); 
            free(r3);
            //write(b2a[1],&random_number,sizeof(int)); // write the result to the pipe
        }
        free(filename);
        close(b2a[1]); 
        exit(0);
    }
}


for (int i=1;i<argc;i++)
{
    wait(0); //wait for all the processes
}

for(int i = 1; i<argc ; i++){
    result k;
    read(b2a[0],&k,sizeof(result));
    printf("%s:  %d\n",k.argument,k.value); //read from the pipe the info about all arguments
}

close(a2b[0]); //close all the pipes  
close(a2b[1]);
close(b2a[1]);
close(b2a[0]);
return 0;

}

popen使用的shell文件只回显目录中的文本文件数。

Valgrind告诉我这个:

==4647== Conditional jump or move depends on uninitialised value(s)
==4647==    at 0x100000893: main (homework.c:38)
==4647==  Uninitialised value was created by a stack allocation
==4647==    at 0x100000734: main (homework.c:18)

它也向我展示了第38和38行。 50,这是:

if((buf.st_mode & S_IFREG) == S_IFREG)
if((buf.st_mode & S_IFDIR) == S_IFDIR)

还有:

==4647== HEAP SUMMARY:
==4647==     in use at exit: 35,727 bytes in 201 blocks
==4647==   total heap usage: 282 allocs, 81 frees, 42,059 bytes allocated

我无法弄清楚为什么我会这样做。

0 个答案:

没有答案