首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux ()导致“分段错误”

Linux ()导致“分段错误”
EN

Stack Overflow用户
提问于 2021-06-17 14:58:11
回答 1查看 64关注 0票数 0

我正在尝试打印系统的所有组(Ubuntu20.04):

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
   printf("Here are all of this system's groups:\n\n");

   struct group* grp;
   while ((grp = getgrent()) != NULL)
      puts(grp->gr_name);

   endgrent();
   
   exit(EXIT_SUCCESS);
}

我使用sudo运行程序,我得到:

代码语言:javascript
复制
$ sudo ./program
Here are all of this system's groups:

Segmentation fault

在使用struct spwd时也会发生同样的错误。

更新

我用include行更新了源代码,省略了lib/*.c部分。当我编译这段代码时:

代码语言:javascript
复制
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: [...]
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

$ gcc -std=c17 main.c -o program
main.c: In function ‘main’:
main.c:11:18: warning: implicit declaration of function ‘getgrent’ [-Wimplicit-function-declaration]
   11 |    while ((grp = getgrent()) != NULL)
      |                  ^~~~~~~~
main.c:11:16: warning: assignment to ‘struct group *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   11 |    while ((grp = getgrent()) != NULL)
      |                ^
main.c:14:4: warning: implicit declaration of function ‘endgrent’ [-Wimplicit-function-declaration]
   14 |    endgrent();
      |    ^~~~~~~~

当我运行它时:

代码语言:javascript
复制
$ ./program 
Here are all of this system's groups:

Segmentation fault (core dumped)

$ sudo ./program 
Here are all of this system's groups:

Segmentation fault

现在,当我运行VS代码调试器时,它工作正常。VS代码是根据这篇文章配置的。

这是我的launch.json

代码语言:javascript
复制
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc-9 - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc-9 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-17 15:05:23

添加正确的标题:

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
   printf("Here are all of this system's groups:\n\n");

   struct group* grp;
   while ((grp = getgrent()) != NULL)
      puts(grp->gr_name);

   endgrent();

   exit(EXIT_SUCCESS);
}

更新:-std=c17很奇怪,它没有包含<grp.h>。您可能需要std=gnu17,其中包括一些gnu+posix内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68021820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档