如何找到分配动态内存的C ++语言构造? [英] How to find C++ language constructs allocating dynamic memory?

查看:94
本文介绍了如何找到分配动态内存的C ++语言构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在嵌入式系统固件代码基础上工作,该代码当前使用动态内存分配。现在,出于可靠性原因,应删除所有动态内存分配。

I work on an embedded system firmware code-basis which currently uses dynamic memory allocation. Now, all dynamic memory allocation shall be removed for reliability reasons.

第一步,我删除了_sbrk syscall实现,以使所有动态分配都无法链接。由于我使用-ffunction-sections -fdata-sections进行编译,并使用-Wl,-gc-sections进行链接,因此,只要没有发生动态内存分配,我就希望链接步骤能够完成。 (这在过去的C项目中对我有用,在这里不确定C ++。)

As a first step, I removed the _sbrk syscall implementation, such that all dynamic allocations fail to link. Since I compile with -ffunction-sections -fdata-sections and link with -Wl,--gc-sections I expect the link step to complete as long as no dynamic memory allocation occurs. (This worked for me in past C projects, unsure about C++ here.)

尽管预计会出现以下链接器错误消息,但它无助于查找哪个C ++构造触发动态分配。

Although the following linker error message is expected, it does not help to find the C++ construct which triggers the dynamic allocation.

[...]arm-none-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status

有没有找到使用动态C ++语言构造的有效方法堆分配(除了明显使用 new 之外)?

Is there an effective way to find C++ language constructs, which use dynamic heap allocation (apart from the obvious use of new)?

EDIT :我在寻找静态工具,而不是运行时检测/分析技术。

EDIT: I am looking for a static tool, not a runtime instrumentation/analysis technique.

EDIT2 :(由于有人删除了适当的标签:)该项目针对的是STM32F4 MCU,并且编译并与基于GCC的arm-none-eabi- *工具链链接。

EDIT2: (Since somebody removed the appropriate tags:) The project targets an STM32F4 MCU and is compiled and linked with an GCC-based arm-none-eabi-* toolchain.

推荐答案

问题阻塞到sbrk的链接的em表示sbrk是堆管理过程的最底层,不必为所有分配调用。

The problem with blocking linkage to sbrk is that sbrk is the very bottom of the heap management process and is not necessarily called for all allocations. It is simply a means of adding to the existing heap pool.

new 运算符可能会过载或放置。 -new,因此甚至不需要执行动态分配或通过系统堆进行分配。但是,确实会执行 new 的实例至少会调用 malloc()

The new operator may be overloaded, or placement-new, so need not even perform dynamic allocation or allocation via the system heap. However instances of new that do will at least call malloc().

GNU链接器-cref 的输出可能会有所帮助;这将告诉您每个符号,哪些文件引用它们。不幸的是,GNU链接器没有ARM链接器具有的-callgraph 选项,该选项将显示导致任何特定功能的完整调用路径。

The GNU linker --cref output may help; this will tell you for each symbol, what files reference them. Unfortunately the GNU linker does not have the --callgraph option that the ARM linker has, which will show the complete call path that leads to any particular function.

避免显式链接动态内存分配可能意味着删除标准库的大多数C ++特定部分-当然是STL容器类, std ::字符串

Avoiding explicit linkage of dynamic memory allocation probably means removing most of the C++ specific parts of the standard library - certainly STL container classes, std::string.

这篇关于如何找到分配动态内存的C ++语言构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆