为什么这两个"相当于"代码最终没有相同的行为?

时间:2015-10-29 09:34:54

标签: c++ templates

On a separate post,我正在帮助George Edwards使用BLE API对某些代码进行分解。

我非常确定我发布的代码(使用分解模板)与原始代码相同。但是当George将其发送到嵌入式设备时,行为就不同了:

通过阅读代码,有人可以帮助确定这两段代码之间的区别 (它们似乎与我的观点完全相同视图)?

代码版本1 (使用此代码,可以访问特征的BLE描述符):

// create descriptor
GattAttribute nameDescr( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
// create descriptor array
GattAttribute *descriptors[] = {&nameDescr};
// create characteristic
WriteOnlyArrayGattCharacteristic<uint8_t,sizeof(percentageValue)> 
        percentageChar( PercentageUUID, 
                        percentageValue,
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                        descriptors, 
                        sizeof(descriptors) / sizeof(GattAttribute*) );
// create characteristic array
GattCharacteristic *characteristics[] = {&percentageChar, &timeChar, &UseProfileChar};
// create service
GattService        newService(newServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));

代码版本2 (使用此代码,特征的BLE描述符可访问):

template <typename T, unsigned NUM_ELEMENTS, template <typename T, unsigned NUM_ELEMENTS> class CharacType>
class CharacteristicWithNameDescrptorHelper
{
public:
    CharacteristicWithNameDescrptorHelper( const          UUID &uuid,
                                           T              valuePtr[NUM_ELEMENTS],
                                           uint8_t        additionalProperties,
                                           const std::string& name ) : 
        // create descriptor
        descriptor( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)name.c_str(), name.size() ) 
    {
        // create descriptor array
        descriptors[0] = &descriptor;
        // create characteristic:
        charac = new CharacType<T,NUM_ELEMENTS>( uuid, valuePtr, additionalProperties, descriptors, 1 );
    }

    ~CharacteristicWithNameDescrptorHelper()
    {
        delete charac;
    }

    CharacType<T,NUM_ELEMENTS>* charac;
    GattAttribute descriptor;
    GattAttribute *descriptors[1];
};

// create charcteristic, descriptor and descriptor array
CharacteristicWithNameDescrptorHelper<uint8_t,sizeof(percentageValue),WriteOnlyArrayGattCharacteristic> 
        percentageChar( PercentageUUID, 
                        percentageValue,
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                        "Percentage" );
// create characteristic array
GattCharacteristic *characteristics[] = {percentageChar.charac};
// create service
GattService        newService(newServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
可在此处找到

CharacteristicWithNameDescrptorHelper代码(如果需要)。

请注意,我帮助的人无法轻松调试代码,因为它是嵌入式代码。因此,以不同于进行代码审核的方式来调查问题很困难。

1 个答案:

答案 0 :(得分:2)

显然,GattAttribute不会将您传递给其构造函数的指针中的数据复制,而只是保存指针。

在原始代码中,这没有问题,因为它传递了一个始终有效的字符串文字。在第二个版本中,您向它传递了一个指针,该指针指向很快被破坏的string中的数据,因此指针变为无效。

这可以通过将字符串放在GattAttribute旁边存储的成员中并在成员上调用c_str()来解决。