使用我的API修改新运算符
Modifying new operator using my API
我想使用处理器的自定义API修改工具链中的new
和delete
运算符。有一些内存分配问题,所以供应商说我必须这样修改它们。在工具链中,我转到名为"new"的头文件并检查了这些
void* operator new(std::size_t) throw (std::bad_alloc);
void* operator new[](std::size_t) throw (std::bad_alloc);
void operator delete(void*) throw();
void operator delete[](void*) throw();
我想做一些类似于使用的事情
using namespace std;
if( size == 0 )
size = 1;
while( true ){
void* pMem = my_api_malloc(size);
if( pMem )
return pMem;
}
这是正确的方法吗?我可以在my_api_malloc
是我应该使用的位置进行这样的更改吗。这是因为处理器主要使用C,并且无法识别C++运算符。
很抱歉由于缺乏声誉而无法在评论中写下这篇文章。我认为您不应该在while循环中执行malloc。如果操作系统无法为进程分配更多内存,那么无论您如何执行malloc,这都是徒劳的。此外,请注意不要使用C++中new
创建的free
,也不要使用malloc
创建的delete
。
C 中的内存分配
int *a = (int*)malloc(sizeof(int)*n);
if(!a)
return 1;
以及在C++中
try{
int *a= new int[n];
}
catch (std::bad_alloc &e){
// ...
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- C++ 运算符修改/元编程策略,用于不那么冗长的语法
- 通过运算符使用标量参数重写来修改类成员
- 在 c++ 中,如何修改运算符以便在同一语句中连续调用它两次
- 如何在不修改C 中的LHS参数的情况下实现` `运算符
- C++将索引器运算符添加到现有类而不进行修改
- 是否可以在 C++ 中使用运算符重载来更改(未修改的)包含函数的含义
- 比较字符串中的字符(由"="运算符修改)
- 如何在不修改操作数的情况下使用运算符重载链
- 使用我的API修改新运算符
- 由算术运算符成员函数修改的动态数组的复制构造函数
- 在传递给函数的 char 指针上使用下标运算符不会修改值。为什么?
- ++和+=运算符不修改原始对象是否"incorrect behaviour"?
- C++ 重载运算符不修改原始对象(通过引用传递)
- 插入运算符 (<<) 是否修改C++值?