0

I have this part of code:

struct IARM_DATA
{
    IARM_HANDLE handle;
    float position[6];  //May be joint or linear position
    float gripper_opening;
    IARM_LIFT_POSITION lift_position;
    bool foldIn;  //true if foldin and false if fold out
    bool openGripper;

    IARM_DATA(IARM_HANDLE _handle, bool _foldIn):handle(_handle),foldIn(_foldIn){}; 

    IARM_DATA(IARM_HANDLE _handle, IARM_LIFT_POSITION _lift_position):handle(_handle),
        lift_position(_lift_position){}; //handle, lift_position

    IARM_DATA(IARM_HANDLE _handle, volatile float _position[6], float _gripper_opening, IARM_LIFT_POSITION _lift_position):
    handle(_handle),gripper_opening(_gripper_opening), lift_position(_lift_position)
    {
        for(int i=0; i<6; i++)
            position[i] = _position[i];
    }; //handle, position, gripper_opening, lift_position

} ;

typedef IARM_RESULT (iArmDriver::*singleMvt)(IARM_DATA); //This is a pointer on an iArmDriver method.

typedef struct {
    singleMvt mvt;
    IARM_DATA data;
} ONE_MVT;

And a queue declaration as this:

std::queue<ONE_MVT> *iarmActions;

My question is, i faced an execution problem due to that queue, should I make a dynamic allocation like that:

iramActions = (std::queue<ONE_MVT> *)sife0f(std::queue<ONE_MVT> *); ??

Thanks in advance

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60

2 Answers2

2

You should not use a dynamic allocation. Just create the queue in automatic storage:

std::queue<ONE_MVT> iarmActions;

If you really need dynamic allocation (which I doubt), use a smart pointer. The exact type of smart pointer depends on your use case. This example uses a unique_ptr, which is usually the default choice:

std::unique_ptr<std::queue<ONE_MVT>> iarmActions(new std::queue<ONE_MVT>());
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I need to make it dynamic, i have been working with it static. – Houssam Badri Mar 12 '13 at 07:09
  • I needed it in multithreaded code, instead of using it volatile, i can pass it just as pointer. – Houssam Badri Mar 12 '13 at 07:11
  • @HoussemBdr that doesn't make any sense... you can pass a reference or a pointer to this automatic storage object. – juanchopanza Mar 12 '13 at 07:12
  • @LukaRahne I am reproducing what OP had originally, i.e. a single, dynamically allocated queue. – juanchopanza Mar 12 '13 at 07:13
  • @HoussemBdr maybe have a look at a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Mar 12 '13 at 07:27
  • @HoussemBdr BTW I wrote something about a concurrent queue [here](http://juanchopanzacpp.wordpress.com/2013/02/26/concurrent-queue-c11/), but it does require some deeper C++ knowledge. Anyway, it could be useful to you too. – juanchopanza Mar 12 '13 at 07:34
0
std::queue<ONE_MVT> *iarmActions;

Why bother with a pointer to a container? You can just have the container - it will be default-constructed to have no elements / size 0, but won't use much space until you populate it.

std::queue<ONE_MVT> iarmActions;
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252