自己define size_t编译出错

今天写程序,突然发现之前的一个程序编译会出错,奇怪之,之前都好好的呀。

后来定位问题,发现该程序引用了本工程common下的typedefine.h,而其中有这样的代码在此期间被添加:


#ifdef __x86_64__
#define     size_t  unsigned long
#else
#define     size_t  unsigned int
#endif

 

而这个程序还自己引用了 #include <string>

string里本身就include了系统的 size_t ,这样就冲突了。

 

解决办法:

不要自己写size_t,而是#include <include/linux/types.h>

#define _CRT_SECURE_NO_WARNINGS 1 #include"ConcurrentAlloc.h" // ntimes 一轮申请和释放内存的次数 // rounds 轮次 template<typename... Args> void safe_foo(Args&&... args) { // 使用完美转发处理参数 } void BenchmarkMalloc(size_t ntimes, size_t nworks, size_t rounds) { std::vector<std::thread> vthread(nworks); std::atomic<size_t> malloc_costtime = 0; std::atomic<size_t> free_costtime = 0; for (size_t k = 0; k < nworks; ++k) { vthread[k] = std::thread([&, k]() { std::vector<void*> v; v.reserve(ntimes); for (size_t j = 0; j < rounds; ++j) { size_t begin1 = clock(); for (size_t i = 0; i < ntimes; i++) { v.push_back(malloc(16)); //v.push_back(malloc((16 + i) % 8192 + 1)); } size_t end1 = clock(); size_t begin2 = clock(); for (size_t i = 0; i < ntimes; i++) { free(v[i]); } size_t end2 = clock(); v.clear(); malloc_costtime += (end1 - begin1); free_costtime += (end2 - begin2); } }); } for (auto& t : vthread) { t.join(); } printf("%u个线程并发执行%u轮次,每轮次malloc %u次: 花费:%u ms\n", nworks, rounds, ntimes, malloc_costtime); printf("%u个线程并发执行%u轮次,每轮次free %u次: 花费:%u ms\n", nworks, rounds, ntimes, free_costtime); printf("%u个线程并发malloc&free %u次,总计花费:%u ms\n", nworks, nworks * rounds * ntimes, malloc_costtime + free_costtime); } // 单轮次申请释放次数 线程数 轮次 void BenchmarkConcurrentMalloc(size_t ntimes, size_t nworks, size_t rounds) { std::vector<std::thread> vthread(nworks); std::atomic<size_t> malloc_costtime = 0; std::atomic<size_t> free_costtime = 0; for (size_t k = 0; k < nworks; ++k) { vthread[k] = std::thread([&]() { std::vector<void*> v; v.reserve(ntimes); for (size_t j = 0; j < rounds; ++j) { size_t begin1 = clock(); for (size_t i = 0; i < ntimes; i++) { v.push_back(ConcurrentAlloc(16)); //v.push_back(ConcurrentAlloc((16 + i) % 8192 + 1)); } size_t end1 = clock(); size_t begin2 = clock(); for (size_t i = 0; i < ntimes; i++) { ConcurrentFree(v[i]); } size_t end2 = clock(); v.clear(); malloc_costtime += (end1 - begin1); free_costtime += (end2 - begin2); } }); } for (auto& t : vthread) { t.join(); } printf("%u个线程并发执行%u轮次,每轮次concurrent alloc %u次: 花费:%u ms\n", nworks, rounds, ntimes, malloc_costtime); printf("%u个线程并发执行%u轮次,每轮次concurrent dealloc %u次: 花费:%u ms\n", nworks, rounds, ntimes, free_costtime); printf("%u个线程并发concurrent alloc&dealloc %u次,总计花费:%u ms\n", nworks, nworks * rounds * ntimes, malloc_costtime + free_costtime); } int main() { size_t n = 100000; cout << "==========================================================" << endl; BenchmarkConcurrentMalloc(n, 4, 10); cout << endl << endl; BenchmarkMalloc(n, 4, 10); cout << "==========================================================" << endl; return 0; } 错误 C2280 “std::atomic<unsigned int>::atomic(const std::atomic<unsigned int> &)”: 尝试引用已删除的函数 错误 C4839 将类 "std::atomic<unsigned int>" 作为可变参数函数的参数的非标准用法 帮我解决这两个错误
03-08
#include <Arduino.h> #include <driver/i2s.h> // 必须包含 // I2S 配置(输入:INMP441,输出:98357A) #define I2S_IN I2S_NUM_0 #define I2S_OUT I2S_NUM_1 #define IN_BCLK 13 #define IN_LRCK 12 #define IN_DATA 14 #define OUT_BCLK 27 #define OUT_LRCK 26 #define OUT_DATA 25 int16_t audio_buffer[1024]; void i2s_init() { // 输入配置(主模式接收) i2s_config_t in_conf = { .mode = I2S_MODE_MASTER | I2S_MODE_RX, // 位运算组合(无强制转换) .sample_rate = 44100, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, .communication_format = I2S_COMM_FORMAT_I2S, .dma_buf_count = 8, .dma_buf_len = 1024, .use_apll = false }; i2s_driver_install(I2S_IN, &in_conf, 0, NULL); i2s_pin_config(I2S_IN, &(i2s_pin_config_t){ .bck_io_num = IN_BCLK, .ws_io_num = IN_LRCK, .data_in_num = IN_DATA }); // 输出配置(主模式发送) i2s_config_t out_conf = { .mode = I2S_MODE_MASTER | I2S_MODE_TX, // 位运算组合 .sample_rate = 44100, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, .communication_format = I2S_COMM_FORMAT_I2S, .dma_buf_count = 8, .dma_buf_len = 1024, .use_apll = false }; i2s_driver_install(I2S_OUT, &out_conf, 0, NULL); i2s_pin_config(I2S_OUT, &(i2s_pin_config_t){ .bck_io_num = OUT_BCLK, .ws_io_num = OUT_LRCK, .data_out_num = OUT_DATA }); i2s_start(I2S_IN); i2s_start(I2S_OUT); } void setup() { Serial.begin(115200); i2s_init(); Serial.println("I2S 初始化完成"); } void loop() { size_t len; i2s_read(I2S_IN, audio_buffer, sizeof(audio_buffer), &len, portMAX_DELAY); i2s_write(I2S_OUT, audio_buffer, len, &len, portMAX_DELAY); }出现Compilation error: invalid conversion from 'int' to 'i2s_mode_t' [-fpermissive]报错请解决,并给出修改后完整代码
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值