0

my problem is with freeing a dynamic 2D array, that's inside linked list

typedef struct name{
    char **given_name;
    char **surname;
} Name;

typedef struct list {
    float id;
    struct name author_name;
    struct list *p_prev, *p_next;
} List;

void load_data(List** head){
     List* tmp_new = Null;
     while(1){
          tmp_new->author_name.given_name= (char**) malloc(10 * sizeof(char*));
          tmp_new->author_name.surname   = (char**) malloc(10 * sizeof(char*));
          while(condition){ //this condition is always whithin 1-10
               tmp_new->author_name.given_name[i]=(char*) malloc(200 * sizeof(char*));
               tmp_new->author_name.surname[i]   =(char*) malloc(200 * sizeof(char*));
         }
          given_name= "John"
          surname= "Blake"
          sscanf(name, "%s %s", tmp_new->author_name.given_name[i], tmp_new->author_name.surname[i]);
  
          tmp_new->p_prev = *head;
          tmp_new->p_next = NULL;
          (*head)->p_next = p_new;
          (*head) = p_new;

          // realloc again for the next enrty
         
           given_name= "Tom"
           surname= "Jerry"
          sscanf(name, "%s %s", tmp_new->author_name.given_name[i],tmp_new->author_name.surname[i]);
      }
     }
//once done within this function free it
if(*head != NULL) {
            while (1){
                if ((*head)->p_next!= NULL) {
                    tmp = (*head);
                    (*head) = (*head)->p_next;
                    for(int i=0; i<200; i++){
                            free(tmp->author_name.given_name[i]);
                            free(tmp->author_name.surname[i]);
                            }
                    free(tmp->author_name.given_name);
                    free(tmp->author_name.surname);
                    free(tmp);
                    }
                else {
                    break;
                }
            }
        }
}
int main()
    List* head = NULL;
    List* tmp;


    load_data(&head);
    if (head != NULL) {
        while (1){
            if (head->p_next!= NULL) {
                 tmp = head;
                 head = head->p_next;
                     for(int i=0; i<200;i++){     
                         free(tmp->author_name.given_name[i])   
                         free(tmp->author_name.surname[i])                             
                            }
                           free(tmp->author_name.given_name);
                           free(tmp->author_name.surname);
                           free(tmp);
                        }
                        else {
                            printf("free was sucesfull.\n");
                            break;
                        }
                    }
                }
                return 0;
   

whenever i try to free the ///(tmp->author_name.given_name[i]) or /// tmp->author_name.surname[i]) i get segmentation faults, i put inside printf to see where it would break and it even breaks on 0 index, which is always full, so the problem isn't that while condition that's within 1-10, have no idea what might be happening, maybe I'm doing something wrong with pointer arithmetic? and needs to be freed differently :/

D. K.
  • 137
  • 10
  • Try not casting the mallocs and let me know if it solves the problem – joaopfg Apr 24 '22 at 15:32
  • what do you mean not casting? I do need there to be 2D array, so i have to do it that way and i need them dynamic – D. K. Apr 24 '22 at 16:35
  • Check this question: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc#:~:text=In%20C%2C%20you%20don't,compiler%2C%20a%20cast%20is%20needed. – joaopfg Apr 24 '22 at 16:43
  • I just want to be sure that it's not the problem before making other hypothesis. Can you check if the problem is solved without casting the mallocs ? – joaopfg Apr 24 '22 at 16:45
  • 1
    tried without casting for both char** and char* and still got segmentation fault – D. K. Apr 24 '22 at 16:58
  • Shouldn't it be malloc(200 * sizeof(char)) instead of malloc(200 * sizeof(char*)) ? – joaopfg Apr 24 '22 at 17:03
  • daaam didnt get sementation fault with that :D what? let me test some more... edit : it did fix it within main, but in other function where i want to free it it still does fail – D. K. Apr 24 '22 at 17:05
  • it fixed it within main() but the free within other function (basicly the same except its tmp = (*head) it still fails – D. K. Apr 24 '22 at 17:09
  • Could you fill the blanks that you left in the comments with some data in the way you are doing ? I will try to reproduce – joaopfg Apr 24 '22 at 17:12
  • 1
    did it, it may need some editing since im doing the loading from file but it should be enought to give basic idea – D. K. Apr 24 '22 at 17:20
  • tried again while testing other function and it now failed in main also :D :D lol – D. K. Apr 24 '22 at 17:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244159/discussion-between-d-k-and-john-doe). – D. K. Apr 24 '22 at 17:28

0 Answers0