gpt4 book ai didi

在C中使用malloc创建两个暗淡的char数组

转载 作者:行者123 更新时间:2023-11-30 19:03:26 25 4
gpt4 key购买 nike

我使用 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);

}

最佳答案

它崩溃了,因为你的 for 循环遍历的元素比你分配的要多:

*(json_data + i) = (char*)malloc(sizeof(char) * 10000);

一万

for (int j = 0 ; j < 100000; j++ ) {

十万

关于在C中使用malloc创建两个暗淡的char数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53980909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com