如何发送vector< vector< type>>.通过MapViewOfFile [英] How to send vector<vector<type>> via MapViewOfFile

查看:111
本文介绍了如何发送vector< vector< type>>.通过MapViewOfFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在父进程中有以下代码:

I have the following code in a parent process:

vector<vector<double> > matrix(n); /* matrix NxM */
/* pushing data */
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
    0, PAGE_READWRITE, 0, 2*sizeof(int) + sizeof(double)*n*m, lpName);

LPVOID lp = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);

mat_tmp* tmp = (mat_tmp*)lp;
tmp->n = n; tmp->m = m;
tmp->vv = vector<vector<double> >(matrix.begin(), matrix.end());

在子进程中,我试图接收此向量>,但是子进程每次都终止.

In a child process I'm trying to receive this vector >, but my child process terminate every time.

LPTSTR lpName = _tcsdup(TEXT(map_name));
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
    0, PAGE_READWRITE, 0, 2*sizeof(int) + sizeof(double)*n*m, lpName);
LPVOID lp = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);

mat_tmp * tmp = (mat_tmp*)lp;
vector<vector<double> > matrix(tmp->vv.begin(), tmp->vv.end());

当我尝试在子进程中使用matrix中的数据时发生错误. 小结构如下:

Error occurs when I'm trying to use data from matrix in child process. And the little struct is following:

struct mat_tmp
{
    int n; int m; vector<vector<double> > vv;
};

我如何在儿童中正确接收我的病媒?

How can I receive my vector correctly in a child?

推荐答案

目前无法按照您的方式进行.映射的内存将包含n& ;; m和vector类的内部结构,这可能是几个指针,具体取决于您的STL实现.其次是一堆垃圾.您将无法从另一个进程安全地访问这些指针,因为它位于不同的地址空间中.

Its not going to work the way you are doing it at the moment. The mapped memory will contain n & m and the internal structure of the vector class which will probably be a couple of pointers, depending on your STL implementation. Followed by a load of junk. You'll not be able to safely access these pointers from the other process as it is in a different address space.

您实际上需要将向量指向的内存复制到映射的内存中,并在另一侧重建向量.

You need to actually copy the memory pointed to by the vector into the mapped memory and reconstruct the vector on the other side.

此外,您所需的内存大小不会是sizeof(int)* n * m.看来您想拥有2个整数n&结构中的m和n * m个double的集合.

Additionally the size of the memory you need is not going to be sizeof(int)*n*m. It looks like you want to have 2 ints n & m in the structure and an collection of n*m doubles.

因此,分配2 * sizeof(int)+ n * m * sizeof(double).复制n& m到内存中,然后将矩阵的行复制到其中. 在接收端,读取n& m,然后将数据反序列化为向量的向量.

So, allocate 2*sizeof(int) + n*m*sizeof(double). Copy n & m into the memory and then copy the rows of the matrix in. On the receiving side, read n & m and then deserialize the data into your vector of vectors.

也许使结构如下:

struct mat_tmp
{
    int n; int m; double vv[1];
};

然后,直接复制到共享内存中:

Then, copy directly into the shared memory:

double* dst = tmp.vv;
for (size_t y = 0; y < matrix.size(); ++y) {
   memcpy(dst, &(matrix[y][0]), matrix[y].size()*sizeof(double));
   dst += matrix[y].size();
}

另一方面,

dst_matrix.resize(n);
for (size_t y = 0; y < n; ++y) {
    dst_matrix[y].resize(m);
    memcpy(&(dst_matrix[y][0]), tmp + (y * m), m * sizeof(double));
}

所有未经测试的东西,可以做得更好,但希望它能更好地解释您需要做的事情.

All untested and could be made more optimal but hopefully it explains better what you need to do.

这篇关于如何发送vector&lt; vector&lt; type&gt;&gt;.通过MapViewOfFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆