-
August 26th, 2013, 10:22 AM
#1
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);
}
-
August 26th, 2013, 12:28 PM
#2
Re: pointer to memory at offset and length
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
-
August 26th, 2013, 12:38 PM
#3
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?
-
August 26th, 2013, 01:10 PM
#4
Re: pointer to memory at offset and length
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.
-
August 26th, 2013, 01:24 PM
#5
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);
}
// how to get a length of the memory?
I don't really understand what you are requiring 'get a length of memory'?
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 26th, 2013, 01:44 PM
#6
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
-
August 26th, 2013, 01:57 PM
#7
Re: pointer to memory at offset and length
You're not making much sense. Returning a pointer doesn't involve moving or copying anything.
-
August 26th, 2013, 02:06 PM
#8
Re: pointer to memory at offset and length
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 26th, 2013, 02:18 PM
#9
Re: pointer to memory at offset and length
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.
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);
}
-
August 26th, 2013, 02:22 PM
#10
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.
Last edited by GCDEF; August 26th, 2013 at 02:26 PM.
-
August 26th, 2013, 02:36 PM
#11
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 26th, 2013, 02:56 PM
#12
Re: pointer to memory at offset and length
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);
}
-
August 26th, 2013, 03:09 PM
#13
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.
-
August 26th, 2013, 03:27 PM
#14
Re: pointer to memory at offset and length
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?
-
August 26th, 2013, 03:41 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|