0

I want to know the highest address of a RAM. Say I have RAM 4GB (in a 32 bits computer) and 16GB (in a 64-bits computer), but it also shared with display, which is mean that not all the RAM used for application. I want to know what is the address of part of the RAM that it still unoccupied, and what is the data type inside of a particular memory address? Seems like many question in here. This is clearly about pointer and memory allocation. But actually is what I want to know is:

What is the command to know in C/C++? Say I want to access the content of a memory address '4294967295d' or 'FFFF FFFFh', or any other address.

My compiler is gcc both under Windows and under Linux.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • Is this code running on an operating system? Most OSes provide virtual memory, which contradicts accessing memory via its true address. – chris Jun 04 '20 at 02:33
  • 1
    You have a misconception of how memory addressing works with modern operating systems. See [Virtual memory](https://en.wikipedia.org/wiki/Virtual_memory). Anyway, **why** do you want to do this? What actual problem do you believe this will help you solve? – Igor Tandetnik Jun 04 '20 at 02:33
  • Are you writing a kernel-mode program? You should not normally have access to any memory location you choose, and in general the operating system is free to remap pointers in any way it chooses. – paddy Jun 04 '20 at 02:33
  • Hidden in this question is the other question: _"what is the data type inside of a particular memory address?"_ -- there is no data type associated with raw memory (although we commonly treat it as consecutive bytes). Knowledge of the actual data type being stored at a location is contained within the program that stored the value there. – paddy Jun 04 '20 at 02:42
  • C++ doesn't require an operating system. So all the answers and comments that talk about virtual memory and such are talking about what you can and cannot do on Windows or Linux. But if you don't have an OS (or are using an OS like MSDOS or the OS on an Apple 2 or Commodore 64 that doesn't hide the memory) then you CAN access memory with a specific address. – Jerry Jeremiah Jun 04 '20 at 02:53
  • For MSDOS Have a look at this question: https://stackoverflow.com/questions/32972051/in-c-how-do-i-write-to-a-particular-memory-location-e-g-video-memory-b800-in – Jerry Jeremiah Jun 04 '20 at 02:57
  • @IgorTandetnik In all explanations I read and I watched, all just giving example. Something like this: int x=10; int *ptr=&x; and commented like this: "Say that the x is rest in address 100 occupied 4 byte as it is integer, and p rested in 108 pointing to 100." Then how can I actually verify to the more real condition? I.e: I want to know where is the x actually stored? If I don't know the actual address, so what is the pointer used for? This is become not clear. – AirCraft Lover Jun 04 '20 at 02:58
  • `x` is stored at address `&x`. If you really want to see the numerical value of that address, run `std::cout << &x;` It's unclear what you are going to do with this information, once obtained. – Igor Tandetnik Jun 04 '20 at 03:05
  • @IgorTandetnik, I know about it. It was just an example of a case. In the real coding, will be much more complex, especially when come to debugging. – AirCraft Lover Jun 04 '20 at 03:09
  • When the description says "assume x is stored in memory location 100" it is because it can be stored anywhere the OS wants it stored - every time the program is run on Linux or Windows it will be some new place. So in your program you can never assume you know where it is - that's why you use `&x` for the memory location. If you want to know where it is stored this time you can print &x to find out but it's not helpful because next time it won't be in the same place. – Jerry Jeremiah Jun 04 '20 at 03:09
  • @JerryJeremiah, there are four commands pertaining memory allocation: malloc(), calloc(), free() and realloc(). If we don't have full control the the memory address (say that we can not reserve for us), so how to use those command? – AirCraft Lover Jun 04 '20 at 03:12
  • On Windows and Linux the memory allocation functions ask the OS for a pointer to some memory and if the virtual memory already given to you isn't big enough the OS will create a new virtual memory page - like the answers below said: it isn't real physical memory – Jerry Jeremiah Jun 04 '20 at 03:16
  • You call, say, `malloc(some_size)`. It allocates a block of memory of the size you requested **somewhere** (you shouldn't care where), and returns a pointer to that memory. You then use that pointer to access (write to and read from) that memory. – Igor Tandetnik Jun 04 '20 at 03:18

2 Answers2

1

A "memory address" doesn't exist as you're thinking of it. On a modern machine this will be done with protected memory meaning the addresses aren't tied to a physical bank of memory per-se, but to pages that represent memory and are mapped to physical memory.

If your process tries to access memory that it has not been specifically allocated then it will trigger a page fault and get terminated. You're boxed in. This is different from older operating systems where you could just poke around in memory and see what was there, even modify it freely.

Technically the addresses in the mapping table can be anything the kernel wants them to be, as demonstrated with ASLR.

This has been the case pretty much since 32-bit machines and protected-mode operating systems took over in the late 1990s.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

There is no way in standard C++ to access arbitrary specific memory. You can create objects, and you can acquire memory for objects through std::malloc (there's hardly ever any need to use std::malloc though) or operator new, but you have no control over the memory address where allocations come from. Those are entirely controlled by the language implementation.

Syntactically, you may technically attempt to access arbitrary memory address simply by reinterpreting an integer value (representing some memory address). Whether one integer value represents the memory address that you expect is not guaranteed by the language and is entirely dependent on the language implementation. This is typically useful on embedded systems which don't use virtual memory.

On modern multi-tasking operating systems, such as those that you mention in the question, the memory that a process sees is not physical memory in the RAM, but virtual memory which the operating system maps onto the physical one. And attempting to access virtual memory that has not been mapped by the operating system, or has been mapped with restrictions, typically result in the operating system terminating the process in order to prevent the rogue process from corrupting data.

The operating system may provide an interface to access physical memory (or virtual memory of another process) if the accesser process has sufficient privileges. This may require special configuration of the operating system since such interfaces are typically considered a security risk.

Say I want to access the content of a memory address '4294967295d' or 'FFFF FFFFh'

When asking such question, you should first consider: Are you attempting to access the physical address, or the virtual address? Virtual address of this process or some other process? Why would this particular address contain something relevant? How do you know that? Is that knowledge reliable? Do you have permission to access that memory?


I want to know where is the x actually stored?

A variable is stored somewhere that the language implementation chose to store it. Knowing the number that represent the address is hardly ever useful.

Consider an analogy: Go to a sandy beach, lie down to the sand to see closer, and choose one grain of sand. Ask the question: What is the address of this grain of sand i.e. how many grains of sand are there to the left of this grain? There exist an answer sure... but what would you do with that knowledge? If you come to the beach the next day, the grains of sand have shifted and the address is no longer the same. Similarly, what would you do with the knowledge of knowing the numeric representation of memory address where some object is stored?

That said, it is actually possible. Simply insert the pointer into a character stream such as std::cout, and the address will be printed in textual representation. Unless it is a pointer to character in which case it is treated differently.

eerorika
  • 232,697
  • 12
  • 197
  • 326