可能重复:
为什么我需要一个IoC容器而不是直接的DI代码?
我一直在阅读有关依赖注入的内容,最好的解释来自James Shore.对于5美分概念,"依赖注入"是一个25美元的术语...依赖注入意味着为对象提供实例变量."
如果它是一个如此简单的概念,依赖注入框架的重点是什么?我应该什么时候使用?
我运行此代码时出现分段错误错误.我在gdb中运行它时没有收到错误.当我<17时,我也没有得到这个错误.
void test()
{
struct node *listHead=NULL;
int i=0;
while(i<17)
addTail(&listHead,createNode(i++));
}
struct node* createNode(int i)
{
struct node *n = malloc(sizeof(*n));
n->item = i;
return n;
}
void addTail(struct node **listHead, struct node *n)
{
if(*listHead!= NULL)
{
struct node *temp = *listHead;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = n;
} else
{
*listHead= n;
}
}
Run Code Online (Sandbox Code Playgroud)