C ++结构中数组的初始化

时间:2015-12-16 12:16:29

标签: c++ structure

我想在构造对象时在结构中启动数组大小:

class someclass{
public:
  someclass(int init_msgsz) { this->msgsz = init_msgsz; }

private:
   int msgsz;
   struct msgbuf {
      long msqtype;
      uint8_t msgtext[msgsz];
   } send_message_buf, receive_message_buf;
};

但有些事情是错的:-(任何人帮助我?
注意这个结构来自: msgrcv,msgsnd - 消息操作

更新

#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdint.h> //uint8_t == unsigned char
#include <string.h>

class communication
{
public:
    communication(key_t &init_key, int &init_msgflg, size_t init_msgsz);
    ~communication();

    int send_msg(uint8_t *send_msg);
    int receive_msg(uint8_t *rec_msg);

private:
    int msqid;
    key_t key;
    int msgflg;
    int msgsz;
    size_t buf_length;

    struct msgbuf {
        long    msqtype;
        uint8_t    *msgtext;
        } send_message_buf, receive_message_buf;
    int get_msgid();
};

communication::communication(key_t &init_key, int &init_msgflg, size_t init_msgsz)
{
    this->key = init_key;
    this->msgflg = init_msgflg;
    this->msgsz = init_msgsz;
    get_msgid();

    send_message_buf.msgtext = new uint8_t[this->msgsz];
    receive_message_buf.msgtext = new uint8_t[this->msgsz];
}
communication::~communication()
{
    delete [] send_message_buf.msgtext;
    delete [] receive_message_buf.msgtext;
    send_message_buf.msgtext = NULL;
    receive_message_buf.msgtext = NULL;
}

int communication::get_msgid()
{
    if ((this->msqid = msgget(this->key, this->msgflg )) < 0)
    {
        std::cout << "\n[erro]...msgget\n";
        return -1;
    }
    return 0;
}

int communication::send_msg(uint8_t *send_msg)
{
    std::copy ( send_msg, send_msg+this->msgsz,
    this->send_message_buf.msgtext);
    this->buf_length = sizeof(this->send_message_buf.msgtext);
    this->send_message_buf.msqtype = 1;

    if (msgsnd(this->msqid, &send_message_buf, this->buf_length, IPC_NOWAIT) < 0)
    {
        std::cout << "\n[erro]...msgsnd\n";
        return -1;
    }

    return 0;
}

int communication::receive_msg(uint8_t *rec_msg)
{
    if (msgrcv(this->msqid, &receive_message_buf, this->msgsz+1, 1, 0) < 0) 
    {
        std::cout << "\n[erro]...msgrcv\n";
        return -1;
    }

    std::copy ( this->receive_message_buf.msgtext,       
    this->receive_message_buf.msgtext+this->msgsz, rec_msg);

    return this->msgsz;
}


int main()
{

    size_t msgsz = 16;
    int msgflg = IPC_CREAT | 0666;
    key_t keySpi = 1001;

    unsigned int msg_size;
    uint8_t receive_msg[msgsz];
    uint8_t send_msg[] = {0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF};

    communication msgSpi(keySpi, msgflg, msgsz);
    msgSpi.send_msg(send_msg);
    msg_size = msgSpi.receive_msg(receive_msg);

    std::cout << "I got msg with size: " << msg_size << std::endl;
    for (int i = 0; i < msg_size; i++)
        std::cout << (int)i << ": " << (int)receive_msg[i] << std::endl;

    std::cout << "\n";
    return 0;
}

编译确定,但我无法在析构函数中删除mem。

2 个答案:

答案 0 :(得分:6)

必须在编译时知道数组的大小,而不是运行时。因此,您必须拥有动态数组或std::vector

struct msgbuf {
    long msqtype;
    std::vector<uint8_t> msgtext;
} send_message_buf, receive_message_buf;

答案 1 :(得分:1)

如果在编译时知道init_msgsz,则可以将其设为模板参数:

template <std::size_t Msgsz>
class someclass{
   static constexpr std::size_t msgsz = Msgsz;
   struct msgbuf {
      long msqtype;
      std::array<uint8_t, Msgsz> msgtext;
   } send_message_buf, receive_message_buf;
};