C语言 为什么阵列不断恢复为0

du7egjpx  于 2023-01-16  发布在  其他
关注(0)|答案(1)|浏览(120)

我有一个一维数组,其中ive初始化为0,但由于某种原因,当我进入一个循环,并试图将其内容增加1时,位置0的值保持恢复为0,即使我将其增加1。

#include <stdio.h>
#include <stdlib.h>
#define TOTAL_V 3
#define NUM_CANDIDATES 7

int hex_age(unsigned short hex){
    unsigned short age = hex >> 9;
    if (age >18 && age <101)
        return age;
    else return 0;
}
int hex_gender(unsigned short hex){
    unsigned short gender = hex >> 7 & 3;
    return gender;
}
int hex_vote(unsigned short hex){
    unsigned short vote, tmp = hex & 0x7f , count = 0;
    if (tmp == 0)
        return 7;
    for (int i = 0 ; i<7; i++){
        if (tmp & 1 == 1){
            count++;
            vote = i;
        }
        tmp = tmp >> 1;
    }
    if (count > 1)
        return 7;
    return vote;
}

    
int main() {
    int s_votes = 0, f_votes = 0, v_count[NUM_CANDIDATES] = {0};
    unsigned short **v_info, hex_v_info , age , gender , vote;

    FILE *fp;

    fp = fopen("data1.dat" , "r");
    if (fp == NULL){
        fprintf(stderr ,"apotuxe o anoigmos tou arxeiou");
        exit(-1);
    }

    if (feof(fp)){
        fprintf(stderr, "to arxeio einai adeio");
        exit(-1);
    }

    
    while (fscanf(fp ,"%x", &hex_v_info) != EOF){
        age = hex_age(hex_v_info);
        if(age == 0)
            f_votes++;
        else {
            gender = hex_gender(hex_v_info);
            if (gender == 0)
                f_votes++;
            else{
                vote = hex_vote(hex_v_info);
                if (vote == 7)
                    f_votes++;
                else{
                    if (s_votes == 0){
                            v_info = malloc(sizeof(int *));
                            v_info[s_votes] =malloc(sizeof(int)* TOTAL_V);
                    }
                    else{
                        v_info = realloc(v_info , sizeof(int *)*(s_votes+1));
                        v_info[s_votes] = malloc(sizeof(int)*TOTAL_V);
                    }

                    v_info[s_votes][0] = age;
                    v_info[s_votes][1] = gender;
                    v_info[s_votes][2] = vote;
                    v_count[vote]++;
                    s_votes++;
                    }
                }
            }
    }
    fclose(fp);
    for (int i = 0; i<s_votes; i++)
        free(v_info);
    return 0;
}

由于某种原因,当我使用calloc创建数组时,它没有这个问题,有人知道为什么会发生这个问题吗

sd2nnvve

sd2nnvve1#

1.您声明了unsigned short hex_v_info(在我的系统中为2个字节),然后使用fscanf(fp ,"%x", &hex_v_info)从读取数据,其中格式字符串%x期望int的地址(在我的系统中为4个字节)。

  1. unsigned short **v_info但是你存储了一个int [s_votes]的数组。如果你的指针不一致,这将是一个问题。
  2. realloc(NULL, 1)定义得很好,所以只要用它来代替malloc() of the first element. You need to assign the result of realloc()'到一个临时变量,就可以处理NULL。否则会丢失数据和内存泄漏。
    1.如果s_votes〉1,free(v_info);会导致释放两次,并且仍然会泄漏在v_info[i]上分配的内存。
#include <stdio.h>
#include <stdlib.h>
#define TOTAL_V 3
#define NUM_CANDIDATES 7

// 0x1111 1110 0000 0000
int hex_age(unsigned short hex){
    unsigned short age = hex >> 9;
    if (age >18 && age < 101)
        return age;
    return 0;
}

// 0x0000 0001 1000 0000
int hex_gender(unsigned short hex){
    unsigned short gender = hex >> 7 & 3; // bit 7 and 6
    return gender;
}

// 0x0000 0000 0111 1111
int hex_vote(unsigned short hex){
    unsigned short vote, tmp = hex & 0x7f , count = 0; // bit 11 through 0
    if (tmp == 0)
        return 7;
    for (int i = 0 ; i<7; i++){
        if ((tmp & 1) == 1){
            count++;
            vote = i;
        }
        tmp = tmp >> 1;
    }
    if (count > 1)
        return 7;
    return vote;
}

int main() {
    int s_votes = 0, f_votes = 0, v_count[NUM_CANDIDATES] = {0};
    unsigned short hex_v_info;
    int **v_info = NULL;

    FILE *fp = fopen("data1.dat" , "r");
    if (!fp){
        fprintf(stderr ,"apotuxe o anoigmos tou arxeiou");
        exit(-1);
    }
    for(;;) {
        int rv = fscanf(fp ,"%hx", &hex_v_info);
        if(rv == EOF) break;
        if(rv != 1) {
            printf("err\n");
            return 1;
        }
        unsigned short age = hex_age(hex_v_info);
        if(!age) {
            f_votes++;
            continue;
        }
        unsigned short gender = hex_gender(hex_v_info);
        if (!gender) {
            f_votes++;
            continue;
        }
        unsigned short vote = hex_vote(hex_v_info);
        if (vote == 7) {
            f_votes++;
            continue;
        }
        int **tmp = realloc(v_info, sizeof *tmp * (s_votes + 1));
        if(!tmp) {
            // handle error: free v_info[i] and v_info?
            return 1;
        }
        v_info = tmp;
        v_info[s_votes] = malloc(sizeof **v_info * TOTAL_V);
        v_info[s_votes][0] = age;
        v_info[s_votes][1] = gender;
        v_info[s_votes][2] = vote;
        v_count[vote]++;
        s_votes++;
    }
    fclose(fp);

    printf("f_votes: %d\n", f_votes);
    for(size_t i = 0; i < s_votes; i++) {
        printf("%zu: %d %d %d\n",
            i,
            v_info[i][0],
            v_info[i][1],
            v_info[i][2]
        );
    }
    for (int i = 0; i< s_votes; i++)
        free(v_info[i]);
    free(v_info);
    return 0;
}

和输入文件file:

a081
a082

它似乎正确处理信息数据:

f_votes: 0
0: 80 1 0
1: 80 1 1

相关问题