does memory assign to the pointer's name or pointer's address? could we memory allocation to specific legal address (e.g: 0x7fff12345678) without any variable declaration or could we declare pointer variable and allocate memory but with specific legal address ?
to declare a pointer variable, does memory assign to the pointer's name or pointer's address?
235 views Asked by MD128 AtThere are 6 answers
Let me us your example
main() { // This allocates memory for A,B (usually on the stack)
char *A,*B;
// At this point A and B have totally undefined values. So making this assignment puts whatever unknown values was located at B in A.
A=B; //A and B have same address(point to-location) but different name,
// This allocates 10 bytes and stores the location of those bytes in A.
A=new char[10]; //is the allocation, matter of the name (A as string) or the location (A as address)
// This allocates 20 bytes and stores the location of those bytes in B.
B=new char[20];
// This is totally unpredictable and likely to screw up something will appear down the road.
A[18]='C';//is this really OK or we have potentially hidden error?
}
To your questions:
When we have memory allocation or memory deallocation in Code, memory is assigned to what?
1- A pointer's name (or equally the any name addressing related to compiler)
or
2- A pointer's address (what we can usually dereference it and get memory content) ?
In C, the memory address is stored within the memory allocated for the pointer.
in other word: could we memory allocation to specific legal address (e.g: 0x7fff12345678) without any variable declaration?
To get a good understanding of what goes on, you really need to learn assembly language. Memory can be allocated without any memory location and stored in registers.
To answer your query, let's have a look at a piece of code, shall we? That way, IMHO, it will be easier to explain the scenario.
int * p = NULL; //define a pointer, set it to NULL
p = malloc(sizeof*p); //allocation, assume success
....
free(p); //de-allocation
Here, as you can see, p
is a pointer to int
. In the code
- First,
p
is defined and initialized toNULL
. malloc()
is called, and considering success case, the returned memory address is assigned top
. Thus, now,p
points to the allocated memory.- Lastly once
free()
is called, the allocated memory block is deallocated (cannot be used any more).
So, to answer your question:
memory is assigned to a variable -what identified by name- or assigned to an memory address -what identified by number?
The memory address, (which is a number itself) is assigned to a variable, in general. In other words, the memory is allocated to the process which is making the call to malloc()
, in the form of a memory address.
FWIW, if you do not collect the returned pointer from malloc()
, there is no way you can access the (if) allocated memory. You need to collect the return value of malloc()
in some variable.
It's an address. And a pointer points to (or better stores) that address (which is the beginning address of the allocated memory).
By dereferencing the pointer you set/get the value(s) stored in the memory (beginning at that address).
Your question has immense meaning and dose. You can't program in C if you don't understand this vital concept.
Memory is not assigned at all. The address of the memory is assigned.
my answer is:
a) the memory assigned to address, in the other word,for memory allocation int[10] to specific legal address (e.g: 0x7fff12345678) without any int variable declaration, :we can write
*reinterpret_cast<int**>( 0x7fff12345678)=new int[10];
b) in the example, in this line:
A[18]='C';
we have potentially memory error, because assignment "A=B" ,before memory allocation of B, don't any change on A.
Computers don't deal with variable names. A variable name, whatever that is, is only used to help you reference memory when writing a program and by the compiler to understand how you want your program to work.
Processors don't deal with names or strings. When you allocate something, you're usually returned a pointer which points to the memory address. The name of the pointer is irrelevant; the address and how much memory you allocated: that's what counts. The operating system decides whether to satisfy your memory allocation request. Unless your program is the only one running on a machine (i.e. you're writing an OS yourself), you can't just assume that an address is empty and write anything you want there through a pointer (that will end in tears). And even in that case you would have address restrictions due to hardware mapping.
After you compile your program (release mode), pointer names and variable names are lost and the program just deals with memory addresses.
Nb. when you're suggested to use good variable names.. it doesn't mean you have to in order to make the processor happy. You should only because your code might have to be read or maintained by someone else. Meaningful variable names are necessary for human reading.