0

Would the following work?

Initialize a pointer (to any type of object) to 0.

int* thisPtr = 0;

Perform pointer arithmetic by incrementing the pointer until it has reached the last memory location. We'll know it's the last memory location because adding 1 to the pointer will not do anything. Keep track of how many memory locations we've visited.

int count = 1;
 while (thisPtr + 1 > thisPtr) { 
    ++thisPtr;
    ++count;
}

Now count is equal to the number of memory locations we were able to visit. Multiply it by the number of bytes in a pointer.

int bytesInMemory = count * sizeof(int*);

Does that work??? If not, why, and what is the correct way?

user3178285
  • 151
  • 2
  • 7
  • 6
    You have many wrong assumptions here. – Maroun Feb 03 '14 at 06:33
  • You should read http://stackoverflow.com/questions/2513505/how-to-get-available-memory-c-g then try a differet implementation – Sully Feb 03 '14 at 06:37
  • it's kind of a contrived and somewhat perverted approach... did you have that idea or have you seen that somewhere?? – Marco A. Feb 03 '14 at 09:36
  • One, physical memory addresses are non-continuous. Two, not all memory is available for an application to use. (The operating system and other applications need space as well.) Three, all modern operating systems use *virtualization*, i.e. what the application "sees" is virtual, not the actual physical memory. If an application demands more memory than is physically available, the system will still serve the request, and start "swapping" memory to hard drive as necessary. Four, you are accessing memory that your application has not allocated first, which will crash your app. – DevSolar Feb 03 '14 at 09:38
  • http://wiki.osdev.org/Memory_management – DevSolar Feb 03 '14 at 09:40

5 Answers5

2

No it doesn't.

In theory, what it does is undefined. In practice, it will always return 4GB for a 32-bit program, and take a ridiculous amount of time (as in: longer than the age of the universe) for a 64-bit program.

Pointers are just numbers. If you replace int* thisPtr with unsigned int thisPtr, replace thisPtr++ with thisPtr += 4 and replace thisPtr + 1 with thisPtr + 4, you will get an equivalent program (on 32-bit systems). All it does is find the maximum possible value of an integer.

C++ does not have a way to find out how much memory your system has. The operating system will have a way to do it, but there isn't a standard C++ way that works on any operating system.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

On modern computers memory has become a complex topic, though mostly you don't need to know very much.

To get the size of installed physical RAM, on Windows you do this:

    uint64_t nBytes=0;
    bool bOK = GetPhysicallyInstalledSystemMemory(nBytes);

See http://msdn.microsoft.com/en-us/library/windows/desktop/cc300158.aspx

On Linux, you might try:

    uint64_t nBytes = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);

See http://linux.die.net/man/3/sysconf

Of more interest is the amount of Virtual Memory (or swap space) which is configurable, up to the spare space on your HDD.

Also interesting is the Working Set Size. This is the amount of RAM (not swap) that is currently being used by your program. It changes constantly as the OS swaps pages in and out.

The question to ask is "Why do you want to know?". What are you trying to achieve?

Michael J
  • 7,631
  • 2
  • 24
  • 30
0

That does not work because you could potentially increment the pointer outside of the system memory. There is no inherent restriction on that. Besides, memory models are more complicated than just long blocks of accessible addresses when you factor in paging, etc..

There is no one shot method to get the size of the memory on a system.

hsun324
  • 549
  • 3
  • 9
0

So first of all 0 does not mean , memory location at 0 position(basically it means null) , and there is difference between logical and physical address .We cann't access that way in user space program, however kernel may do that . So in user program we cannot access any memory location , that would result in trap . Page table maintained by operating system maps logical and physical address .So access address outside scope results in interrupt generation .

io10
  • 45
  • 8
0

I dont know what Memory are you talking about .
But there is an easy way to find out the HDD memory size ..
If developing on Windows OS ,Use WMI , It is particulary designed to obtain system data in well Modelled fashion .

spetzz
  • 679
  • 8
  • 19