问题描述
我正在为我的大学一批资深的项目,我所遇到的主要障碍在试图让我的code工作。
I am working on a group senior project for my university and I have run into a major hurdle in trying to get my code to work.
这是我们为我们的8位微控制器爱特梅尔不支持新的或delete操作符,它不支持C ++ STL的编译器。我可以用C编程它,但我要实现A *算法,我从来没有做过的事情。虽然我已经试过ç最初我很快意识到,我从来没有以前那样纯C。试图模型结构和功能的对象正在放缓我失望,因为我太习惯了干净多了C ++的语法。
The compiler that we have for our 8 bit Atmel microcontroller does not support the new or delete operators, and it does not support the C++ STL. I could program it in C, but I have to implement an A* algorithm which I have never done before. While I have tried C initially I soon realized that I never did pure C before. Trying to model objects with structs and functions is slowing me down since I am so used to the much cleaner C++ syntax.
但无论如何,我的编译器缺点确切措辞可以在这里找到:http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus
Regardless, the exact wording for my compilers shortcomings can be found here: http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus
要克服这些困难,仍然使用C ++我已经考虑了以下可能性。
1)不要分配任何东西,只要使用模板来生成栈上的固定阵列。
2)分配和发现一些黑客来调用构造函数的对象一旦我分配的空间他们。安置新是不是一种选择,因为新的不是一个运营商。
3)只要使用C和吮吸它,它的微控制器为什么我越来越花哨?
4)找到一个更好的编译器这可能会花费$$$。
To overcome them and still use C++ I have considered the following possibilities.1) Don't allocate anything, just use templates to generate fixed arrays on the stack.2) Allocate and find some hack to call the constructor for objects once I have allocated the space for them. Placement new isn't an option since new isn't an operator.3) Just use C and suck it up, its a microcontroller why am I getting fancy?4) Find a better compiler which will probably cost $$$.
第二个选择是最困难的,但将有最大还清在我怎么可以这样写code条款。不过,我想,调试它可能是一个巨大的痛苦,如果我做错了。我想创建堆栈上的对象,它们的位复制到所分配的空间,然后归零位对象,因此它不会调用析构函数的。要做到这一点我会用一个unsigned char型指针,sizeof操作符直接访问该位得到的字节数。
The second option is the hardest but it would have the biggest pay off in terms of how I can write this code. However, I imagine that debugging it could be a huge pain if I get it wrong. I'm thinking of creating objects on the stack, copying their bits into the allocated space, and then zeroing the bits in the object so it doesn't call its destructor. To do that I would access the bits directly with an unsigned char pointer and the sizeof operator to get the byte count.
这听起来很可怕的,我不知道它是否能可靠地工作,但我认为它。我知道虚函数表可以是一个问题,但我不想有任何虚函数表,因为它仅仅是一个8位微控制器打算。
That sounds terrible and I don't know if it could work reliably, but I am considering it. I know vtables can be a problem but I don't intend on having any vtables since it is just an 8 bit microcontroller.
推荐答案
为了记录,在对象归零位将不会影响是否析构函数被调用(除非编译器有一个特殊的怪癖,使这种行为) 。只要写在你的析构函数中的一些日志语句测试了这一点。
Just for the record, zeroing the bits in an object won't affect whether the destructor gets called (unless the compiler has a special quirk that enables this behaviour). Just write some logging statements in your destructor to test this out.
构建你的程序没有分配任何东西可能是系统设计的方式。我没有与嵌入式系统工作过,但我已阅读,不鼓励使用动态内存,因为运行时环境具有稀缺数额是一些有经验的嵌入式商店。
Structuring your program not to allocate anything is probably the way the system was designed. I've not worked with embedded systems before, however I have read some experienced embedded shops that discourage use of dynamic memory because the runtime environment has scarce amounts of it.
不过,如果你一定要,你仍然可以使用新的位置。如果你不具备<新>
头,这里有来自直接相关的行我GCC版本:
However, if you must, you can still use placement new. If you don't have the <new>
header, here are the relevant lines directly from it on my version of GCC:
// Default placement versions of operator new.
inline void* operator new(std::size_t, void* __p) throw() { return __p; }
inline void* operator new[](std::size_t, void* __p) throw() { return __p; }
// Default placement versions of operator delete.
inline void operator delete (void*, void*) throw() { }
inline void operator delete[](void*, void*) throw() { }
在坚持按每个源文件包含的头文件的地方,放置使用新的/删除。
Stick that somewhere in a header file included by every source file that uses placement new/delete.
这是测试此示例文件:
#include <cstdio>
#include <new>
int
main(int argc, char** argv)
{
typedef char const* cstr;
char foobar[16];
cstr* str = new (&foobar) cstr(argc > 1 ? argv[1] : "Hello, world!");
std::puts(*str);
str->~cstr();
}
在我的版本的GCC,这不使用的libstdc ++
在所有(如 -fno-例外
使用)。
On my version of GCC, this does not use libstdc++
at all (if -fno-exceptions
is used).
现在,如果你想结合,与的malloc
(如果你的平台提供了这一点),那么你可以这样做:
Now, if you want to combine that with malloc
(if your platform provides this), then you can do this:
#include <cstdio>
#include <cstdlib>
inline void* operator new (std::size_t n) {return std::malloc(n);}
inline void* operator new[](std::size_t n) {return std::malloc(n);}
inline void operator delete (void* p) {std::free(p);}
inline void operator delete[](void* p) {std::free(p);}
int
main(int argc, char** argv)
{
typedef char const* cstr;
cstr* str = new cstr(argc > 1 ? argv[1] : "Hello, world!");
std::puts(*str);
delete str;
}
这使您可以使用标准的新
/ 删除
你是熟悉,而无需使用的libstdc ++
。
This allows you to use the standard new
/delete
that you're familiar with, without requiring use of libstdc++
.
祝你好运!
这篇关于你怎么能这样做C ++时,您的嵌入式编译器不具备运营商新的或STL支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!