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.
我有一个字节数组,也就是unsigned char,称为内容。我想在单独的函数中将我的内容数组拆分为2个新数组。此函数还应确定每个块的大小,并为它们分配内存。此外,我希望能够在函数之外使用新数组及其大小。我的实现给了我分段错误,我必须用指针做错事。
Main function
主功能
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, ¶m, &data,
size_content, &size_param, &size_data);
// SEGMENTATION ERROR
putchar(param[0]);
return 0;
}
Split Content Function:
拆分内容功能:
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];
}
}
Get number of param bytes function:
获取param字节数的功能:
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;
}
Load file to memory:
将文件加载到内存:
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.
它必须是指针,我在这里做错了。
1 个解决方案
#1
2
Because of precedence rules, *param[i]
means *(param[i])
, but you actually want (*param)[i]
.
由于优先规则,* param [i]表示*(param [i]),但实际上你想要(* param)[i]。
Likewise, *data[i-*size_param]
means *(data[i-*size_param])
but you want (*data)[i-*size_param]
.
同样,* data [i- * size_param]表示*(data [i- * size_param])但你想要(* data)[i- * size_param]。
Add the extra brackets around *param
and *data
.
在* param和* data周围添加额外的括号。
#1
2
Because of precedence rules, *param[i]
means *(param[i])
, but you actually want (*param)[i]
.
由于优先规则,* param [i]表示*(param [i]),但实际上你想要(* param)[i]。
Likewise, *data[i-*size_param]
means *(data[i-*size_param])
but you want (*data)[i-*size_param]
.
同样,* data [i- * size_param]表示*(data [i- * size_param])但你想要(* data)[i- * size_param]。
Add the extra brackets around *param
and *data
.
在* param和* data周围添加额外的括号。