Arraylist in C not working

前端 未结 4 696
旧时难觅i
旧时难觅i 2020-12-30 15:54

I am currently writing a program to implement an arraylist (or dynamic array) in C. Hmm... I think I have 70 - 80% done with it, however, I found a serious problem with my c

4条回答
  •  醉梦人生
    2020-12-30 16:28

    At first glance: in arraylist_add you declare new_data as a local variable. When you pass that to arraylist_set_data_collection, it passes the pointer to this data. However, once arraylist_add returns to main, new_data is out of scope, and is therefore no longer valid.

    Consider doing a deep copy and handling the memory manually with malloc and free.

提交回复
热议问题