Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我将动态分配的变量传递给函数,从该函数返回后,当我在代码上运行输入时,其中一些输入会使程序崩溃。
正如你所看到的,我测试了argv数组,两次,一次在结束时divide,另一次在我返回之后divide。在第一次检查中,我得到了argvfrom index 0to的所有值argc ,但在2nd检查中(从divideback into返回后main) index0使for循环崩溃。
argv
divide
0
argc
2nd
main
for
所以很明显我做错了什么,知道什么吗?
argv =(char**) realloc(argv, (current+1)*sizeof(char*));
这可能是罪魁祸首。调用realloc()可以分配一个新的(可能更大的)内存块,将旧块中的数据复制到该内存块中,然后释放旧块。问题不在于您要更改argvin的值,main()而在于您没有更改它。您正在释放它指向的块,而无需更新 main()argv以指向新块。
realloc()
main()