user3348461
user3348461

Reputation: 3

Creating new struct multiple times

I am struggling to find a good way to fill a buffer with new data. I have a thread producing data from the sound card, and I want to share this data with other threads through a shared Object called Rawcontainer. The container holds a mutex and a ringbuffer, but when I try to fill the buffer I have noticed that the objects I am filling the buffer with all have the same memory address, making the whole buffer useless.

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';
}

Upvotes: 0

Views: 124

Answers (2)

Avt
Avt

Reputation: 17053

Use

std::cout << "Adressse fra Streamhandler:  " << p <<'\n';

instead of

std::cout << "Adressse fra Streamhandler:  " << &p <<'\n';

p is already a pointer. You do not need here to take its address.

Upvotes: 0

btse
btse

Reputation: 8021

You should be printing just p, not &p, since p already contains the address of the new struct. What you're doing is printing the address of the p variable, which could very easily be the same each time the function is called.

Upvotes: 1

Related Questions