proj2.c:
#include <stdio.h>
#include "duplicate.h"
#include<stdlib.h>
int main(int argc, char **argv)
{
if (argc <= 1)
{
return 1;
}
char **copy = duplicate_args(argc, argv);
char **p = copy;
argv++;
p++;
//while (*argv)
for(int i=0;i<argc-1;i++)
{
printf("%s %s\n", argv[i], p[i]);
}
free_duplicated_args(argc,copy);
return 0;
}
duplicate.c:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
char **duplicate_args(int argc, char **argv)
{
char **arrPointer=(char**)malloc((argc)*sizeof(char*));
for(int i=0;i<argc;i++)
{
// Added one for string terminator '\0'
arrPointer[i] = (char*)malloc((strlen(argv[i])*(sizeof(char)))+1);
for(int j=0;j<strlen(argv[i]);j++)
{
if(argv[i][j] > 64 && argv[i][j] < 91)
{
arrPointer[i][j]=argv[i][j]+32;
}
else if(argv[i][j] > 96 && argv[i][j] < 123)
{
arrPointer[i][j]=argv[i][j]-32;
}
else
{
arrPointer[i][j]=argv[i][j];
}
}
arrPointer[i]+= '\0';
}
return arrPointer;
}
void free_duplicated_args(int argc,char **copy)
{
/* while(copy[i])
{
free(copy[i]);
i++;
}
free(copy);
*/
for(int i=0;i<argc;i++)
{
free(copy[i]);
}
free(copy);
}
duplicate.h:
char **duplicate_args(int argc, char **argv);
void free_duplicated_args(int argc,char **copy);
在上面的代码中,复制函数首先创建一个指针数组(取决于argc),然后在循环中,我将整个数组的每个元素指向另一个指针,该指针保存我的重复字符串。我没有任何内存泄漏,但有一个错误说我正在基于未初始化的值进行跳转?我正在使用--track-originins = yes但仍然无法弄清楚为什么会发生这种情况?
┌─[✗]─[root@danial]─[~/Documents/OS/proj2/project]
└──╼ # valgrind --tool=memcheck --track-origins=yes ./proj2 1
==15798== Memcheck, a memory error detector
==15798== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==15798== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==15798== Command: ./proj2 1
==15798==
==15798== Conditional jump or move depends on uninitialised value(s)
==15798== at 0x4838CD8: __strlen_sse2 (vg_replace_strmem.c:462)
==15798== by 0x48B9BEE: vfprintf (vfprintf.c:1638)
==15798== by 0x48C0815: printf (printf.c:33)
==15798== by 0x1091F5: main (in /root/Documents/OS/proj2/project/proj2)
==15798== Uninitialised value was created by a heap allocation
==15798== at 0x483577F: malloc (vg_replace_malloc.c:299)
==15798== by 0x10928E: duplicate_args (in /root/Documents/OS/proj2/project/proj2)
==15798== by 0x109194: main (in /root/Documents/OS/proj2/project/proj2)
==15798==
1 1
==15798==
==15798== HEAP SUMMARY:
==15798== in use at exit: 0 bytes in 0 blocks
==15798== total heap usage: 4 allocs, 4 frees, 1,050 bytes allocated
==15798==
==15798== All heap blocks were freed -- no leaks are possible
==15798==
==15798== For counts of detected and suppressed errors, rerun with: -v
==15798== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
正如@AnttiHaapala指出的那样,我只需要在for循环中附加'\ 0'。
for(int i=0;i<argc;i++)
{
// Added one for string terminator '\0'
arrPointer[i] = (char*)malloc((strlen(argv[i])*(sizeof(char)))+1);
for(int j=0;j<=strlen(argv[i]);j++)
{
if(j==strlen(argv[i]))
{
arrPointer[i][j]='\0';
continue;
}
if(argv[i][j] > 64 && argv[i][j] < 91)
{
arrPointer[i][j]=argv[i][j]+32;
}
else if(argv[i][j] > 96 && argv[i][j] < 123)
{
arrPointer[i][j]=argv[i][j]-32;
}
else
{
arrPointer[i][j]=argv[i][j];
}
}
}