0

Below is my source code, I am trying to store sensor information on linked list. Sensor information is received by type of JSON. And, I confirmed that it is well parsed.

However, when i am trying to store received value, then "Segmentation fault " error is occurred. I think it is because of usage of double pointer of struct. How can i solve this?

//sensor
   if (!strcmp(key, "sensor") && (num_sen > 0))
   {
#if DEBUG
    fprintf(stderr, "\n********************SENSORINFO********************\n");
#endif
    SENSOR_CONFIG **p_sen = NULL;
    p_sen = mcfg->sensor_cfg;

    SENSOR_CONFIG ** firstnode = NULL;//첫번째 노드
    SENSOR_CONFIG ** lastnode = NULL;//마지막 노드
    if (num_sen > 0)
    {

     for (int i = 1; i < num_sen; i++)
     {
      json_t * arr_data, *obj2, *arr2;
      const char * key2;
      int k;
      json_array_foreach(obj, i, arr) {
       //각 센서 array 시작
       json_object_foreach(arr, key2, obj2) {

        p_sen = (SENSOR_CONFIG **)malloc(sizeof(SENSOR_CONFIG));//먼저 생성하고 보자

        arr_data = json_object_get(arr, key2);


        if (_eq("id"))
        {
         fprintf(stderr, "can read id\n");

         (*p_sen)->id = (int)json_integer_value(arr_data);
         fprintf(stderr, "id:");
         fprintf(stderr, "%d\n", (*p_sen)->id);
        }
        ...
       }
       (*p_sen)->next = NULL;
       if (firstnode == NULL) {//처음으로 실행되면

        firstnode = p_sen;
        lastnode = p_sen;
       }
       else {//처음이 아니면 뒤에 계속 추가. lastnode가 계속 바뀐다
        (*lastnode)->next = p_sen;//원래 lastnode가 가리키는 곳이 새로 생성된 노드의 값을 가리키게 한 후,
        lastnode = p_sen;//방금 생성한 노드가 맨 끝자리에 추가했으므로 방금 생성한 노드가 lastnode가 된다.
       }
      }//sensor array

     }
    }
    else
    {
     mcfg->sensor_cfg = NULL;
    }

   }
nayang
  • 157
  • 1
  • 14
  • 3
    Why are you using `SENSOR_CONFIG **` and not `SENSOR_CONFIG *`? Are your first and last nodes not single nodes? Please provide [**A Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). Too much above is left to having to guess and what some other code, not provided does. – David C. Rankin Mar 15 '18 at 08:46
  • SENSOR_CONFIG struct have another struct inside. So, I think it is needed to use sensor_config **. – nayang Mar 15 '18 at 08:47
  • 2
    If there is another struct inside, you might want to use `SENSOR_CONFIG *first;` and `first->other_struct` for the inner part. – Gerhardh Mar 15 '18 at 08:51
  • 5
    This is wrong: `p_sen = (SENSOR_CONFIG **)malloc(sizeof(SENSOR_CONFIG));` `p_sen` points to 'SENSOR_CONFIG*' not to `SENSOR_CONFIG` You allocate wrong size. And you do not allocate any memory for `*p_sen` – Gerhardh Mar 15 '18 at 08:52
  • it is also not working for me. I fixed to SENSOR_CONFIG *, and then print out all liked listed structure. There are no values. – nayang Mar 15 '18 at 08:54
  • Oh i understand – nayang Mar 15 '18 at 08:59
  • 3
    Using `**` is almost always a sign of an incorrect program. There's a few special cases where `**` makes sense: returning a pointer through a parameter to a function, or when iterating through a list of pointers such as a string array. In all other cases, the presence of `**` is an indication of a fundamentally wrong program design. See for example [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Mar 15 '18 at 09:08
  • 2
    @Anna Yang This if statement if (num_sen > 0) is redundant because this condition was already checked. – Vlad from Moscow Mar 15 '18 at 10:29
  • when calling any of the heap allocation functions: (`malloc` `calloc` realloc` ) 1) the returned type is `void*` which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc. 2) always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Mar 16 '18 at 17:42
  • for ease of readability and understanding. 1) consistently indent the code. Suggest each indent level be 4 spaces 2) separate code blocks (`for` `if` `else` `while` `do...while` `switch` `case` `default` ) via a single blank line – user3629249 Mar 16 '18 at 17:45
  • when asking a question about a run time problem. Post a [mcve], not just some snippet of code. – user3629249 Mar 16 '18 at 17:46
  • please post the definition of `sensor_config` – user3629249 Mar 16 '18 at 17:57
  • Strongly suggest re-designing your code so the acquiring of the data is NOT part of adding a instance of `sensor_config` to a linked list. in general, we only need the creating of the new entry in the linked list, not the method of acquiring the data (but we do need the format of the data) We do not need any of the 'debug' statements – user3629249 Mar 16 '18 at 18:01

0 Answers0