C函数将数组拆分为块 [英] C function split array into chunks

查看:61
本文介绍了C函数将数组拆分为块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 bytes 的数组,又称为 content unsigned char .我想在单独的函数中将 content 数组拆分为2个新数组.此函数还应确定2个块中每个块的大小,并为其分配内存.另外,我希望能够在函数之外同时使用新数组及其大小.我的实现给我 segmentation fault ,我必须做一些指针错误的事情.

I have an array of bytes aka unsigned char called content. I want to split my content array into 2 new arrays in a separate function. This function should also determine the size for each of the 2 chunks and allocate memory for them. Also, I want to be able to use both the new arrays and their sizes outside of the function. My implementation is giving me segmentation fault and I must be doing something with pointers wrong.

主要功能

int main(int argc, char *argv[]) {

    byte *content = NULL;
    size_t size_content;

    byte *param = NULL;
    size_t size_param;

    byte *data = NULL;
    size_t size_data;

    // This works fine!
    size_content = load_file_to_memory(argv[1], "rb", &content);

    // split content
    split_content(content, &param, &data,
            size_content, &size_param, &size_data);

    // SEGMENTATION ERROR
    putchar(param[0]);

    return 0;
}

分割内容功能:

    void split_content(byte *content, byte **param, byte **data,
            size_t size_content, size_t *size_param, size_t *size_data) {
    *size_param = get_number_of_param_bytes(content);
    *size_data = size_content - *size_param;

    *param = malloc((*size_param) * sizeof(byte));
    *data = malloc ((*size_data) * sizeof(byte));

    for(size_t i=0; i<*size_param; i++) {
        *param[i] = content[i];
    }

    for(size_t i=*size_param; i<size_content; i++) {
        *data[i-*size_param] = content[i];
    }
}

获取参数字节数功能:

    size_t get_number_of_param_bytes(byte *content) {
    size_t num_whites = 0;
    size_t byte_index = 0;
    do {
        byte b = content[byte_index];
        if(isspace(b)) {
            num_whites += 1;
        }
        byte_index += 1;
    } while (num_whites < N_WS);
    return byte_index;
}

将文件加载到内存中

    size_t load_file_to_memory(const char *filename, const char *mode, byte **buffer) {
    size_t size = 0;

    FILE *file = fopen(filename, mode);
    if (file == NULL) {
        *buffer = NULL;
        printf("Unable to open file! Terminating...\n");
        exit(-1);
    }

    size = get_file_size(file);
    *buffer = malloc(size * sizeof(byte));

    if (fread(*buffer, sizeof(byte), size, file) != size) {
        printf("Error loading file into memory! Terminating...\n");
        free(*buffer);
        exit(-1);
    }

    fclose(file);
    return size;
}

在这里我做错的一定是带有指针的东西.

It must be something with the pointers that I am doing wrong here.

推荐答案

由于优先规则, * param [i] 表示 *(param [i]),但是您实际上需要(* param)[i] .

Because of precedence rules, *param[i] means *(param[i]), but you actually want (*param)[i].

同样, * data [i- * size_param] 表示 *(data [i- * size_param]),但您想要(* data)[i-* size_param] .

Likewise, *data[i-*size_param] means *(data[i-*size_param]) but you want (*data)[i-*size_param].

* param * data 周围添加多余的括号.

Add the extra brackets around *param and *data.

这篇关于C函数将数组拆分为块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆