CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    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

    Can I return the block of memory between offset 1 and offset 2 in my GetMemory function?
    You don't return a 'block of memory'. You return a pointer to a particular memory location which the caller then uses as required.

    Based on your example use, I think this test program shows what you want. What has been confusing us is from your original function that you want to return a certain number of bytes. You don't do this and GetMemory does not need to know how many bytes are required as you are not allocating memory as the memory has already been allocated and m_lpdata points to the start of this block of already allocated memory. It is up to the caller of GetMemory how the memory pointer returned is used.

    This is a simple test program to demonstate what I think you are after.

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    const int noEntry = 30;		//Number of elements for m_lpdata for test purpose
    LPVOID	m_lpData;
    
    //Returns pointer to memory, offset bytes from start of m_lpData
    LPVOID GetMemory(DWORD offset)
    {
    BYTE* buffer = (BYTE*)m_lpData;
    
    	buffer += offset;	
    	return ((LPVOID)buffer);
    }
    
    
    int main()
    {
    	//Allocate some memory to m_lpdata for purpose of test
    	//noEntry entries of int
    	if ((m_lpData = calloc(noEntry, sizeof(int))) == NULL) {
    		puts("Cannot allocate memory");
    		return 1;
    	}
    
    	//Initialise memory pointed to by m_lpData for purpose of test
    	//Contains the numbers 1 2 3 4  etc
    	for (int m = 0; m < noEntry; ++m) 
    		((int*)m_lpData)[m] = m + 1;
    
    
    DWORD start = 10 * sizeof(int);  // start is after 10th int
    
    LPVOID subblock = GetMemory(start);
    
    //Cast subblock to an array of 4 ints
    int* array = (int*)subblock;
    
    	//Array contains 4 ints with values 11, 12, 13, 14
    	//Prints 11 12 13 14
    	for (int i = 0; i < 4; ++i) {
    		//assert(array[i] == 10 + i + 1);
    		printf("%i  ", array[i]);
    	}
    
    	puts("");
    	free(m_lpData);
    	return 0;
    }
    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)

  2. #17
    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

    This is an alternative way of doing it. In this case GetMemory() returns a pointer to new memory of the size specified which contains a copy of the memory pointed to by m_lpData from position offset for size bytes. When finished with use, this new memory needs to be freed.

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    const int noEntry = 30;		//Number of elements for m_lpdata for test purpose
    LPVOID	m_lpData;
    
    //Returns pointer to new memory which is a copy offset bytes from start of m_lpData number of bytes size
    LPVOID GetMemory(DWORD offset, DWORD size)
    {
    BYTE* buffer = (BYTE*)m_lpData;
    BYTE* array;
    
    	if ((array = (BYTE*)malloc(size)) == NULL) {
    		return NULL;
    	}
    
    	buffer += offset;	
    	memcpy(array, buffer, size);
    
    	return ((LPVOID)array);
    }
    
    
    int main()
    {
    	//Allocate some memory to m_lpdata for purpose of test
    	//noEntry entries of int
    	if ((m_lpData = calloc(noEntry, sizeof(int))) == NULL) {
    		puts("Cannot allocate memory");
    		return 1;
    	}
    
    	//Initialise memory pointed to by m_lpData for purpose of test
    	//Contains the numbers 1 2 3 4  etc
    	for (int m = 0; m < noEntry; ++m) 
    		((int*)m_lpData)[m] = m + 1;
    
    
    DWORD start = 10 * sizeof(int);  // start is after 10th int
    DWORD size = 4 * sizeof(int);   // subblock size is 4 ints
    
    //Allocate and return new memory - needs to be freed when finished
    //Should check for NULL return here in production code
    LPVOID subblock = GetMemory(start, size);
    
    //Cast subblock to an array of 4 ints
    int* array = (int*)subblock;
    
    	//Array contains 4 ints with values 11, 12, 13, 14
    	//Prints 11 12 13 14
    	for (int i = 0; i < 4; ++i) {
    		//assert(array[i] == 10 + i + 1);
    		printf("%i  ", array[i]);
    	}
    
    	puts("");
    	free(subblock);
    	free(m_lpData);
    	return 0;
    }
    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)

  3. #18
    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

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

    Re: pointer to memory at offset and length

    Quote Originally Posted by 2kaud View Post
    You don't return a 'block of memory'. You return a pointer to a particular memory location which the caller then uses as required.

    Based on your example use, I think this test program shows what you want. What has been confusing us is from your original function that you want to return a certain number of bytes. You don't do this and GetMemory does not need to know how many bytes are required as you are not allocating memory as the memory has already been allocated and m_lpdata points to the start of this block of already allocated memory. It is up to the caller of GetMemory how the memory pointer returned is used.

    This is a simple test program to demonstate what I think you are after.

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    const int noEntry = 30;		//Number of elements for m_lpdata for test purpose
    LPVOID	m_lpData;
    
    //Returns pointer to memory, offset bytes from start of m_lpData
    LPVOID GetMemory(DWORD offset)
    {
    BYTE* buffer = (BYTE*)m_lpData;
    
    	buffer += offset;	
    	return ((LPVOID)buffer);
    }
    
    
    int main()
    {
    	//Allocate some memory to m_lpdata for purpose of test
    	//noEntry entries of int
    	if ((m_lpData = calloc(noEntry, sizeof(int))) == NULL) {
    		puts("Cannot allocate memory");
    		return 1;
    	}
    
    	//Initialise memory pointed to by m_lpData for purpose of test
    	//Contains the numbers 1 2 3 4  etc
    	for (int m = 0; m < noEntry; ++m) 
    		((int*)m_lpData)[m] = m + 1;
    
    
    DWORD start = 10 * sizeof(int);  // start is after 10th int
    
    LPVOID subblock = GetMemory(start);
    
    //Cast subblock to an array of 4 ints
    int* array = (int*)subblock;
    
    	//Array contains 4 ints with values 11, 12, 13, 14
    	//Prints 11 12 13 14
    	for (int i = 0; i < 4; ++i) {
    		//assert(array[i] == 10 + i + 1);
    		printf("%i  ", array[i]);
    	}
    
    	puts("");
    	free(m_lpData);
    	return 0;
    }
    Thanks, I understand now. Your example is code is very helpful to clarify the point ...

    "You don't return a 'block of memory'. You return a pointer to a particular memory location which the caller then uses as required."

Page 2 of 2 FirstFirst 12

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