gpt4 book ai didi

c - 符号查找错误: undefined symbol: fclose

转载 作者:行者123 更新时间:2023-11-30 17:20:52 25 4
gpt4 key购买 nike

我有以下程序,它基本上实现了 DNS 服务器。共享内存用于实现服务器缓存。我试图通过从文件中读取一些初始条目来将它们填充到缓存中。但是,当文件条目超出某个点时,我会遇到段错误。我无法自己调试该程序。

typedef struct rs{
char domainName[256];
char ip[36];
time_t timeStamp;
struct rs *cacheEnd;
} ResourceRecord;
ResourceRecord *cache, *tempCachePtr, *cacheEnd;
void create_Shared_Memory() //Creates shared memory
{
int stateMemory;
stateMemory=shmget(IPC_PRIVATE, (MAX_CACHE_SIZE)*sizeof(ResourceRecord *),IPC_CREAT|0660);
if (stateMemory == -1)
{
perror("Shared memory creation");
exit(EXIT_FAILURE);
}
cache=(ResourceRecord *)shmat(stateMemory,NULL,0);
tempCachePtr = cache;
if(shmctl(stateMemory,IPC_RMID,NULL)==-1)
printf("ERROR CREATING CARMODEMEMORY!!!");

if(cache==NULL)
{
perror("Shared memory attach ");
shmctl(stateMemory,IPC_RMID,NULL);
exit(EXIT_FAILURE);

}
}

void populateCache(){
tempCachePtr = cache+1;
struct hostent* domainAddress;
int i;
ResourceRecord record;
char * line = NULL;
size_t fileInputLength = 0;
ssize_t read;
puts("Populating cache");
FILE *fp = fopen("hosts.txt", "r");
for(i=1;i<=40; i++){
if((read = getline(&line, &fileInputLength, fp)) == -1){
break;
}
printf("%d From file %s\n",i, line);
strcpy(record.domainName, strtok(line, " "));
strcpy(record.ip, strtok(NULL, "\n"));
time(&record.timeStamp);
*tempCachePtr = record;
tempCachePtr++;
}
cacheEnd = tempCachePtr-1;
*(cache->cacheEnd) = cacheEnd;
fclose(fp);
}

我尝试使用 gdb 调试代码,这就是我所拥有的

symbol lookup error: /home/path/dnsserver: undefined symbol: fclose, version GLIBC_2.2.5 [Inferior 1 (process 7178) exited with code 0177]

当我尝试更改文件条目数并再次调试时:

SIGSEGV from /lib64/ld-linux-x86-64.so.2 shared memory

最佳答案

正如 Anto Jurković 所指出的,错误在于我使用 50*sizeof(ResourceRecord*) 创建共享内存,其中 sizeof(ResourceRecord*) 只有几个字节,因为它是一个指针,但 sizeof(ResourceRecord) 却很大更大(我的系统中为 8 和 312)。因此 50*sizeof(ResourceRecord*) 仅分配 400 字节内存,这显然不足以容纳 40 个 ResourceRecord 类型记录。

关于c - 符号查找错误: undefined symbol: fclose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407304/

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