C++中的set_new_handler函数

首先,namespace std中有如下定义:

  Typedef void  (*new_handler)();

        new_handler  set_new_handler(new_handler  new_p) throw();//C++98

        new_handler  set_new_handler (new_handler  new_p) noexcept;//C++11

----------------------------------------------------------------------------------------------------------------------------------------------------------

译文开始:对于函数set_new_handler

函数说明

1.   set_new_handler函数的作用是设置new_p指向的函数为new操作或new[]操作失败时调用的处理函数。

2.   设置的处理函数可以尝试使更多空间变为可分配状态,这样新一次的new操作就可能成功。当且仅当该函数成功获得更多可用空间它才会返回;否则它将抛出bad_alloc异常(或者继承该异常的子类)或者终止程序(例如调用abortexit)。

3.   如果设置的处理函数返回了(例如,该函数成功获得了更多的可用空间),它可能将被反复调用,直到内存分配成功,或者它不再返回,或者被其它函数所替代。

4.   在尚未用set_new_handler设置处理函数,或者设置的处理函数为空时,将调用默认的处理函数,该函数在内存分配失败时抛出bad_alloc异常。

参数说明

new_p:该函数指针所指的函数应为空参数列表且返回值类型为void

该函数可以尝试获得更多的可用空间,或者抛出异常,或者终止程序。

如果是一个空指针,处理函数将被重置为默认值(将会执行抛出bad_alloc异常)。

返回值

返回先前被设置的处理函数指针;如果尚未被设置或者已被重置,将返回空指针。

返回的函数指针是无参的void返回值类型的函数指针。

下面是一个简单的示例程序:

// new_handler example
#include <iostream>     // std::cout
#include <cstdlib>      // std::exit
#include <new>          // std::set_new_handler

void no_memory () {
  std::cout << "Failed to allocate memory!\n";
  std::exit (1);
}

int main () {
  std::set_new_handler(no_memory);
  std::cout << "Attempting to allocate 1 GiB...";
  char* p = new char [1024*1024*1024];
  std::cout << "Ok\n";
  delete[] p;
  return 0;
}

new操作分配内存失败时将调用no_memory函数。

数据争用:

调用此函数不会引入数据竞争,任何这样的调用将会和随后set_new_handlerset_new_handler的调用同步。

注意,此要求仅适用于set_new_handler函数,但对于作为参数(new_p)传递的新处理函数却非必须。

异常安全:

无异常抛出保证:该函数(set_new_handler)不会抛出异常。

注意,如果new_p是没有实现适当功能的函数指针(见上面的参数说明),或者如果new_p是无效的指针,它会导致未定义的行为。

---------------------------------------------------------------------------------------------------------------------------------------------------------

原文如下:

Set new handler function

Sets new_p as the new-handlerfunction.

The new-handler function is a function which is called by thedefault allocation functions (operatornew and operatornew[]) when they fail to allocate storage.

The new-handler function may try to make more storageavailable for a new attempt to allocate the storage. If -and only if- thefunction succeeds in making more storage available, it may return. Otherwise itshall either throw a bad_allocexception (or a derived class) orterminate the program (such as by calling abort or exit).

If the new-handler function returns (i.e., it made morestorage available), it may be called repeatedly for as long as theallocationfunction fails to allocate the requested storage, or until the new-handlerfunction does not return or is replaced.

Before this function is called by the program for the first time, or if new_p isa null-pointer, the default allocation functions directlythrow bad_alloc on failure.


Parameters

new_p

Functionthat takes no arguments and returns no value (void).
The function can make more storage available, or throw an exception, orterminate the program.
If this is a null-pointer, the new-handler function isreset to none (and bad_alloc is thrown instead).
new_handler isa function pointer type for functions that take no arguments and return novalue.



Return value

The value ofthe current new-handler function if this has already been setby this function previously, or a null-pointer if this is thefirst call to set_new_handler (or if it was reset by a previouscall).

new_handler isa function pointer type taking no arguments and returning no value.

Data races

Calling this function does notintroduce data races, and any such call is synchronized with subsequent callstoset_new_handler and get_new_handler.

Notice that this requirement applies only to the set_new_handler function,but not necessarily to the new-handler functionpassed as argument (new_p).


Exception safety

No-throwguarantee: this function (set_new_handler) never throwsexceptions.

Notice that if new_p is a function that does not implement theproper functionality (described above), or if new_p is aninvalid pointer, it causes undefined behavior.



  • 14
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在PCL, `pcl::visualization::PointCloudColorHandler` 类是用于处理点云颜色信息的。其,`pcl::visualization::PointCloudColorHandlerCustom`、`pcl::visualization::PointCloudColorHandlerRGBField`和`pcl::visualization::PointCloudColorHandlerGenericField`分别用于处理自定义颜色、RGB颜色和通用颜色(例如,强度、曲率等)。 在使用 `fromHandlersToScreen` 函数将颜色处理器转换为 VTK 颜色数据时,可以采用以下步骤: 1. 确定点云点的数量。 ``` size_t num_points = cloud->size(); ``` 2. 创建一个 `vtkUnsignedCharArray` 对象来存储颜色数据。 ``` vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); colors->SetName("Colors"); colors->SetNumberOfComponents(3); colors->SetNumberOfTuples(num_points); ``` 3. 使用 `pcl::visualization::fromHandlersToRGB` 函数将颜色处理器转换为 RGB 颜色数据。 ``` std::vector<unsigned char> color_vec; color_handler->getColor(index, color_vec); Eigen::Vector3i color = pcl::visualization::fromHandlersToRGB(color_vec); ``` 4. 将 RGB 颜色数据存储到 `vtkUnsignedCharArray` 。 ``` colors->SetTuple3(index, color(0), color(1), color(2)); ``` 完整的代码示例: ``` pcl::PointCloud<pcl::PointXYZ> cloud; // create a color handler for the cloud pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_handler(cloud_ptr, 255, 0, 0); // determine the number of points in the cloud size_t num_points = cloud->size(); // create a vtkUnsignedCharArray object to store the color data vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); colors->SetName("Colors"); colors->SetNumberOfComponents(3); colors->SetNumberOfTuples(num_points); // loop through each point in the cloud and set the color for (size_t index = 0; index < num_points; index++) { // convert the color handler to RGB color data std::vector<unsigned char> color_vec; color_handler->getColor(index, color_vec); Eigen::Vector3i color = pcl::visualization::fromHandlersToRGB(color_vec); // store the RGB color data in the vtkUnsignedCharArray colors->SetTuple3(index, color(0), color(1), color(2)); } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值