我写了一个使用堆栈ADT的程序.
main创建一个新堆栈,同时提供3个函数供用户使用:
Stack my_stack = sCreate (copy_int, free_int, print_int);
Run Code Online (Sandbox Code Playgroud)
当我打电话给'偷看'功能时:
printf ("Peek: |%d|\n\n", *(int*)sPeek(my_stack));
Run Code Online (Sandbox Code Playgroud)
我有内存泄漏.
peek函数看起来像这样:
Element sPeek (Stack stack){
if ((NULL == stack) || (0 >= stack->used_places))
return NULL;
Element returnElement = stack->copy_function(stack->stack_array[stack->used_places-1]);
if (NULL == returnElement){
free (returnElement);
return NULL;
}
return returnElement;
Run Code Online (Sandbox Code Playgroud)
它可能是由那里调用的copy_function引起的,它是用户给出的copy_int:
Element copy_int (Element element){
int *new_int = (int*) malloc(sizeof(int*));
*new_int = *(int*)element;
if (NULL != new_int)
return new_int;
else
return NULL;
Run Code Online (Sandbox Code Playgroud)
如何从copy_int释放指针(malloc)?
我没有成功搜索删除工作目录中除了子目录之外的所有文件的解决方案.
我找到了一种方法来删除所有目录中的所有文件,但我正在寻找一种方法来删除我所在的同一"级别"上的文件.