如何修复和理解此细分故障(核心转储)错误?我无法理解
我的任务是创建一个执行页面替代算法的C代码。 代码仅200行,因此我将尝试发送一些部分,希望您仍然可以在没有整个代码的情况下为我提供帮助。 我创建一个全局int数组,然后在中对其进行修改,如果 Main 中的语句。我已经尝试了所有内容来调试此问题,但我仍然无法理解为什么它与未修改的数组完美运行,但是在修改它时,它 core dumps 。它负担我的负担,因为整个数组实际上已更改,但它给出了细分故障。如果代码实际上写入该内存,为什么它仍然给出分割故障?
这是整个代码中的很大一部分:
#include <stdio.h>
#include <stdlib.h>
// GLOBAL VARIABLES
int stringNum = 100;
int pageReferences[100] = {2, 8, 7, 2, 0, 3, 1, 7, 3, 1, 9, 3, 6, 1, 8, 4, 5, 1, 8, 8, 3, 4, 4, 3, 4, 2, 0, 6, 6, 7, 0, 1, 0, 9, 3, 6, 4, 6, 8, 0, 4, 6, 2, 3, 5, 7, 8, 0, 3, 2, 0, 0, 0, 4, 6, 9, 1, 4, 3, 8, 8, 0, 0, 9, 7, 0, 7, 0, 9, 7, 7, 3, 8, 8, 9, 2, 7, 2, 1, 2, 0, 9, 1, 1, 1, 5, 0, 7, 1, 4, 9, 1, 9, 5, 8, 4, 4, 7, 9, 6};
int frame[15];
// PROTOTYPES
void fifo(int frameSize);
void lru(int frameSize);
void opt(int frameSize);
// MY MAIN
void main(int argc, char*argv[]) {
int frameSize;
if (argc == 2) {
frameSize = atoi(argv[1]);
}
else if (argc == 3) {
frameSize = atoi(argv[1]);
unsigned int seed = atoi(argv[2]);
srand(seed);
for (int i = 0; i < 100; i++){
pageReferences[i] = (rand() % 10);
}
}
else
printf("Usability: Input 1 or 2 numbers. First number is Frame size and Second number is the Seed for random.\n");
fifo(frameSize);
lru(frameSize);
opt(frameSize);
}
我得到了其他人的帮助,没有人能确定为什么它以其行动方式行事。这个问题似乎是数组的修改,但是我尚未找到修复它的方法,即使我在IF语句中将其声明为本地变量,并将数组作为函数的参数。我希望有一个默认数组,并在必要时更改其内容。
我该如何解决?使用Malloc或将其制作指针是否有帮助?
I am tasked with creating a C code that does what page-replacement algorithms do.
The code is barely 200 lines, so I will try to send some portions and hopefully, you can still be able to help me without the whole code.
I create a global int array and I modify it inside the if statement in main. I have tried everything to debug this, and I still cannot comprehend why it runs perfectly with the unmodified array, but it Core Dumps when it is modified. It burdens me because the whole array is actually changed, but it gives a segmentation fault. If the code actually is writing to that memory, why does it still give a Segmentation Fault?
Here is a good portion of the whole code:
#include <stdio.h>
#include <stdlib.h>
// GLOBAL VARIABLES
int stringNum = 100;
int pageReferences[100] = {2, 8, 7, 2, 0, 3, 1, 7, 3, 1, 9, 3, 6, 1, 8, 4, 5, 1, 8, 8, 3, 4, 4, 3, 4, 2, 0, 6, 6, 7, 0, 1, 0, 9, 3, 6, 4, 6, 8, 0, 4, 6, 2, 3, 5, 7, 8, 0, 3, 2, 0, 0, 0, 4, 6, 9, 1, 4, 3, 8, 8, 0, 0, 9, 7, 0, 7, 0, 9, 7, 7, 3, 8, 8, 9, 2, 7, 2, 1, 2, 0, 9, 1, 1, 1, 5, 0, 7, 1, 4, 9, 1, 9, 5, 8, 4, 4, 7, 9, 6};
int frame[15];
// PROTOTYPES
void fifo(int frameSize);
void lru(int frameSize);
void opt(int frameSize);
// MY MAIN
void main(int argc, char*argv[]) {
int frameSize;
if (argc == 2) {
frameSize = atoi(argv[1]);
}
else if (argc == 3) {
frameSize = atoi(argv[1]);
unsigned int seed = atoi(argv[2]);
srand(seed);
for (int i = 0; i < 100; i++){
pageReferences[i] = (rand() % 10);
}
}
else
printf("Usability: Input 1 or 2 numbers. First number is Frame size and Second number is the Seed for random.\n");
fifo(frameSize);
lru(frameSize);
opt(frameSize);
}
I have had help from other people and none can identify why it acts the way it does. The issue seems to be the MODIFICATION of the array, but I have not yet found a way to fix it, even if I declare it inside the if statements as local variables and give the array as parameters to the functions. I wish to have a default array and change its contents if necessary.
How can I fix this? Does using malloc or making it a pointer help with anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑阵列的写作会导致您的问题。全球阵列具有100个INT的位置,这正是您的循环写入的INT量。因此,您似乎并没有超出范围。
您确定循环不仅完成了,而且您会得到Coredump,因为您之后呼叫的功能指向无效的地址,因为它们尚未定义?我的意思是,在这种情况下,您的编译器应该已经在尝试运行之前已经应该抱怨了,但是乍一看,这是唯一缺少的东西。
I doubt that the writing to the array causes your issue. The global array has places for 100 ints and thats exactly the amount of ints your loop writes into. So you don't seem to go out of bounds on that.
Are you sure that the loop doesn't just finish and you get the coredump because the functions you call afterwards point to invalid addresses because they were not defined yet? I mean in that case your compiler should already complain before you try to run this, but it's the only thing missing here at first glance.