【发布时间】:2018-12-30 19:49:53
【问题描述】:
我用 malloc 创建了以下两个暗淡的 char 数组,称为 json_data。 mem进程的分配工作正常。
char **json_data;
json_data = (char**)malloc(sizeof(char *) * NUMBER_OF_OBJECT);
for (int i = 0; i < NUMBER_OF_OBJECT; i++) {
*(json_data + i) = (char*)malloc(sizeof(char) * 10000);
}
但每当我尝试访问数据时,都会收到 SegmentationFault 错误。 以下是我用来访问json_data中数据的方法:
第一个:
for (int i = 0; i < NUMBER_OF_OBJECT; i++ ) {
for (int j = 0 ; j < 10000; j++ ) {
printf("%c", *(*(json_data +i) + j));
}
}
第二个:
for (int i = 0; i < NUMBER_OF_OBJECT; i++ ) {
for (int j = 0 ; j < 10000; j++ ) {
printf("%c", json_data[i][j]);
}
}
这是完整的代码,直到 foo 循环:
// define local variables
int CHAR_SIZE = size_in_char("main.json"); // size of file in bytes, each char represents a byte
int NUMBER_OF_OBJECT = 0; // number of object in json file
// array holding json data char by char
char *data = malloc( sizeof(char)* CHAR_SIZE );
store("main.json", data);
char **json_data;
json_data = (char**)malloc(sizeof(char *) * NUMBER_OF_OBJECT);
for (int i = 0; i < NUMBER_OF_OBJECT; i++) {
*(json_data + i) = (char*)malloc(sizeof(char) * 10000);
}
int j = 0; // index of data array
int bracket_counter = 0; // conter for opning and closing of json object
int obj_index = 0; // index of json_data array
while( data[j] != '\0') {
// start & end of an object
// k temporay index for each object
int k = 0, start = 0 , end = 0;
if ( data[j] == '{') {
if (bracket_counter == 0 ) {
// begining of json object
start = j; // stroe the value of begining
bracket_counter++;
k = j; // init the temp index
do {
// keep going until the end of json object
k++;
if (data[k] == '{') {
bracket_counter++;
}
if ( data[k] == '}') {
bracket_counter--;
}
} while (/*data[k] != '}' &&*/ bracket_counter != 0);
end = k; // store end of json object
// copy copy json object into json_data
strncpy(json_data + obj_index, data + start , end - start + 2 );
*(json_data + obj_index + ( end - start + 1 ) ) = '\0'; // add null terminated value at end
obj_index++;
}
j = k; // move data index to end of current object and contineu the parsing
}
j++;
}
printf("the json file contains %d objects\n", NUMBER_OF_OBJECT);
for (int i = 0; i < NUMBER_OF_OBJECT; i++ ) {
for (int j = 0 ; j < 10000; j++ ) {
printf("%c", json_data[i][j]);
}
}
void store(string filename, char *data) {
/*
open file named by filename.
read data and stored it in data[].
data[] need to be created and memory allocated in main function
*/
char buff = 1; // buffer for fgetc();
int j = 0 ; // index of array data[];
FILE *file = fopen(filename, "r");
if (!file) {
printf("ERROR OPENING FILE\n");
printf("press enter to exit...");
getc(stdin);
exit(cant_open_file);
}
while ( buff != EOF) {
// read char by char
buff = fgetc(file);
// escape whitespace char during the process
if ( buff != '\n' && buff != ' ' && buff != '\t') {
data[j++] = buff;
}
}
// add null terminated value at end of array
data[j] = '\0';
// close the file
fclose(file);
}
【问题讨论】:
-
什么是
NUMBER_OF_OBJECT? -
只是int类型的数字
-
它的定义是什么? 1? 10? 1000?在 for 循环中使用它很重要
-
json_data 是一个由 NUMBER_OF_OBJECT 个 json 对象组成的数组,每个对象的最大大小为 10000 个字符