简体   繁体   中英

is it possible to declare a [pointer] variable with specific address?

could we memory allocation to specific legal address (eg: 0x7fff12345678) without any variable declaration or could we declare pointer variable and allocate memory but with specific legal address ?

Edit 2 :

I found the answer:

*reinterpret_cast<int**>( 0x7fff12345678)=new int[10];

if 0x7fff12345678 is Allowed memory address, ofcorse.

当您使用malloc ,会在堆上分配一块内存(如果malloc成功),并使指针指向已分配内存的开头(假设您使用pointer = malloc(...)而不是简单地malloc(...) )。

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 (ie 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.

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 (eg: 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.

my answer is:

a) the memory assigned to address, in the other word,for memory allocation int[10] to specific legal address (eg: 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.

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

  1. First, p is defined and initialized to NULL .
  2. malloc() is called, and considering success case, the returned memory address is assigned to p . Thus, now, p points to the allocated memory.
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM