gpt4 book ai didi

C - 结构问题 - 写作

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:36 24 4
gpt4 key购买 nike

我正在用 C 编写程序,我想我在内存方面遇到了一些麻烦。

所以我的问题是:我有 2 个返回结构的函数。当我一次只运行一个函数时,我没有任何问题。但是当我一个接一个地运行时,我在写入第二个结构时总是会出错。

函数 struct item* ReadFileBIN(char *name) -- 读取一个二进制文件。struct tables* getMesasInfo(char* Filename) -- 读取文本文件。

我的代码是这样的:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int numberOfTables=0;
int numberOfItems=0;

//struct tables* mesas;
//struct item* Menu;

typedef struct item{
char nome[100];
int id;
float preco;
};
typedef struct tables{
int id;
int capacity;
bool inUse;
};
struct tables* getMesasInfo(char* Filename){
struct tables* mesas;
char *c;
int counter,numberOflines=0,temp=0;
char *filename=Filename;
FILE * G;
G = fopen(filename,"r");
if (G==NULL){
printf("Cannot open file.\n");
}
else{
while (!feof(G)){
fscanf(G, "%s", &c);
numberOflines++;
}
fclose(G);
}
/* Memory allocate for input array */
mesas = (struct tables *)malloc(numberOflines* sizeof(struct tables*));
counter=0;
G=fopen(filename,"r");
while (!feof(G)){
mesas[counter].id=counter;
fscanf(G, "%d", &mesas[counter].capacity);
mesas[counter].inUse= false;
counter++;
}
fclose(G);
numberOfTables = counter;
return mesas;
}

struct item* ReadFileBIN(char *name)
{
int total=0;
int counter;
FILE *ptr_myfile;
struct item my_record;
struct item* Menu;
ptr_myfile=fopen(name,"r");
if (!ptr_myfile)
{
printf("Unable to open file!");
}

while (!feof(ptr_myfile)){
fread(&my_record,sizeof(struct item),1,ptr_myfile);
total=total+1;
}
numberOfItems=total-1;
Menu = (struct item *)calloc(numberOfItems , sizeof(struct item));
fseek(ptr_myfile, sizeof(struct item), SEEK_END);
rewind(ptr_myfile);
for ( counter=1; counter < total ; counter++)
{
fread(&my_record,sizeof(struct item),1,ptr_myfile);
Menu[counter] = my_record;
printf("Nome: %s\n",Menu[counter].nome);
printf("ID: %d\n",Menu[counter].id);
printf("Preco: %f\n",Menu[counter].preco);
}
fclose(ptr_myfile);
return Menu;
}

int _tmain(int argc, _TCHAR* argv[])
{
struct item* tt = ReadFileBIN("menu.dat");
struct tables* t = getMesasInfo("Capacity.txt");
getchar();
}**

我得到的错误是:

“test.exe 中 0x00411700 处的未处理异常:0xC0000005:访问冲突写入位置 0x00000000。”

在“菜单[计数器] = my_record;”中

提前致谢。

最佳答案

您似乎在 getMesasInfo() 中分配了错误大小的内存块:sizeof(struct tables*) 为您提供了指针,而不是它指向的结构:

mesas = (struct tables *)malloc(numberOflines* sizeof(struct tables*));

因此您可以轻松地覆盖未分配的内存。适当的分配应该是

mesas = (struct tables *)malloc(numberOflines* sizeof(struct tables));

或者,类似于您在 ReadFileBIN() 中分配另一个数组的方式:

mesas = (struct tables *)calloc(numberOflines, sizeof(struct tables));

此外,我不知道是不是有意的,但是在ReadFileBIN()中你正在分配(1)和读取(2)比记录总数少一条记录:

    numberOfItems=total-1;                                             // 1
Menu = (struct item *)calloc(numberOfItems , sizeof(struct item)); // 1
fseek(ptr_myfile, sizeof(struct item), SEEK_END);
rewind(ptr_myfile);
for ( counter=1; counter < total ; counter++) // 2
...

由于循环计数器从 1 开始(而不是 C 中正常的 0),您有效地执行了循环 total-1 次。也就是说,您读入数组的元素 1 到 total-1。但是,数组的实际元素是从 0 到 total-2 的索引,因此数组中的第一个元素未初始化,最后您会犯缓冲区溢出错误。

关于C - 结构问题 - 写作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2835424/

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