简体   繁体   English

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

[英]C++ Initialization of array in structure

I would like initiated size of array in structure when I constructing object: 在构造对象时,我想在结构中初始化数组的大小:

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

But something is wrong :-( anybody help me? 但是出了点问题:-(有人帮我吗?
Note this structure is from: msgrcv, msgsnd - message operations 请注意,此结构来自: msgrcv,msgsnd-消息操作

Update: 更新:

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

Compilation OK, but I can't delete mem in destructor. 编译正常,但无法删除析构函数中的mem。

The size of an array must be known at compile time, not run time. 必须在编译时(而不是运行时)知道数组的大小。 Therefore you'd have to have either a dynamic array or a std::vector 因此,您必须具有动态数组或std::vector

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

If init_msgsz is known at compile-time, you can make it a template parameter: 如果在编译时知道init_msgsz则可以将其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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM