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

Hybrid View

  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,449

    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
    Location
    Florida
    Posts
    12,635

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. 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)

  8. #8
    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);
    }

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: pointer to memory at offset and length

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

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

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  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
    Join Date
    Apr 1999
    Posts
    27,449

    Re: pointer to memory at offset and length

    Quote Originally Posted by cbpetro View Post
    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
    I think you're making this much more difficult than it seems.

    A pointer can point anywhere you place it. If you place it at a memory location, that's where it points to.

    How does simple C++ work?
    Code:
    int main()
    {
       const char *p = "abc123456";
       const char *p2 = p + 3;
    }
    Is this basically all of what you're trying to do? What does p2 point to after it is executed? Isn't it pointing to the "subblock" of p, to be more exact, where the "123456" starts?

    Regards,

    Paul McKenzie

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  15. #15
    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?

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
  •  





Click Here to Expand Forum to Full Width

Featured