段故障造成使用新的或malloc的嵌入式设备上 [英] Segment Fault caused by using new or malloc on embedded device
问题描述
我试图用 C ++
而不是的C
我的 MIPS建立我的应用程序基于嵌入式设备
。首先,我有一个链接的问题,你可以在这里看到 。这个问题定,我可以成功地建立我的申请。在我的code,我用的malloc
函数进行内存分配,但是当我调用这个函数,我得到段故障
消息。我换成的malloc
与新
运营商,但结果是一样的。欲了解更多信息,请参见波纹管code:
I am trying to build my application with C++
instead of C
for my MIPS based embedded device
. First, I had a link problem that you can see here. This issue fixed and I could build my application successfully. In my code, I use malloc
function for memory allocation, but when I call this function, I get "Segment Fault"
message. I replaced malloc
with new
operator, but result was same. For more information, see the bellow code:
int main(int argc, char** argv)
{
char* str = (char*)malloc(10 * sizeof(char)); //or char* str = new char[10];
strcpy(str, "Hello");
return 0;
}
在3号线执行,并呼吁,段故障
消息出现在屏幕上的malloc
功能。如果我使用 mipsel体系精灵-GCC
而不是 mipsel体系精灵-G ++
,我没有这个问题。
When line 3 is executed and malloc
function called, "Segment Fault"
message appears on the screen. If I use mipsel-elf-gcc
instead of mipsel-elf-g++
, I don't have this problem.
什么是错在这里?
推荐答案
库code代表的malloc()是likley是成熟的和正确的。 GNU工具链库需要目标特定移植层图书馆胶水目标硬件和/或操作系统。在malloc()函数,并在C ++的情况下新
,及其变体的相关制度code为 SBRK()
(或 sbrk_r()
重入,虽然这通常是本身的包装aropund SBRK()
)。
The library code for malloc() is likley to be mature and correct. GNU tool-chain libraries require a target specific porting layer to glue the library to your target hardware and/or OS. In the case of malloc(), and in C++ new
, and their variants the relevant system code is in sbrk()
(or sbrk_r()
for re-entrancy, though that is usually itself a wrapper aropund sbrk()
).
工具链供应商的实施的SBRK()
是likley是一个通用的存根,而不是针对特定的目标。您需要将已实施了您的运行环境。
The tool-chain vendor's implementation of sbrk()
is likley to be a generic stub and not tailored to your specific target. You will need to have implemented it for your runtime environment.
这篇关于段故障造成使用新的或malloc的嵌入式设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!