我得到一个核心转储
我正在尝试阅读一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少这些问题
节省了时间。启用所有警告
示例:
非生机化指针
使用
struct
而不是非初始化的指针。https://stackoverflow.com/questions/71905395/im-getting-a-core-dump/71905810#comment127062115_71905395 >
避免使用涉及签名的休闲类型变化。强大的代码寻找问题值。
在需要时声明对象,远离其首次使用。
At least these issues
Save time. Enable all warnings
Example:
Uninitialized pointer
Use a
struct
rather than an uninitialized pointer. @OkaTypes 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.