How does the memory process begin and where does it end?

On a Windows platform, I am trying to flush memory from my application where the variables are. Here's the function:


void MyDump(const void *m, unsigned int n)
{
        const unsigned char *p = reinterpret_cast<const unsigned char *>(m);
        char buffer[16];
        unsigned int mod = 0;

        for (unsigned int i = 0; i < n; ++i, ++mod) {
                if (mod % 16 == 0) {
                        mod = 0;

                        std::cout << " | ";

                        for (unsigned short j = 0; j < 16; ++j) {
                                switch (buffer[j]) {
                                        case 0xa:
                                        case 0xb:
                                        case 0xd:
                                        case 0xe:
                                        case 0xf:
                                                std::cout << " ";

                                                break;

                                        default: std::cout << buffer[j];
                                }
                        }

                        std::cout << "\n0x" << std::setfill('0') << std::setw(8) << std::hex << (long)i << " | ";
                 }

                buffer[i % 16] = p[i];

                std::cout << std::setw(2) << std::hex << static_cast<unsigned int>(p[i]) << " ";

                if (i % 4 == 0 && i != 1)
                        std::cout << " ";
        }
}

Now, how can I find out from what address my process memory space starts, where are all the variables stored? And how am I now, how long is the area?

For instance:


MyDump(0x0000 /* <-- Starts from here? */, 0x1000 /* <-- This much? */);

Regards,
nhaa123

+5
source share
6 answers

Overview

What you are trying to do is absolutely possible, and there are even tools that will help you, but you will have to do more work than I think you expect.

" ". System heap API Windows . , , API , .

, , , , . , , . , seg- , segfault, .

. :

  • ( ),
  • ( ),
  • (, )
  • ( malloc new).

, . , . , (1) (2, 3 4), , .

...

, 0 2 ^ 64 (, , ), . - , , , ; . , .

: Windows , , - .

Windows API. . , . C . .

Linux 1 3 , /proc/pid/maps. /proc/pid/maps , . ; , , , .

Windows , Windows . , .

+2

: . , , , , . , ( ).

, , . , , , : C?

, , ( , ). - , "" ( , "" ).

, , , . "" , , , . , , .

, , . , , .

+7

, , ... , . - :

void* ptr_to_start_of_stack = 0;
int main(int argc, char* argv[])
{
    int item_at_approximately_start_of_stack;
    ptr_to_start_of_stack = &item_at_approximately_start_of_stack;
    // ... 
    // ... do lots of computation
    // ... a  function called here can do something similar, and
    // ... attempt to print out from ptr_to_start_of_stack to its own
    // ... approximate start of stack
    // ... 
    return 0;
}

sbrk() ( sbrk(0)), ( , , , ).

, . , , , , , . Log4Cxx.

, , GDB, . , , , , . , , .

+2

AFAIK, , , . .

+1

, . , , . , Windows VirtualQueryEx(). , , , - .

.

+1

, , . .

CP/M MS-DOS.

, , , .

- , , , , , , , ..

, , , . , . , . , , , . (, . , , .)

3 : , . OS , , , .

, .

.

.

"" . , "startHere()", , "endHere()". , "startHere" "endHere".

. , .

0

All Articles