在XCode 3中构建我的项目(一个简单的可可应用程序)后,我收到以下错误消息:
ld: framework not found SDL
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但框架存在于此/Library/Frameworks/SDL.framework.如何让链接器找到它?
我在调试模式下测试了gcc上的一些代码,但不确定它是否适用于其他编译器.
我的问题是任何编译器将如何优化以下c ++代码(make_auto宏):
class A
{
private:
void *ptr;
public:
A(void* _ptr) {ptr = _ptr;}
~A() {free(ptr);}
};
#define make_auto(ptr) A(ptr)
int main ()
{
char *a = (char*)malloc(sizeof(char)),*b = (char*)malloc(sizeof(char));
make_auto(a);
make_auto(b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它总是会调用A的构造函数和析构函数吗?或者编译器会在任何情况下优化此代码并删除A(ptr)调用,因为它可以认为它不会被更多地使用.
UPD:
我知道boost和std :: auto_ptr.我有自己的内存管理器控制内存碎片,有助于避免内存泄漏.现在我想"教"我的内存管理器创建auto_ptrs,就像它在boost和stl中做的那样.
UPD2:
这是完整的代码,我正在使用,我认为是正确的:
class AutoPtr
{
private:
void *ptr;
Application *app;
public:
AutoPtr(Application *_app,void *a)
{
printf("constructor called\n");
ptr = a;
app = _app;
}
~AutoPtr()
{
fast_free(app,ptr);
printf("destructor called\n");
}
};
#define make_auto_ptr(app,ptr) AutoPtr(app,ptr)
static void AutoPtrTest1()
{
test_declare_app …Run Code Online (Sandbox Code Playgroud) 例如,我有下一个代码:
#include <set>
using namespace std;
struct SomeStruct
{
int a;
};
int main ()
{
set<SomeStruct *> *some_cont = new set<SomeStruct *>;
set<SomeStruct *>::iterator it;
SomeStruct *tmp;
for (int i = 0 ; i < 1000; i ++)
{
tmp = new SomeStruct;
tmp->a = i;
some_cont->insert(tmp);
}
for (it = some_cont->begin(); it != some_cont->end(); it ++)
{
delete (*it);
}
some_cont->clear(); // <<<<THIS LINE
delete some_cont;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在删除some_cont以避免内存泄漏或自动调用析构函数之前,是否需要调用"THIS LINE"?