问题描述
我的任务是在动态库中为指针创建一个存储器
并释放在main函数中分配的内存..
我可以在同一侧创建和释放内存..
但反之亦然..
我的图书馆功能代码:
my task is create a memory for pointer in dynamic library
and free up the memory of allocated in the main function..
i can do create and free the memory in same side..
but i can''t perform the operation in vice versa..
my library function code:
DYNAMICLIB_API int* aa1(void)
{
int *a1;
a1=(int*)malloc(sizeof(int));
printf("Interger created\n");
printf("bye");
printf("good");
printf("memory created");
free(a1);
return a1;
}
my main function code:
int main(int argc, char* argv[])
{
int *a1;
a1=aa1();
printf("Hello World!\n");
free(a1);
return 0;
}
免费功能会导致运行时错误..能帮帮我吗
[edit]已添加代码块-OriginalGriff [/edit]
the free function provide the run time error.. can you help me
[edit]Code block added - OriginalGriff[/edit]
推荐答案
free(a1);
return a1;
然后,您尝试再次释放它!
坏主意-永远不要返回指向任何内容的指针,除非您分配了它,并且还没有释放它.其中包括以下内容:
Then, you try to free it again!
Bad idea - never, ever return a pointer to anything unless you allocated it, and haven''t freed it. That includes things like this:
int* GetBadPointer(void)
{
int i = 76;
return &i;
}
除非您非常小心地使用指针,否则它总是会在某些时候吹到您的脸上,除非您非常小心!!
It will always blow up in your face at some point, normally when you don''t expect it, unless you are very careful with your pointers!
这篇关于在c中释放内存的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!