pointer to memory at offset and length
I'm working with memory mapped files and I have a block of memory that I've mapped to.
I want to write a function that returns a pointer to a portion of the mapped memory at an offset and length so I can write to it.
I've never worked with memory at this level, is what I'm attempting possible?
I know that mapping functions can map to a part of the file at length and offset but I'm not sure if I should make multiple calls to map the memory from the file or just map the memory once and work with the portions I'm interested in using my proposed GetMemory function
Code:
LPVOID m_lpData;
LPVOID GetMemory(DWORD pos, DWORD length)
{
BYTE* buffer = (BYTE*)m_lpData;
buffer += pos;
// how to get a length of the memory?
return ((LPVOID)buffer);
}
Re: pointer to memory at offset and length
Quote:
Originally Posted by
cbpetro
I'm working with memory mapped files and I have a block of memory that I've mapped to.
I want to write a function that returns a pointer to a portion of the mapped memory at an offset and length so I can write to it.
This is a Windows API question. You should ask in the Windows API forum.
Memory mapped files are OS dependent and may not even exist for some operating systems.
Regards,
Paul McKenzie
Re: pointer to memory at offset and length
Yeah, I considered that but I my question is related to parsing memory not to the mapping which is OS related.
I have the memory, how do I get pointer to a portion of that?
Re: pointer to memory at offset and length
Quote:
Originally Posted by
cbpetro
Yeah, I considered that but I my question is related to parsing memory not to the mapping which is OS related.
I have the memory, how do I get pointer to a portion of that?
Pointer to start + offset.
Re: pointer to memory at offset and length
Code:
LPVOID m_lpData;
LPVOID GetMemory(DWORD pos, DWORD length)
{
BYTE* buffer = (BYTE*)m_lpData;
buffer += pos;
// how to get a length of the memory?
return ((LPVOID)buffer);
}
Quote:
// how to get a length of the memory?
I don't really understand what you are requiring 'get a length of memory'?:confused:
m_lpData is a pointer to the start address of a block of memory. pos is an offset into that memory. So your function returns a pointer to a new memory address. But where does 'length of memory' come into it? Do you want a start and end memory address? In which case you will need to return 2 pointers.
Re: pointer to memory at offset and length
Let's say my memory; m_lpData, points to an int + 2 doubles + 100 structs of sum POD type
I want a function to return a pointer to the memory of 100 structs for instance
so offset would be sizeof(int) + 2 * sizeof(double), length would be 100 * sizeof(someStruct).
I think memmove is a solution but I don't want to copy any memory. That defeats the purpose of using memory mapping
Re: pointer to memory at offset and length
You're not making much sense. Returning a pointer doesn't involve moving or copying anything.
Re: pointer to memory at offset and length
Quote:
Originally Posted by
cbpetro
Let's say my memory; m_lpData, points to an int + 2 doubles + 100 structs of sum POD type
I want a function to return a pointer to the memory of 100 structs for instance
so offset would be sizeof(int) + 2 * sizeof(double), length would be 100 * sizeof(someStruct).
I think memmove is a solution but I don't want to copy any memory. That defeats the purpose of using memory mapping
If you just want to return a pointer to some part of memory, then all you need is the start and offset - you don't need the length.:confused:
Re: pointer to memory at offset and length
Quote:
Originally Posted by
2kaud
If you just want to return a pointer to some part of memory, then all you need is the start and offset - you don't need the length.:confused:
Code:
LPVOID m_lpData;
LPVOID GetMemory(DWORD pos, DWORD offset)
{
BYTE* buffer = (BYTE*)m_lpData;
buffer += pos;
// how to return block of the memory an offset from start position +pos ?
// pos in my case is sizeof(int) + 2 * sizeof(double)
return ((LPVOID)buffer);
}
Re: pointer to memory at offset and length
What are you doing with offset there? Is it always and int plus two doubles from the start? What is pos?
You need to think this through a bit. Perhaps reading up on pointer arithmetic would help.
Re: pointer to memory at offset and length
Sorry but still don't understand.
Lets say that m_lpdata points to memory location 1000.
At memory location starting 1000 say the data is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now give us an example of using GetMemory and what you expect it to return. Then we can figure out what the function is supposed to do and then how to code it.
Re: pointer to memory at offset and length
Quote:
Originally Posted by
2kaud
Sorry but still don't understand.
Lets say that m_lpdata points to memory location 1000.
At memory location starting 1000 say the data is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now give us an example of using GetMemory and what you expect it to return. Then we can figure out what the function is supposed to do and then how to code it.
m_lpData would not point to memory at any particular location but would point to a particular block of memory from which I want to get a sub-block so using your data...
Code:
// using your data off 20 ints
DWORD start = 10 * sizeof(int); // start is after 10th int
DWOD offset = 4 * sizeof(int); // subblock size is 4 ints
LPVOID subblock = GetMemory(start, offset);
// cast subblock to an array of 4 ints
int* array =(int*)subblock;
for(int i = 0; i < 4; ++i)
{
// array contains 4 ints with values 11, 12, 13, 14
assert(array[i] == 10 + i);
}
Re: pointer to memory at offset and length
This is a very confusing thread. An offset is a distance from a starting point, not a length of a block. Also, when you return the pointer to the beginning of your block, it's up to the caller to know where that block ends, unless as 2kaud asked earlier you want to return an end pointer, but that's a pretty unusual thing to do.
Re: pointer to memory at offset and length
Quote:
Originally Posted by
GCDEF
This is a very confusing thread. An offset is a distance from a starting point, not a length of a block. Also, when you return the pointer to the beginning of your block, it's up to the caller to know where that block ends, unless as 2kaud asked earlier you want to return an end pointer, but that's a pretty unusual thing to do.
I change length to offset because that appears to be confusing people.
Like I said in my original post I've never worked with memory at this level and I asked if what I'm doing is even possible. But it's hard for me to believe it's not. So hopefully to clarify let's say I have two offsets from the start of the memory where offset1 < offset2. Can I return the block of memory between offset 1 and offset 2 in my GetMemory function?
Re: pointer to memory at offset and length
You seem to either not be explaining yourself well, or not understanding the concept. Are you trying to copy the block of memory somewhere and return that, or just give access directly to the block of memory. If the former, they you'll probably want to use memcpy. If the latter, that question's been answered a number of times already.
The word length is confusing because in the context you're asking, it doesn't make much sense. Calling it offset instead only makes things worse.