gpt4 book ai didi

c++ - 多次创建新结构

转载 作者:行者123 更新时间:2023-11-28 07:04:11 24 4
gpt4 key购买 nike

我正在努力寻找一种用新数据填充缓冲区的好方法。我有一个从声卡生成数据的线程,我想通过一个名为 Rawcontainer 的共享对象与其他线程共享这些数据。该容器包含一个互斥锁和一个环形缓冲区,但是当我尝试填充缓冲区时,我注意到我填充缓冲区的对象都具有相同的内存地址,从而使整个缓冲区变得无用。

void useBuffer(){
//Create a new "OBject" (struct) each time this methos is called??
SoundData *p = new SoundData();
//Copy the data of the sound into the the struct data field
memcpy(p->data, soundCopy, 2048*sizeof(double) );
//Put the struct into the buffer and forget about it?
Rawcontainer->writeRaw(p);
//This should print a differnt adress each time the method is called?, but it dosent!
std::cout << "Adressse fra Streamhandler: " << &p <<'\n';
}

最佳答案

您应该只打印 p,而不是 &p,因为 p 已经包含新结构的地址。您正在做的是打印 p 变量的地址,每次调用函数时该地址很容易相同。

关于c++ - 多次创建新结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com