Reputation: 43
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
#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.
Upvotes: 2
Views: 141
Reputation: 65580
If init_msgsz
is known at compile-time, you can make it a template parameter:
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;
};
Upvotes: 1
Reputation: 117856
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
struct msgbuf {
long msqtype;
std::vector<uint8_t> msgtext;
} send_message_buf, receive_message_buf;
Upvotes: 7