2012-06-20 56 views
0

以下是我的程序。我創建一個結構,使用malloc分配內存,因爲我想要一個非常大的結構數組,然後將該結構數組傳遞給函數。這些功能並不重要,因爲我無法離開主程序。我的困惑是我的編譯器(gcc)說當我嘗試釋放each_event時未聲明each_event,但是沒有其他地方。當我註釋它編譯的自由語句時,valgrind說當我執行each_event [i] .timestamp = tim [i]行時,我有無效大小爲4的寫。由於valgrind告訴我錯誤在那裏(我使用gcc -g -O0編譯),所以上面那行註釋掉了,儘管我知道它必須表示下面的行。C -Struct數組,空閒問題,在結構數組中賦值的問題

#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 #_patterns\nTry 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] = '\0'; 
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;} 

回答

0

因爲each_eventif { ... }塊內聲明的,它是超出範圍的嘗試free()它的時間。或者在if { ... }的末尾釋放它,或者在main()開始處將聲明與其他聲明一起放入。但是,如果你使用後者,你可能會得到警告,可能會在未經初始化的情況下使用它,所以第一種選擇可能是最好的。

+0

就是這樣。我認爲使用malloc並把這個內存放在堆上就足夠了,但我想這個變量名會在塊之外消失。我每小時至少學習一次新東西! – jbf81tb