【发布时间】:2025-12-12 18:55:03
【问题描述】:
我实际上在使用一个简单的程序时遇到了麻烦,该程序应该通过命名管道传递结构。
这是我的 main.cpp:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include "NamedPipe.hh"
int main()
{
pid_t pid;
std::string str("test_namedPipe");
NamedPipe pipe(str);
message *msg;
//Initialisation of my struct
msg = (message *)malloc(sizeof(message) + sizeof(char) * 12);
msg->type = 1;
sprintf(msg->str, "Hello World");
//Forking
pid = fork();
if (pid != 0) {
pipe.send(msg);
} else {
message msg_receive = pipe.receive(); //Here is the overflow
std::cout << "type: " << msg_receive.type << " file: " << msg_receive.str << std::endl;
}
return (0);
}
我的 NamedPipe.cpp:
#include "NamedPipe.hh"
#include <stdio.h>
NamedPipe::NamedPipe(std::string const &_name) : name("/tmp/" + _name) {
mkfifo(name.c_str(), 0666);
// std::cout << "create fifo " << name << std::endl;
}
NamedPipe::~NamedPipe() {
unlink(name.c_str());
}
void NamedPipe::send(message *msg) {
int fd;
int size = sizeof(char) * 12 + sizeof(message);
fd = open(name.c_str(), O_WRONLY);
write(fd, &size, sizeof(int));
write(fd, msg, (size_t)size);
close(fd);
}
message NamedPipe::receive() {
int fd;
int size;
message msg;
fd = open(name.c_str(), O_RDONLY);
read(fd, &size, sizeof(int));
read(fd, &msg, (size_t)size);
close(fd);
return (msg); //I debugged with printf. This actually reach this point before overflow
}
我的结构定义如下:
struct message {
int type;
char str[0];
};
我实际上认为这可能是内存分配的问题,但我真的不知道该怎么做才能解决这个问题。
感谢阅读/帮助!
【问题讨论】:
-
提示:
assert(size<sizeof(message))在read(fd, &msg, (size_t)size)之前 -
另外,你为什么在 C++ 中使用
malloc和原始指针? -
你是对的,我在这一点上遇到了一个错误......谢谢你的提示! (我其实是一个学习c++的c程序员)
-
我会放弃
str[0]的思路,直到您真正理解该语言为止。这是先进的东西,很难做到正确。只需使用std::vector<unsigned char>代替字节缓冲区。 -
本次培训的目的是通过命名管道发送一个包含 std::vector<:string> 和一个 int 的对象。我没有找到任何方法来发送整个对象并检索它,所以我尝试将它拆分为结构(消息)......