Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C中的malloc创建两维字符数组_C_Arrays_Malloc - Fatal编程技术网

用C中的malloc创建两维字符数组

用C中的malloc创建两维字符数组,c,arrays,malloc,C,Arrays,Malloc,我用malloc创建了以下两个dim字符数组,称为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); } char**jso

我用malloc创建了以下两个dim字符数组,称为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);
}
char**json\u数据;
json_data=(char**)malloc(sizeof(char*)*对象的数量);
for(int i=0;i<对象的数量;i++){
*(json_data+i)=(char*)malloc(sizeof(char)*10000);
}
但每当我试图访问数据时,就会出现SegmentationFault错误。 以下是我用来访问json_数据中数据的方法:

第一个:

    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
第二个:

    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
以下是完整的代码,直到foor循环:

    // 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);
//定义局部变量
int CHAR_SIZE=SIZE_in_CHAR(“main.json”);//文件大小(字节),每个字符代表一个字节
_对象的int NUMBER_=0;//json文件中的对象数
//一个字符一个字符地保存json数据的数组
char*data=malloc(sizeof(char)*char\u SIZE);
存储(“main.json”,数据);
字符**json_数据;
json_data=(char**)malloc(sizeof(char*)*对象的数量);
for(int i=0;i<对象的数量;i++){
*(json_data+i)=(char*)malloc(sizeof(char)*10000);
}
int j=0;//数据数组索引
int方括号_计数器=0;//用于选择和关闭json对象的内容
int obj_索引=0;//json_数据数组的索引
而(数据[j]!='\0'){
//对象的开始和结束
//k每个对象的时间索引
int k=0,开始=0,结束=0;
如果(数据[j]='{'){
如果(括号\计数器==0){
//json对象的开始
start=j;//选择begining的值
括号_计数器++;
k=j;//初始化临时索引
做{
//继续,直到json对象结束
k++;
如果(数据[k]='{'){
括号_计数器++;
}
如果(数据[k]='}'){
括号_计数器--;
}
}而(/*数据[k]!='}'和&*/括号内的计数器!=0);
end=k;//存储json对象的末尾
//将json对象复制到json_数据中
strncpy(json_数据+对象索引,数据+开始,结束-开始+2);
*(json_data+obj_index+(end-start+1))='\0';//在末尾添加以null结尾的值
obj_索引++;
}
j=k;//将数据索引移动到当前对象的末尾并继续解析
}
j++;
}
printf(“json文件包含%d个对象\n”,对象的数量);
for(int i=0;i<对象的数量;i++){
对于(int j=0;j<10000;j++){
printf(“%c”,json_数据[i][j]);
}
}
无效存储(字符串文件名,字符*数据){
/*
打开以文件名命名的文件。
读取数据并将其存储在数据[]中。
需要在主函数中创建数据[],并分配内存
*/
char buff=1;//fgetc()的缓冲区;
int j=0;//数组数据的索引[];
FILE*FILE=fopen(文件名,“r”);
如果(!文件){
printf(“打开文件时出错”);
printf(“按回车键退出…”);
getc(stdin);
退出(无法打开文件);
}
while(buff!=EOF){
//逐字符读取
buff=fgetc(文件);
//在此过程中转义空白字符
如果(buff!='\n'&&buff!='&&buff!='\t'){
数据[j++]=buff;
}
}
//在数组末尾添加以null结尾的值
数据[j]='\0';
//关闭文件
fclose(文件);

}

它崩溃了,因为for循环通过的元素比分配的元素多:

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

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

十万个

导致“崩溃”的最有可能的原因是——取决于对象的数量
的值——在某个时候系统不再分配内存。 分配较大的内存块时,尤其是在循环中执行此操作时,请始终检查
malloc
的结果;如果结果为
NULL
,则系统无法保留您请求的内存:

char **json_data;
json_data = (char**)malloc(sizeof(char *) * NUMBER_OF_OBJECT);
if (json_data == NULL) {
   printf("could not allocate memory.");
   exit(1);
}
for (int i = 0; i < NUMBER_OF_OBJECT; i++) {
   char* obj = (char*)malloc(sizeof(char) * 10000);
   if (obj == NULL) {
      printf("could not allocate memory.");
      exit(1); 
   }
   *(json_data + i) = obj;
}
char**json\u数据;
json_data=(char**)malloc(sizeof(char*)*对象的数量);
if(json_data==NULL){
printf(“无法分配内存”);
出口(1);
}
for(int i=0;i<对象的数量;i++){
char*obj=(char*)malloc(sizeof(char)*10000);
if(obj==NULL){
printf(“无法分配内存”);
出口(1);
}
*(json_数据+i)=obj;
}
返回
NULL
malloc
不会导致未定义的行为;但是如果没有这些检查,您可能会将
NULL
分配给稍后取消引用的元素之一。然后这将是UB,很可能是segfault崩溃


顺便说一句:请注意,您只分配内存,但不初始化内存。据我所知,这是未定义的行为;可能您没有在填写内容的地方显示代码;否则,请使用
calloc
(而不是
malloc
)是否至少将内存块初始化为
0

什么是
NUMBER\u OF_OBJECT
?它只是一个int类型的数字。它定义为什么?1?10?1000?在for loop中使用它很重要。json_数据是一个由NUMBER\u OF_OBJECT json OBJECT组成的数组,每个对象的最大大小为10000字符。抱歉,我需要更新这个错误问题是,我对分配和循环都使用了相同的长度10000,但得到的结果仍然相同error@ablil98当我修复这个问题并重新编译时,它对我来说就不再是故障了。问题一定是你没有发布的程序中的一部分。请在你的问题中编辑一个完整的程序来显示问题,可以是com没有添加或修改。我更新了这个问题,你可以看看完整的程序,直到loop@ablil98那是圣