CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2020
    Posts
    7

    [RESOLVED] copy the allocation address of pointer to a byte array ? NEED HELP!

    Hi, I am an user of MSVC++ and I am programming some small programs under Windows.

    I have an intermediate level at the c++ language so I can come up with some questions..

    For a case in the recent, I have a memory address in a BYTE pointer, like this.

    Code:
    PBYTE pAddress = 0x50505050;
    Code:
    // address of pAddress = 0x20304050
    then I want to store the address where pAddress is stored, not the address that is stored in it. you understand? I mean not 0x50505050, but 0x20304050

    I want to store it in an four-item array of bytes, like this

    Code:
    BYTE aArray[4] = { 0x00, 0x00, 0x00, 0x00 };
    so I want to asign to each item of the array like this

    Code:
    aArray[0] = ?; 
    aArray[1] = ?;
    aArray[2] = ?;
    aArray[3] = ?;
    At this moment I don't know how to do it, and I'm starting to think that there's no way for doing it "directly", unless you wanna go with using additional variables. Which I don't want to do.

    My idea is to get the 4-element array of bytes, filled up like this
    Code:
    // address of pAddress = 0x20304050
    
    aArray[0] = 20; 
    aArray[1] = 30;
    aArray[2] = 40;
    aArray[3] = 50;
    loading to it, the allocation address of the pointer pAddress. I don't want to store the contained address in the pointer (0x50505050).

    So I tried to do this:
    Code:
    PBYTE pAddress = 0x50505050;
    aArray[0] = (BYTE)pAddress; 
    aArray[1] = (BYTE)pAddress;
    aArray[2] = (BYTE)pAddress;
    aArray[3] = (BYTE)pAddress;
    which only works for the first element, but the rest of the 3 elements I can not assign correctly. I don't know exactly how to do this directly. I mean, not helping it by using other variables.
    Is there anyway of assigning the address 0x20304050 of pAddress, correcly and directly to the 4-item byte array aArray ??

    is left to say that I'm currently in a platform of 32 bits, and pointers are 4 bytes. That's why I am using a 4 elemented array of bytes.

    I really appreciate your help friends

  2. #2
    Join Date
    Nov 2018
    Posts
    121

    Re: copy the allocation address of pointer to a byte array ? NEED HELP!

    One of
    Code:
    memcpy(aArray,&pAddress,4); // where pAddress is
    memcpy(aArray,pAddress,4);  // what pAddress contains
    memcpy(aArray,*pAddress,4); // what's in what pAddress points to

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: copy the allocation address of pointer to a byte array ? NEED HELP!

    Why do you want to do this? How do you want the ordering of aArray - little endian or big endian?

    PS. I see from post #1 that you want big-endian ordering - the most significant byte first (aArray[0] = 0x20). memcpy (and using a union) gives little-endian ordering (aArray[0] = 0x50) as Intel uses little-endian.

    Consider:

    Code:
    void as_bigend(BYTE* dest, uint32_t num)
    {
    	for (size_t i = sizeof(uint32_t); i > 0; --i, num >>= 8)
    		dest[i - 1] = num & 0x00ff;
    }
    
    int main()
    {
    	BYTE aArray[sizeof(uint32_t)];
    	BYTE num = 12;
    	BYTE* anum = #
    	BYTE** aanum = &anum;
    
    	as_bigend(aArray, (uint32_t)aanum);
    	printf("0x%p\n%02x %02x %02x %02x\n", aanum, aArray[0], aArray[1], aArray[2], aArray[3]);
    
    	return 0;
    }
    which displays on my system:

    Code:
    0x0018FF2C
    00 18 ff 2c
    Last edited by 2kaud; March 28th, 2020 at 07:19 AM. Reason: PS
    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)

  4. #4
    Join Date
    Mar 2020
    Posts
    7

    Re: copy the allocation address of pointer to a byte array ? NEED HELP!

    hi mates thank you so much for your answers.

    This is exactly the program that I want, I mean the results that I want

    Code:
    #include <Windows.h>
    #include <stdio.h>
    
    DWORD dwAddress = (DWORD)0x10505090;
    BYTE bArray[4] = { 0x00, 0x00, 0x00, 0x00 };
    
    int main()
    {
    
    	printf("dwAddress 0x%X\n&dwAddress 0x%X", dwAddress, &dwAddress);
    	putchar('\n');
    
    	PBYTE ptrAddress = (PBYTE)&dwAddress;
    
    	printf("ptrAddress 0x%X\n&ptrAddress 0x%X", (PBYTE)ptrAddress, (PBYTE)&ptrAddress);
    	putchar('\n');
    
    	
    	PBYTE pA = (PBYTE)(ptrAddress);
    	memcpy(bArray,&pA,4);// thanks for your solution ;)
    
    	printf("bArray: 0x%X\n*bArray: 0x%X(0x%X)", (PBYTE)bArray, *(DWORD*)bArray, **(DWORD**)bArray);
    	putchar('\n');
    
    //	while(getchar() != '\n')
    	while(1)
    	{
    		if(getchar() != '\n')
    		{
    		}
    		else
    		{
    			break;
    		}
    	}
    
    	return 0;
    }
    as you see you have a global variable dwAddress holding this value 0x10505090.
    then you create a pointer that holds the address of dwAddress, the allocation address not the address that it contains (0x10505090).
    then I want to just copy the address of dwAddress to a 4 bytes array, and it is bArray[4]
    I want to do this using that pointer ptrAddress, but also it can be done directly using &dwAddress.

    then I just used this code that you just gave me
    Code:
    PBYTE pA = (PBYTE)(ptrAddress);
    memcpy(bArray,&pA,4);// thanks for your solution ;)
    now I want to do that code in other way more specific, like this

    Code:
    bArray[0] = *(BYTE*)&ptrAddress;
    bArray[1] = *(BYTE*)((&ptrAddress)+1);
    bArray[2] = *(BYTE*)((&ptrAddress)+2);
    bArray[3] = *(BYTE*)((&ptrAddress)+3);
    but that is wrong, I don't know how to do it. can you tell me how to do that directly in c / c++ , not using other functions or more variables, just in this way, just pure assignation:
    Code:
    bArray[0] = ?;
    bArray[1] = ?;
    bArray[2] = ?;
    bArray[3] = ?;
    thanks

  5. #5
    Join Date
    Nov 2018
    Posts
    121

    Re: copy the allocation address of pointer to a byte array ? NEED HELP!

    > bArray[1] = *(BYTE*)((&ptrAddress)+1);
    You have to understand how pointer arithmetic works.

    > I have an intermediate level at the c++ language so I can come up with some questions..
    Do you?

    You need to cast &ptrAddress to a BYTE* before you start adding offsets, not afterwards.

    &ptrAddress+1 is the next address, not the next byte in the current address.

  6. #6
    Join Date
    Mar 2020
    Posts
    7

    Re: copy the allocation address of pointer to a byte array ? NEED HELP!

    all time I used pointers it was just very basic, using char* int* now I am exploring with this kind of stuff related to memory addresses of some elements in arrays or structures, so I am training a lot.
    It seems you understand this well,
    the code now works
    Code:
    bArray[0] = *(BYTE*)&ptrAddress;
    bArray[1] = *(BYTE*)(((BYTE*)&ptrAddress)+1);
    bArray[2] = *(BYTE*)(((BYTE*)&ptrAddress)+2);
    bArray[3] = *(BYTE*)(((BYTE*)&ptrAddress)+3);
    the complete program
    Code:
    #include <Windows.h>
    #include <stdio.h>
    
    DWORD dwAddress = (DWORD)0x10505090;
    BYTE bArray[4] = { 0x00, 0x00, 0x00, 0x00 };
    
    int main()
    {
    
    	printf("dwAddress 0x%X\n&dwAddress 0x%X", dwAddress, &dwAddress);
    	putchar('\n');
    
    	PBYTE ptrAddress = (PBYTE)&dwAddress;
    
    	printf("ptrAddress 0x%X\n&ptrAddress 0x%X", (PBYTE)ptrAddress, (PBYTE)&ptrAddress);
    	putchar('\n');
    
    	
    	bArray[0] = *(BYTE*)&ptrAddress;
            bArray[1] = *(BYTE*)(((BYTE*)&ptrAddress)+1);
            bArray[2] = *(BYTE*)(((BYTE*)&ptrAddress)+2);
            bArray[3] = *(BYTE*)(((BYTE*)&ptrAddress)+3);
    
    	printf("bArray: 0x%X\n*bArray: 0x%X(0x%X)", (PBYTE)bArray, *(DWORD*)bArray, **(DWORD**)bArray);
    	putchar('\n');
    
    //	while(getchar() != '\n')
    	while(1)
    	{
    		if(getchar() != '\n')
    		{
    		}
    		else
    		{
    			break;
    		}
    	}
    
    	return 0;
    }
    thanks a lot

  7. #7
    Join Date
    Mar 2020
    Posts
    7

    Re: copy the allocation address of pointer to a byte array ? NEED HELP!

    Quote Originally Posted by 2kaud View Post
    Why do you want to do this? How do you want the ordering of aArray - little endian or big endian?

    PS. I see from post #1 that you want big-endian ordering - the most significant byte first (aArray[0] = 0x20). memcpy (and using a union) gives little-endian ordering (aArray[0] = 0x50) as Intel uses little-endian.

    Consider:

    Code:
    void as_bigend(BYTE* dest, uint32_t num)
    {
    	for (size_t i = sizeof(uint32_t); i > 0; --i, num >>= 8)
    		dest[i - 1] = num & 0x00ff;
    }
    
    int main()
    {
    	BYTE aArray[sizeof(uint32_t)];
    	BYTE num = 12;
    	BYTE* anum = #
    	BYTE** aanum = &anum;
    
    	as_bigend(aArray, (uint32_t)aanum);
    	printf("0x%p\n%02x %02x %02x %02x\n", aanum, aArray[0], aArray[1], aArray[2], aArray[3]);
    
    	return 0;
    }
    which displays on my system:

    Code:
    0x0018FF2C
    00 18 ff 2c
    Hi, I don't want to be disrespectful with you so I come with an answer for you.

    I just wanted to copy the address (4b for me) of some variable into a 4-byte array, and you asked me in what order do I need this address to be copied?

    when I look at the memory dump, the memory allocation address of the pointer and the address that it contains are shown like this

    given a pointer..
    PHP Code:
    PBYTE pPtr 0x50607080;// it holds address of other variable 
    addressing (pPtr) | dump (0x50607080)
    0x10203040 | 80 70 60 50

    So the answer is that I need it to be copied to the array the way it allows me to treat that array as a pointer, I mean as a working address.

    printf("bArray: 0x%X\n*bArray: 0x%X(0x%X)", (PBYTE)bArray, *(DWORD*)bArray, **(DWORD**)bArray);
    putchar('\n');
    so this way seems to be the correct
    Code:
    bArray[0] = *(BYTE*)&ptrAddress;
    bArray[1] = *(BYTE*)(((BYTE*)&ptrAddress)+1);
    bArray[2] = *(BYTE*)(((BYTE*)&ptrAddress)+2);
    bArray[3] = *(BYTE*)(((BYTE*)&ptrAddress)+3);
    otherway I should have done it in inverse order
    Code:
    bArray[3] = *(BYTE*)&ptrAddress;
    bArray[2] = *(BYTE*)(((BYTE*)&ptrAddress)+1);
    bArray[1] = *(BYTE*)(((BYTE*)&ptrAddress)+2);
    bArray[0] = *(BYTE*)(((BYTE*)&ptrAddress)+3);
    am I right?
    then I see you used bitwise operations so unless you wanna comment what's going on, I not consider to use them because I don't understand to use them
    Last edited by SwagDuro; March 30th, 2020 at 02:20 PM.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: [RESOLVED] copy the allocation address of pointer to a byte array ? NEED HELP!

    There are 2 ways of storing integers in memory known as little-endian or big-endian. Consider the number 0x20304050

    Assume this number is stored starting at location 0x1000

    For little-endian (as used by Intel), the data is stored as

    0x1000 0x50
    0x1001 0x40
    0x1002 0x30
    0x1003 0x20

    ie the lower memory address stores the least significant byte and the highest memory address stores the most significant byte.

    For big-endian (as per your example in post #1). the data is stored as

    0x1000 0x20
    0x1001 0x30
    0x1002 0x40
    0x1003 0x50

    ie the lower memory address stores the most significant byte and the highest memory address stores the least significant byte.

    So if you declare an array BYTE anArray[4], then the address of anArray[0] is the lowest memory address and the address of anArray[3] is the highest memory address. Depending upon whether you want anArray[0] to contain the 0x20 or the 0x50 depends upon whether you want anArray to use big or little endian coding.

    Yes, the as_bigend() function uses bit operations on the passed unsigned integer num. As this is for big-endian, the 0x50 goes into dest[3] and 0x20 goes into dest[0]. For any integer, anding (&) the number with 0xff will return the least significant byte of that number. Given 0x20304050 it will give 0x50. This is put into dest[3]. num is then right shifted by 8 bits. ie the number is divided by 256 (2 to the power 8). As each byte is 8 bits, this then gives num as 0x00203040. The least significant byte (0x40 now) is then put into dest[2]. This operation is repeated 4 times so that given num as 0x20304050, then dest[0] = 0x20, dest[1] = 0x30, dest[2] = 0x40 and dest[3] = 0x50 - the required values for big-endian representation.

    Hope this explanation helps.
    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)

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