This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] 编译器/MSP430F5335:类的全局实例似乎位于堆上?

Guru**** 2378650 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/789552/compiler-msp430f5335-global-instance-of-a-class-seems-to-be-located-on-the-heap

器件型号:MSP430F5335

工具/软件:TI C/C++编译器

当我声明类的全局对象时、例如:

类 RxKey:公共基础{
公共:
RxKey (const char* key_name、const char* str、uint8_t len)
: name(key_name )、s(str)、l(len){}

RxKey (const char* str、uint8_t len)
: name(0), s(str),l(len){}
const char* const name;// regex 字符串
const char* const s; // regex 字符串
const uint8_t l; //最小缓冲器长度
Friend bool operator=(const RxKey& key1, const RxKey* key2);
Friend bool operator=(const RxKey* key1, const RxKey& key2);
Friend bool operator==(const RxKey& key1、const RxKey& key2);
Friend bool 运算符!=(const RxKey& key1、const RxKey& key2);

//---- TO_STRING()--------------------------------------------------
bool TO_string (strings* out){
if (name!= 0){
返回 out->push_str(name);
} 如果(s!= 0){
返回 out->push_str(s);
}
返回 out->push_str("no name");
}

//----列印()----------------------------------------------------------
bool TO_SERIAL_TX (SerialTXInterface* TX){
if (name!= 0){
返回 TX->PRINT (NAME);
} 如果(s!= 0){
返回 TX->PRINT;
}
返回 TX->PRINT ("无名称");
}
bool print_help (SerialTXInterface* TX){
TX->PRINT (s);
if (name!= 0){
TX->PRINT ("、");
TX->PRINT (NAME);
}
返回 true;
}
};

当预主函数运行时、它会调用 malloc 来实例化类、这是否意味着它位于动态存储器空间中?  我认为只有使用新运算符创建的变量才会最终出现在堆上、这种情况不是这样吗?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    通过调用 malloc 来实现新函数。  因此、它们都从堆中获取内存。

    当一个全局对象在 main 之前被实例化时、有可能调用新的来从堆中获取内存。

    谢谢、此致、

    乔治

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    是这样吗?  全局变量不 是动态变量 、为什么它们位于动态存储器空间中。  这似乎是意料之外的

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我没有说所有全局对象都通过调用 new 来分配动态内存。  我说这很有可能。  对于您显示的类、我看不到任何构造函数分配动态内存的位置、因此在这种特定情况下、我不会期望它。  也就是说,我看不到基类的定义,并且基类构造函数可能调用 new

    谢谢、此致、

    乔治