gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:51 25 4
gpt4 key购买 nike

我想在构造对象时在结构中初始化数组的大小:

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

编译正常,但我无法在析构函数中删除内存。

最佳答案

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

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

关于C++结构中数组的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com