如何处理错误:-Wmaybe -Unitialization
我找到了此代码,并想运行它以从中学习,但是在我的PC(Linux系统)上,我会遇到此错误:
$ c99 -Wall -Wextra -pedantic -O2 ls.c
ls.c: In Funktion »list_dir«:
ls.c:44:9: Warnung: »pwd« could be used uninitialized in this function [-Wmaybe-uninitialized]
44 | printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
|
如何通过编辑此代码工作来摆脱它。在我的朋友PC上,它有效,但对我的电脑不起作用。是因为系统还是应该更改代码上的内容。提前致谢
#includes ...
int list_dir(const char *dir) {
struct dirent* cur_directory;
struct stat my_stat;
struct tm lt;
struct passwd *pwd; // User-ID
DIR* directory = opendir(dir);
if(directory == NULL) {
printf("list_dir : %s : %s \n", dir, strerror(errno));
return 0;
}
printf("Directory : %s\n", dir);
printf("\n");
while( (cur_directory = readdir(directory) ) ) {
stat(cur_directory->d_name, &my_stat);
if ( (stat(cur_directory->d_name, &my_stat) ) == 0 ) {
pwd = getpwuid(my_stat.st_uid); // Get User-ID
}
// Last Modified
time_t t = my_stat.st_mtime;
localtime_r(&t, <);
char timebuf[80];
strftime(timebuf, sizeof(timebuf), "%c", <);
if (pwd != 0) {
printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
printf("\n");
}
else {
printf("%d \t %ld \t %s \t %s", my_stat.st_uid, (long)my_stat.st_size, timebuf, cur_directory->d_name);
printf("\n");
}
}
closedir(directory);
return 0;
}
int main(int argc, char* argv[]){
if ( argc == 1 ) {
return list_dir ( "." );
}
else {
int ret = 0;
for (int i = 1; i < argc; i += 1 ) {
if ( list_dir ( argv[i] ) != 0 ) {
ret = 1;
}
}
return ret;
}
}
i found this code and wanted to run it to learn from it, but on my pc (linux system) i get this error:
$ c99 -Wall -Wextra -pedantic -O2 ls.c
ls.c: In Funktion »list_dir«:
ls.c:44:9: Warnung: »pwd« could be used uninitialized in this function [-Wmaybe-uninitialized]
44 | printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
|
how can i get rid of it by editing this code to work. On my friends PC it worked but not on mine. Is it because of the System or should i change something on the code. Thanks in advance
#includes ...
int list_dir(const char *dir) {
struct dirent* cur_directory;
struct stat my_stat;
struct tm lt;
struct passwd *pwd; // User-ID
DIR* directory = opendir(dir);
if(directory == NULL) {
printf("list_dir : %s : %s \n", dir, strerror(errno));
return 0;
}
printf("Directory : %s\n", dir);
printf("\n");
while( (cur_directory = readdir(directory) ) ) {
stat(cur_directory->d_name, &my_stat);
if ( (stat(cur_directory->d_name, &my_stat) ) == 0 ) {
pwd = getpwuid(my_stat.st_uid); // Get User-ID
}
// Last Modified
time_t t = my_stat.st_mtime;
localtime_r(&t, <);
char timebuf[80];
strftime(timebuf, sizeof(timebuf), "%c", <);
if (pwd != 0) {
printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
printf("\n");
}
else {
printf("%d \t %ld \t %s \t %s", my_stat.st_uid, (long)my_stat.st_size, timebuf, cur_directory->d_name);
printf("\n");
}
}
closedir(directory);
return 0;
}
int main(int argc, char* argv[]){
if ( argc == 1 ) {
return list_dir ( "." );
}
else {
int ret = 0;
for (int i = 1; i < argc; i += 1 ) {
if ( list_dir ( argv[i] ) != 0 ) {
ret = 1;
}
}
return ret;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pwd
在的情况下在内部初始化。如果条件是错误的,则永远不会初始化,因此将使用非专业化。
在定义中对其进行初始化(好主意,避免具有过时的状态),或在这种情况下添加
else
分支来初始化它。要在定义上初始化:
要在
else
中分配:而且,由于该 看起来像 您需要后一个方法。
pwd
is initialized insideif
. If the condition is false, it is never initialized, so will be used uninitialized.Initialize it in definition (good idea, avoids having the uninitialized state ever), or add
else
branch to initialize it in that case.To initialize at definition:
To assign in
else
:And, since that if is inside a loop, looks like you need the latter approach.