pointer to memory at offset and length
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: pointer to memory at offset and length

  1. #1
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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);
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: pointer to memory at offset and length

    Quote Originally Posted by cbpetro View Post
    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

  3. #3
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: pointer to memory at offset and length

    Quote Originally Posted by cbpetro View Post
    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.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: pointer to memory at offset and length

    You're not making much sense. Returning a pointer doesn't involve moving or copying anything.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    Re: pointer to memory at offset and length

    Quote Originally Posted by cbpetro View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: pointer to memory at offset and length

    Quote Originally Posted by 2kaud View Post
    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);
    }

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    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.

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: pointer to memory at offset and length

    Quote Originally Posted by 2kaud View Post
    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);
    }

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    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.

  14. #14
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: pointer to memory at offset and length

    Quote Originally Posted by GCDEF View Post
    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?

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center