Csound Csound-dev Csound-tekno Search About

[Csnd-dev] Using STL in C++ Opcodes

Date2016-06-29 16:17
FromEd Costello
Subject[Csnd-dev] Using STL in C++ Opcodes
Hi,

I'm trying to use the STL multimap object in a Csound opcode. I have added the object to a struct but when I attempt to use the insert method in any of the Csound init or process functions I get a bad access error.
Does anyone know why this may be the case?
Below is what my code looks like, the Sequencer_init function is called when the opcode initialises, I get an error when I try to insert there.

typedef struct Sequencer
{
    OPDS h;
    MYFLT *output;
    MYFLT *flag;
    MYFLT *scoreString;
   
    multimap<string, int> score;
    
} Sequencer;

int Sequencer_init(CSOUND *csound, Sequencer *self)
{
    self->score.insert(pair<string, int>("Stuff",369));
    return OK;
}

Thanks
Ed
--
Edward Costello

Date2016-06-29 16:31
FromMichael Gogins
SubjectRe: [Csnd-dev] Using STL in C++ Opcodes
If the memory for Sequencer is being allocated by Csound the constructor for the "score" field will not be called. In your Sequencer_init function you can call the in-place constructor to initialize the "score" field like this:

int Sequencer_init(CSOUND *csound, Sequencer *self)
{
    new (&self->score) multimap<string, int>();
    self->score.insert(pair<string, int>("Stuff",369));
    return OK;
}

You will also have to do something to call the destructor as well.

Regards,
Mike


-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

On Wed, Jun 29, 2016 at 11:17 AM, Ed Costello <phasereset@gmail.com> wrote:
Hi,

I'm trying to use the STL multimap object in a Csound opcode. I have added the object to a struct but when I attempt to use the insert method in any of the Csound init or process functions I get a bad access error.
Does anyone know why this may be the case?
Below is what my code looks like, the Sequencer_init function is called when the opcode initialises, I get an error when I try to insert there.

typedef struct Sequencer
{
    OPDS h;
    MYFLT *output;
    MYFLT *flag;
    MYFLT *scoreString;
   
    multimap<string, int> score;
    
} Sequencer;

int Sequencer_init(CSOUND *csound, Sequencer *self)
{
    self->score.insert(pair<string, int>("Stuff",369));
    return OK;
}

Thanks
Ed
--
Edward Costello


Date2016-06-29 17:33
FromEd Costello
SubjectRe: [Csnd-dev] Using STL in C++ Opcodes
Excellent, that worked! Thanks Mike.


On Wed, 29 Jun 2016 at 16:32 Michael Gogins <michael.gogins@gmail.com> wrote:
If the memory for Sequencer is being allocated by Csound the constructor for the "score" field will not be called. In your Sequencer_init function you can call the in-place constructor to initialize the "score" field like this:

int Sequencer_init(CSOUND *csound, Sequencer *self)
{
    new (&self->score) multimap<string, int>();

    self->score.insert(pair<string, int>("Stuff",369));
    return OK;
}

You will also have to do something to call the destructor as well.

Regards,
Mike


-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

On Wed, Jun 29, 2016 at 11:17 AM, Ed Costello <phasereset@gmail.com> wrote:
Hi,

I'm trying to use the STL multimap object in a Csound opcode. I have added the object to a struct but when I attempt to use the insert method in any of the Csound init or process functions I get a bad access error.
Does anyone know why this may be the case?
Below is what my code looks like, the Sequencer_init function is called when the opcode initialises, I get an error when I try to insert there.

typedef struct Sequencer
{
    OPDS h;
    MYFLT *output;
    MYFLT *flag;
    MYFLT *scoreString;
   
    multimap<string, int> score;
    
} Sequencer;

int Sequencer_init(CSOUND *csound, Sequencer *self)
{
    self->score.insert(pair<string, int>("Stuff",369));
    return OK;
}

Thanks
Ed
--
Edward Costello

--
Edward Costello