Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;,结构中的std::list如何分配内存,在文件映射中使用该映射时,列表结果为空_C++_File Mapping - Fatal编程技术网

C++ 在C++;,结构中的std::list如何分配内存,在文件映射中使用该映射时,列表结果为空

C++ 在C++;,结构中的std::list如何分配内存,在文件映射中使用该映射时,列表结果为空,c++,file-mapping,C++,File Mapping,面临的问题: 使用文件映射共享的列表不提供列表中的任何数据 我有一个过程1,在这个过程中,我将所有数据存储为一个哈希映射列表,然后我尝试使用文件映射来共享它。。。在Process2中,当我尝试检索列表中的数据时,在列表中找不到任何数据 PS:我的exe与dll绑定,我将我的dll设置为process1,exe设置为process2 这是我的密码 过程1 /*这是通用头文件*/ typedef散列映射属性值; CString FileName=L“E:\\DataLog.txt”; TCHAR

面临的问题: 使用文件映射共享的列表不提供列表中的任何数据

我有一个过程1,在这个过程中,我将所有数据存储为一个哈希映射列表,然后我尝试使用文件映射来共享它。。。在Process2中,当我尝试检索列表中的数据时,在列表中找不到任何数据

PS:我的exe与dll绑定,我将我的dll设置为process1,exe设置为process2

这是我的密码

过程1

/*这是通用头文件*/
typedef散列映射属性值;
CString FileName=L“E:\\DataLog.txt”;
TCHAR szName[]=TEXT(“Local\MyFileMappingObject”)

struct-ADstruct
{
std::列表StList;
int i;
};
/*Sharememory.cpp*/
DWORD SharedMemory()
{ 
AttrValues HardCode;//为测试而硬编码的示例数据
硬编码[L“名称”]=L“测试”;
硬编码[L“D.Name”]=L“SAP”;
std::列表硬编码列表;
硬编码列表。推回(硬编码);
ADstruct-CheckStruct;
CheckStruct.i=10;
CheckStruct.StList=硬编码列表;
HANDLE hFile=无效的\u HANDLE\u值;//HANDLE hFile;
hFile=CreateFile(FileName.GetBuffer(),GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_ALWAYS,0,NULL);
if(hFile==无效的句柄值)
{
printf(“创建文件时出错…”);
返回0;
}
hMapFile=CreateFileMapping(
hFile,//使用分页文件
NULL,//默认安全性
PAGE_READWRITE,//读/写访问权限
0,//最大对象大小(高阶DWORD)
sizeof(ADstruct),//最大对象大小(低阶DWORD)
szName);//映射对象的名称
if(hMapFile==NULL)
{
_tprintf(文本(“无法创建文件映射对象(%d)。\n”),
GetLastError());
返回1;
}
ADstruct*ADobj=新的ADstruct;
ADobj=(ADstruct*)映射视图文件(hMapFile,//映射对象的句柄
文件\u映射\u所有\u访问,//读/写权限
0,
0,
sizeof(ADstruct));
CopyMemory((ADstruct*)ADobj和CheckStruct,sizeof(ADstruct));
未经批准的文件(pBuf);
CloseHandle(hMapFile);
返回0
}
过程2:

BOOL ReadMemoryMapping()
{
hash_-map-ADdata;
处理hMapFile;
HANDLE hFile=无效的\u HANDLE\u值;
hMapFile=OpenFileMapping(
文件映射所有访问权限,//读/写访问权限
FALSE,//不继承名称
szName);//映射对象的名称
if(hMapFile==NULL)
{
_tprintf(文本(“无法打开文件映射对象(%d)。\n”),
GetLastError());
返回1;
}
ADstruct*readstruct;
readstruct=(ADstruct*)映射视图文件(hMapFile,//映射对象的句柄
文件\u映射\u所有\u访问,//读/写权限
0,
0,
sizeof(ADstruct));
_tprintf(L“\n打印数据%d\n”,readstruct->i);
属性值在;
对于(std::list::iterator list_iter=readstruct->StList.begin();
list_iter!=readstruct->StList.end();list_iter++)
{
At.clear();
At=*list_iter;//****这里代码崩溃,因为里面没有数据***
if(At.empty()==1)
printf(“列表为空\n”);

std::wcout首先,常规的
std::list
是一种多级数据结构,包含对地址空间中其他位置的引用。实际的列表元素不存储在
std::list
对象中。它们存储在内存中的其他位置。
std::list
只是通过指针引用这些元素。您的文件映射只是共享顶级
std::list
对象,但它甚至不尝试共享实际的列表元素

第二,即使你以某种方式神奇地确定并共享了地址空间的整个区域,该区域保存着与你的
std::list
相关的所有内容,它仍然无法工作,除非你以某种方式确保在所有进程中,内存都映射到地址空间中完全相同的区域t使用指针

换句话说,这是做不到的。你不能仅仅获取一个现有的普通
std::list
并通过内存映射共享它


事实上,您尝试复制非平凡对象(如
std::list
)使用诸如
CopyMemory
之类的函数可能已经足以破坏对象的完整性。无需涉及内存映射。

首先,常规
std::list
是一种多级数据结构,其中包含对地址空间中其他位置的引用。实际的列表元素不存储在
std>中::list
对象。它们存储在内存中的其他位置。而
std::list
只是通过指针引用这些元素。您的文件映射只是共享顶级
std::list
对象,但它甚至不尝试共享实际的列表元素

第二,即使你以某种方式神奇地确定并共享了地址空间的整个区域,该区域保存着与你的
std::list
相关的所有内容,它仍然无法工作,除非你以某种方式确保在所有进程中,内存都映射到地址空间中完全相同的区域t使用指针

换句话说,这是做不到的。你不能仅仅获取一个现有的普通
std::list
并通过内存映射共享它

事实上,您尝试使用
CopyMemory
等函数复制非平凡对象(如
std::list
)可能已经足以破坏对象的int
/* this is in common headerFile */
typedef hash_map <std::wstring,std::wstring> AttrValues;
CString FileName = L"E:\\DataLog.txt";
struct ADstruct
{
    std::list<AttrValues> StList;
    int i;
};


/*Sharememory.cpp*/
DWORD SharedMemory()
{ 
                AttrValues HardCode;//Sample data which i am hard coding for testing
                HardCode[L"NAme"] = L"Test";
                HardCode[L"D.Name"] = L"SAP";
                std::list <AttrValues> HardCodedList;
                HardCodedList.push_back(HardCode);

ADstruct CheckStruct;

CheckStruct.i = 10;
        CheckStruct.StList = HardCodedList;

HANDLE hFile = INVALID_HANDLE_VALUE;// HANDLE  hFile;

    hFile = CreateFile(FileName.GetBuffer(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        printf("Error in creating a file..!");
        return 0;
    }
hMapFile = CreateFileMapping(
        hFile, // use paging file
        NULL, // default security
        PAGE_READWRITE, // read/write access
        0, // maximum object size (high-order DWORD)
        sizeof(ADstruct), // maximum object size (low-order DWORD)
        szName); // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
                                                                     GetLastError());
        return 1;
    }
ADstruct *ADobj = new ADstruct;



ADobj = (ADstruct *) MapViewOfFile(hMapFile, // handle to map object
                                  FILE_MAP_ALL_ACCESS, // read/write permission
                                  0,
                                  0,
                                sizeof(ADstruct) );


    CopyMemory( (ADstruct *) ADobj, &CheckStruct , sizeof(ADstruct) );
UnmapViewOfFile(pBuf);

    CloseHandle(hMapFile); 
return 0 
}
BOOL ReadMemoryMapping()
{
    hash_map<LPWSTR,LPWSTR> ADdata;

   HANDLE hMapFile;

   HANDLE hFile = INVALID_HANDLE_VALUE;
   hMapFile = OpenFileMapping(
                   FILE_MAP_ALL_ACCESS,   // read/write access
                   FALSE,                 // do not inherit the name
                   szName);               // name of mapping object

   if (hMapFile == NULL)
   {
      _tprintf(TEXT("Could not open file mapping object (%d).\n"),
             GetLastError());
      return 1;
   }
ADstruct * readstruct;

    readstruct = (ADstruct *) MapViewOfFile(hMapFile, // handle to map object
               FILE_MAP_ALL_ACCESS,  // read/write permission
               0,
               0,
              sizeof(ADstruct));
    _tprintf(L"\nprint data%d\n",readstruct->i);

    AttrValues At ;
    for(std::list<AttrValues>::iterator list_iter = readstruct->StList.begin(); 
        list_iter != readstruct->StList.end(); list_iter++)
    {
        At.clear();
         At = *list_iter; //*****Here the code crashes as there is no datas inside***
        if(At.empty() == 1)
            printf("List is empty\n");
        std::wcout<<endl<<endl<<"Attribute List In EXE : StList"<<endl;

        for(AttrValues :: iterator it1 = list_iter->begin(); it1!= list_iter->end(); it1++)
        {
            std::wcout<<it1->first<<endl;
            std::wcout<<it1->second<<endl;

        }
    }
 UnmapViewOfFile(readstruct);

   CloseHandle(hMapFile);

   return 0;
}
template<class T, class Allocator = std::allocator<T> > class list;