Code:
#include "stdafx.h"
#include <string.h>
#include <windows.h>
#include <iostream>
using namespace std;
#define ISUCCESS 0
#define IFAILURE -1
const int N=10000;
const int FILESIZE = 10240;
char m_sFileName[25];
int *m_map;
bool m_bIsMapInitzd = false;
#ifdef _WIN32
HANDLE m_fHandle;
HANDLE m_mapHandle;
#endif
void getFileName()
{
strcpy(m_sFileName,"FileNameMmap.dat");
}
int InitMMap()
{
getFileName();
#ifndef _WIN32
/* Open a file for writing.
* - Creating the file if it doesn't exist.
* - Truncating it to 0 size if it already exists. (not really needed)
*
* Note: "O_WRONLY" mode is not sufficient when mmaping.
*/
m_fd= open(m_sFileName, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
if(m_fd == -1)
{
//Log Message to signal failure
error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error opening file for writing" ));
return IFAILURE;
}
if(lseek(m_fd, FILESIZE - 1, SEEK_SET) == -1)
{
error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error in seeking the file" ));
return IFAILURE;
}
if(write(m_fd, "", 1) != 1)
{
error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error in writing to the file" ));
return IFAILURE;
}
/* Now the file is ready to be mmapped.
*/
m_map = (int*)mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (m_map == MAP_FAILED)
{
close(m_fd);
error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error mmapping the file"));
return IFAILURE;
}
m_map = new int[FILESIZE];
m_bIsMapInitzd = true;
return ISUCCESS;
#else
m_fHandle = CreateFile(LPCWSTR(m_sFileName),GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (!m_fHandle)
{
//Log Message to signal failure
cout<<"Error opening file for writing"<<endl;
return IFAILURE;
}
int buff[1];
buff[0]=1;
DWORD dwBytesWritten;
WriteFile (m_fHandle, buff, 1,
&dwBytesWritten, NULL);
m_mapHandle = CreateFileMapping(m_fHandle, NULL, PAGE_READWRITE, 0,FILESIZE, NULL);
if (!m_mapHandle)
{
cout<<"Error creating file mapping"<<endl;
CloseHandle(m_fHandle);
return IFAILURE;
}
m_map = (int*)MapViewOfFile(m_mapHandle, FILE_MAP_ALL_ACCESS,0,0,FILESIZE);
if (!m_map)
{
cout<<"Error creating map view of the file"<<endl;
CloseHandle(m_mapHandle);
CloseHandle(m_fHandle);
return IFAILURE;
}
m_bIsMapInitzd = true;
return ISUCCESS;
#endif
}
int unmap()
{
if(false == m_bIsMapInitzd)
{
return ISUCCESS;//Unmap not required.Just return success
}
#ifndef _WIN32
/* Don't forget to free the mmapped memory
*/
if (munmap(m_map, FILESIZE) == -1)
{
error_exit("DPStrippedPartition::unmap" ,PARTITION_WARN,pmA2U("Error unmapping the file"));
/* Decide here whether to close(fd) and exit() or not. Depends... */
return ISUCCESS;//Unmap not required.Just return success
}
/* Un-mmaping doesn't close the file, so we still need to do that.*/
close(m_fd);
delete[] m_map;
m_map = NULL;
m_bIsMapInitzd = false;
return ISUCCESS;
#else
int check= UnmapViewOfFile(m_map);
if(0 == check)
{
//error_exit("DPStrippedPartition::unmap" ,PARTITION_WARN,pmA2U("Error unmapping the file"));
return IFAILURE;
}
CloseHandle(m_mapHandle);
CloseHandle(m_fHandle);
m_bIsMapInitzd = false;
return ISUCCESS;
#endif
}
int main()
{
int st = InitMMap();
if(st!=ISUCCESS)
{
cout<<"Error in mapping"<<endl;
return -1;
}
for(int i = 0;i<N;i++)
{
cout<<i<<endl;
m_map[i] = i;ąCrashes here at index 3072
}
for(int i =0;i<N;i++)
{
cout<<m_map[i]<<endl;
}
unmap();
return 0;
}
Similarly, if I use