C ++堆溢出导致硬错误

时间:2018-06-06 18:48:40

标签: c++

我试图测试堆溢出: 因此我在向量中添加了许多虚拟元素。 我的期望:某种异常(错误的分配或类似的东西)。

(因为向量是一个连续的内存区域,一旦向量增长,可能会发生重新分配。我想知道我是如何检测添加元素是否有效。 根据{{​​3}} push_back没有返回值。)

此外,我已经定义了一个自定义终止处理程序。自定义终止处理程序设置为,然后引发堆溢出。

我正在将终止处理程序设置为自定义处理程序

void CFatal_Error::Set_Termination_Handler()
{
    set_terminate(Termination_Handler);
}

void CFatal_Error::Termination_Handler()
{
    // crash and stop here
    configASSERT(0);
}

堆溢出是由

引发的
vector<uint32_t> test;
try
{
    for (uint32_t i = 0; i < UINT32_MAX; i++)
    {
        test.push_back(i);
    }
}
catch (const exception &ex)
{
    configASSERT(0);
}

首先,我希望我遇到异常,但我会直接遇到一个严重的错误。有人能解释一下这种行为吗?

使用的编译器标志:

Building file: ../Test_SD.cpp
Invoking: MCU G++ Compiler
\Debug
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 '-D__weak=__attribute__((weak))' '-D__packed=__attribute__((__packed__))' -DUSE_HAL_DRIVER -DSTM32F429xx -I"Inc" -I"Inc/User" -I"xxx/Inc/User/debug" -I"xxx/Inc/User/Testcases" -I"xxx/Inc/User/display" -I"xxx/Inc/User/SD" -I"xxx/Inc/User/SD/XML" -I"xxx/Drivers/STM32F4xx_HAL_Driver/Inc" -I"xxx/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy" -I"xxx/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F" -I"xxx/Middlewares/ST/STM32_USB_Device_Library/Core/Inc" -I"xxx/Middlewares/ST/STemWin/Config" -I"xxx/Middlewares/ST/STemWin/inc" -I"xxx/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc" -I"xxx/Drivers/CMSIS/Device/ST/STM32F4xx/Include" -I"xxx/Middlewares/Third_Party/FatFs/src" -I"xxx/Middlewares/Third_Party/FreeRTOS/Source/include" -I"xxx/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS" -I"xxx/Drivers/CMSIS/Include"  -g3 -pedantic -Wall -Wextra -Wconversion -fmessage-length=0 -std=c++17 -ffunction-sections -c -fno-rtti -MMD -MP -MF"Src/User/Testcases/Test_SD.d" -MT"Src/User/Testcases/Test_SD.o" -o "Src/User/Testcases/Test_SD.o" "../Src/User/Testcases/Test_SD.cpp"
Finished building: ../Src/User/Testcases/Test_SD.cpp

使用链接器标志

    Building target: xxx.elf
Invoking: MCU G++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -L"xxx\Middlewares\ST\STemWin\Lib"  -specs=nosys.specs -specs=nano.specs -u_printf_float -T"../STM32F429ZITx_FLASH.ld" -Wl,-Map=output.map -Wl,--gc-sections -fno-exceptions -fno-rtti -o "Giessomat.elf" @"objects.list"  -lSTemWin540_CM4_OS_GCC -lm
Finished building target: Giessomat.elf

make --no-print-directory post-build
Generating binary and Printing size information:

编辑条目

1 个答案:

答案 0 :(得分:2)

使用-fno-exceptions进行编译时,你肯定不会得到异常,这对像你这样的裸机应用程序来说非常正常。

相关问题