Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query about memory location

Suppose there is a variable a and a pointer p which points to address of a.

int a;
int *p=&a;

Now since I have a pointer pointing to the location of the variable, I know the exact memory location (or the chunk of memory).

My questions are:

  1. Given an address, can we find which variable is using them? (I don't think this is possible).
  2. Given an address, can we atleast find how big is the chunk of memory to which that memory address belongs. (I know this is stupid but still).
like image 224
g4ur4v Avatar asked Nov 06 '12 10:11

g4ur4v


4 Answers

  1. You can enumerate all your (suspect) variables and check if they point to the same location as your pointer (e.g. you can compare pointers for equality)
  2. If your pointer is defined as int *p, you can assume it points to an integer. Your assumption can be proven wrong, of course, if for example the pointer value is null or you meddled with the value of the pointer.
like image 149
r0b0 Avatar answered Oct 21 '22 01:10

r0b0


You can think of memory as a big array of bytes:

now if you have a pointer to somewhere in middle of array, can you tell me how many other pointers point to same location as your pointer?? Or can you tell me how much information I stored in memory location that you point to it?? Or can you at least tell me what kind of object stored at location of your pointer?? Answer to all of this question is really impossible and the question look strange. Some languages add extra information to their memory management routines that they can track such information at a later time but in C++ we have the minimum overhead, so your answer is no it is not possible.

For your first question you may handle it using smart pointers, for example shared_ptr use a reference counter to know how many shared_ptr are pointing to a memory location and be able to control life time of the object(but current design of shared_ptr do not allow you to read that counter).

There is non-standard platform dependent solution to query size of dynamically allocated memory(for example _msize on Windows and memory_size on Unix) but that only work with dynamic memories that allocated using malloc and is not portable, in C++ the idea is you should care for this, if you need this feature implement a solution for it and if you don't need it, then you never pay extra cost of it

like image 26
BigBoss Avatar answered Oct 21 '22 01:10

BigBoss


Given an address ,can we find which variable is using them ?

No, this isn't possible. variables point to memory, not the other way around. There isn't some way to get to variable-names from compiled code, except maybe via the symbol table, reading which in-turn would probably need messing around with assembly.


Given an address ,can we atleast find how big is the chunk of memory to which that memory address belongs..

No. There isn't a way to do that given just the address. You could find the sizeof() after dereferencing the address but not from the address itself.

like image 2
Anirudh Ramanathan Avatar answered Oct 21 '22 02:10

Anirudh Ramanathan


Question 1.
A: It cannot be done natively, but could be done by Valgrind memcheck tool. The VM tracks down all variables and allocated memory space/stack. However, it is not designed to answer such question, but with some modification, memcheck tool could answer this question. For example, it can correlate invalid memory access or memory leakage address to variables in the source code. So, given a valid and known memory address, it must be able to find the corresponding variable.

Question 2.
A: It can be done like above, but it can also be done natively with some PRELOADED libraries for malloc, calloc, strdup, free, etc. By manual instructed memory allocation functions, you can save allocated address and size. And also save the return address by __builtin_return_address() or backtrace() to know where the memory chunk is being allocated. You have to save all allocated address and size to a tree. Then you should be able to query the address belongs to which chunk and the chunk size, and what function allocated the chunk.

like image 1
jclin Avatar answered Oct 21 '22 01:10

jclin