gpt4 book ai didi

C:段错误(核心已转储)

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:39 27 4
gpt4 key购买 nike

<分区>

我正在为 C 中的类分配编写此程序。它模拟读取和写入自定义大小的直接映射缓存,涉及自定义大小的主内存。

这些是我在获取 Segmentation fault 之前使用的参数:

Enter main memory size (words):  65536
Enter cache size (words): 1024
Enter block size (words/block): 16

这是我的代码。它还没有完成。

#include<stdio.h>
#include<stdlib.h>

struct cache{
int tag;
int *block;
};

struct main {
int value;
int address;
int word;

struct main *next;
};

struct cache *cacheptr;
struct main *mainHd;

int main() {
int option = 0;

while (option != 4) {
printf("Main memory to Cache memory mapping:\n--------------------------------------");
printf("\n1) Set parameters\n2) Read cache\n3) Write to cache\n4) Exit\n\nEnter Selection: ");
scanf("%d",&option);
printf("\n");

if (option == 1) {
int mainMemory, cacheSize, block;

printf("Enter main memory size (words): ");
scanf("%d",&mainMemory);

printf("Enter cache size (words): ");
scanf("%d",&cacheSize);

printf("Enter block size (words/block): ");
scanf("%d",&block);

struct main *mainptr=(struct main *)malloc(cacheSize);
mainHd->next=mainptr;

for (int i=1; i < mainMemory; i++) {
mainptr->value=mainMemory-i;
mainptr->address=i;
mainptr->word=i;

struct main *mainnxt=(struct main *)malloc(cacheSize);
mainptr->next=mainnxt;
mainptr=mainnxt;
}
} /* end if */
} /* end while */
} /* end main */

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com