我得到一个核心转储

发布于 2025-01-22 03:34:54 字数 1083 浏览 2 评论 0原文

我正在尝试阅读一个int编号,因为我要建造的树的高度是。输入为

./htree testcase高度,

我试图将其保存在构造中的

struct arguments
{
    uint64_t length;
uint8_t* file;
    uint32_t threadNum;
int heightTree;
};

零件,使我核心转储在MAIN内部我试图分配高度的地方。

int main(int argc, char** argv)
{
    struct arguments *args;
    int32_t fd;
    uint32_t nblocks;
    struct stat hold;
    // input checking
    if (argc != 3)
        Usage(argv[0]);

    // open input file
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        perror("open failed");
        exit(EXIT_FAILURE);
    }


    // use fstat to get file size
    fstat(fd, &hold);
    off_t file_size= hold.st_size;



    // calculate nblocks
    nblocks= file_size/ BSIZE;

    (*args).heightTree= atoi(argv[2]);

    (*args).threadNum=0;
    uint32_t nthreads= (1<<((*args).heightTree+1));
    (*args).length= nblocks/ nthreads;



(*args).file= mmap(NULL, hold.st_size, PROT_READ, MAP_PRIVATE,fd,0);
    printf(" no. of blocks = %u \n", nblocks);


    close(fd);
    return EXIT_SUCCESS;
}

I'm trying to read in a int number as the height for a tree I'm building the input is

./htree testcase height

I'm trying to save it in the struct

struct arguments
{
    uint64_t length;
uint8_t* file;
    uint32_t threadNum;
int heightTree;
};

the part that is giving me the core dump is inside main where I'm trying to assign the height.

int main(int argc, char** argv)
{
    struct arguments *args;
    int32_t fd;
    uint32_t nblocks;
    struct stat hold;
    // input checking
    if (argc != 3)
        Usage(argv[0]);

    // open input file
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        perror("open failed");
        exit(EXIT_FAILURE);
    }


    // use fstat to get file size
    fstat(fd, &hold);
    off_t file_size= hold.st_size;



    // calculate nblocks
    nblocks= file_size/ BSIZE;

    (*args).heightTree= atoi(argv[2]);

    (*args).threadNum=0;
    uint32_t nthreads= (1<<((*args).heightTree+1));
    (*args).length= nblocks/ nthreads;



(*args).file= mmap(NULL, hold.st_size, PROT_READ, MAP_PRIVATE,fd,0);
    printf(" no. of blocks = %u \n", nblocks);


    close(fd);
    return EXIT_SUCCESS;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫尐诺 2025-01-29 03:34:54

至少这些问题

节省了时间。启用所有警告

示例:

(*args).heightTree= atoi(argv[2]);
warning: 'args' may be used uninitialized [-Wmaybe-uninitialized]

非生机化指针

使用struct而不是非初始化的指针。

// struct arguments *args;
// ...
// (*args).heightTree= atoi(argv[2]);

struct arguments args;
...
args.heightTree = atoi(argv[2]);

https://stackoverflow.com/questions/71905395/im-getting-a-core-dump/71905810#comment127062115_71905395 >

避免使用涉及签名的休闲类型变化。强大的代码寻找问题值。
在需要时声明对象,远离其首次使用。

// uint32_t nblocks;
//...
// off_t file_size= hold.st_size;
// nblocks= file_size/ BSIZE;

off_t file_size = hold.st_size;
if (file_size < 0) {
   Handle_unusual_file_size();
}
off_t nblocks = file_size / BSIZE;

At least these issues

Save time. Enable all warnings

Example:

(*args).heightTree= atoi(argv[2]);
warning: 'args' may be used uninitialized [-Wmaybe-uninitialized]

Uninitialized pointer

Use a struct rather than an uninitialized pointer. @Oka

// struct arguments *args;
// ...
// (*args).heightTree= atoi(argv[2]);

struct arguments args;
...
args.heightTree = atoi(argv[2]);

Types changes involving sign-ness

Avoid casual types changes involving sign-ness. Robust code looks for problem values.
Declare object when needed, not far away from their first use.

// uint32_t nblocks;
//...
// off_t file_size= hold.st_size;
// nblocks= file_size/ BSIZE;

off_t file_size = hold.st_size;
if (file_size < 0) {
   Handle_unusual_file_size();
}
off_t nblocks = file_size / BSIZE;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文