C -结构数组,自由问题,在我的结构数组中分配值的问题



接下来是我的程序。我正在创建一个结构,使用 malloc 分配内存,因为我想要一个非常大的结构数组,然后将该结构数组传递给函数。功能并不重要,因为我无法退出主程序。我的困惑是我的编译器(gcc)说当我尝试释放它时each_event未声明,但没有其他地方。当我注释掉它编译的自由语句时,但随后 valgrind 说当我执行 each_event[i].timestamp = tim[i] 行时,我有一个大小为 4 的无效写入。上面的一行被注释掉了,因为 valgrind 告诉我错误在那里(我用 gcc -g -O0 编译),即使我知道它必须意味着下面的行。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define event_length 512
#define bits_n_byte 8
#define timestamp_bytes 8
typedef enum type {
    DATA, CLOCK, EXPECTED, SPILL, ALL
} type;
typedef struct event {
char clocbuffer[event_length*bits_n_byte];
char datbuffer[event_length*bits_n_byte];
char expect[event_length];
char spil[event_length];
char clocerror[event_length*bits_n_byte];
char daterror[event_length*bits_n_byte];
long unsigned int timestamp;
} event;
int i, j, k, l, length, nevents;
char **spil, **expect, **dat, **cloc, **clocerror, **daterror;
long unsigned int *tim;
char library[256];
char *runnum, *clocmode, *datmode;
void GetPiece(char*, type, struct event*);
void FindMode(type);
void ErrorPiece(type);
int main(int argc,char **argv) {
if (argc != 2) {fprintf(stderr, "Format: ./program #_patternsnTry again.n");}
for (i=0;i<256;i++) library[i]=i;
FILE *IN = NULL;
char *buffer = NULL;
runnum = (char *) malloc(2);
runnum = strncpy(runnum,argv[1],1);
runnum[1] = '';
IN=fopen(argv[1], "r");                 /*Open input file.*/
if (IN)
{
    fseek(IN, 0, SEEK_END);                 /*This finds  */
    length  = ftell(IN);                    /*the length  */
    fseek(IN, 0, SEEK_SET);                 /*of the file.*/
    buffer = malloc(length + 2);            /*for buffer.     */
    fread(buffer, 1, length, IN);
    tim = (long unsigned int *) malloc(length + 2*sizeof(long unsigned int));
    fread(tim, sizeof(long unsigned int), length/sizeof(long unsigned int), IN);
    fclose(IN);
    nevents = length/2056;
    struct event* each_event = (struct event *) malloc(nevents*sizeof(struct event));
    for (i=0; i<length/sizeof(unsigned long int); i+=2056/sizeof(unsigned long int))
    {
    tim[i] = __builtin_bswap32 (tim[i]);
    tim[i]-=0x80000000;
    //if (tim[i]<1200000000 || tim[i]>1300000000) fprintf(stderr, "Check timestamp. Either endianness, size of bytes, or size of long ints are different.");
    each_event[i].timestamp = tim[i];
    }
    clocmode = malloc(nevents);
    datmode = malloc(nevents);
    GetPiece(buffer, DATA, each_event);
    GetPiece(buffer, CLOCK, each_event);
    GetPiece(buffer, EXPECTED, each_event);
    GetPiece(buffer, SPILL, each_event);
    GetPiece(buffer, ALL, each_event);
    FindMode(DATA);
    FindMode(CLOCK);
    ErrorPiece(DATA);
    ErrorPiece(CLOCK);
}
else fprintf(stderr,"Error in file naming/opening.n");   /*error*/
free (buffer);
free (tim);
free (runnum);
free (clocmode);
free (datmode);
free (each_event);
return 0;}

由于 each_event 是在 if { ... } 块内声明的,因此当您尝试free()它时,它超出了范围。要么在if { ... }结束时释放它,要么在main()开始时将声明与其他声明一起放入。但是,如果您执行后者,您可能会收到有关它可能在没有初始化的情况下被使用的警告,因此第一个选项可能是最好的。

相关内容

  • 没有找到相关文章

最新更新