简体   繁体   中英

create two dim array of char with malloc in C

i have created the following two dim array of char with malloc, called json_data. The allocation of mem process is working fine.

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);
}

but whenever I try to access the data, I get SegmentationFault error. Here are the methods I used to access data in json_data:

First one :

    for (int i = 0; i < NUMBER_OF_OBJECT; i++ ) {
        for (int j = 0 ; j < 10000; j++ ) {
            printf("%c", *(*(json_data +i) + j));
        }
    }

Second one :

    for (int i = 0; i < NUMBER_OF_OBJECT; i++ ) {
        for (int j = 0 ; j < 10000; j++ ) {
            printf("%c", json_data[i][j]);
        }
    }

Here is the full code, up until the foor loop :

    // 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);

}

It's crashing because your for loops are going through more elements than you allocated:

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

Ten thousand

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

One hundred thousand

The most likely reason for the "crash" is that - depending on the value of NUMBER_OF_OBJECT - at some point the system does not allocate memory any more. When allocating larger chunks of memory, especially when you do this in a loop, always check the result of malloc ; if it is NULL , then the system could not reserve the memory you requested:

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;
}

A malloc returning NULL will not lead to undefined behaviour; but without these checks, you might assign NULL to one of the elements that are later dereferenced. And this will then be UB, likely a segfault crash.

BTW: note that you just allocate memory, but you do not initialize it. As far as I know, this is undefined behaviour; Probably you did not show the code where you fill in content; otherwise, using calloc (instead of malloc ) would at least initialize the memory blocks to 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM