Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

allocation of memory for pointer

Tags:

c

pointers

i was debugging a program in two terminals by giving different input,but at on particular i saw this in one terminal

ins (ptr=0x0, key=1, upKey=0xbffff308, newnode=0xbffff30c)

and in another terminal

ins (ptr=0x0, key=1, upKey=0xbffff308, newnode=0xbffff30c)

where ins function is

ins(struct node *ptr, int key, int *upKey,struct node **newnode)

how can the same memory location be allocated to a pointer. and i am running the same program on two different terminals...with different inputs

like image 741
Manu Avatar asked Feb 02 '11 05:02

Manu


People also ask

How is memory allocated to a pointer type variable?

The basic memory allocation function is malloc(). Its function prototype is: (void *) malloc(size_t numbytes); What this means is that it takes a single argument that is the number of bytes you want to allocate (size_t is usually the same as unsigned int), and it returns a pointer to that new memory space.

Where are pointers allocated in memory?

Pointer Arithmetic and Array Indicies As declared and initialized to a memory space, pointers point to the base, the first element, of that space.

How do you allocate a pointer?

delete and free() in C++ In C++, the delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. It is an operator.

Is memory allocated for referenced pointers?

Memory is allocated when a pointer is defined. A reference however, is a name alias & hence no memory is allocated for it( Is it correct? ). 2. Reference is bound to be initialized at the time of definition because, a reference is implemented with a constant pointer & hence cannot be made to point to the other object.


2 Answers

The memory addresses you are looking at are virtual addresses. Those addresses are then translated by the processor to physical addresses. This is the basis of all modern operating systems. Each process thinks it owns the entire address space (4GB in the case of a 32 bit machine, a lot more in the case of a 64bit machine). When a process accesses memory that has yet to be allocated to it a page fault is generated by the CPU. The OS can then handle that invalid memory access in one of several ways; one common way is a segmentation fault.

like image 117
Alex W Avatar answered Oct 23 '22 10:10

Alex W


With virtual memory, every program running on a system acts as though it has the computer's entire address space to itself. However, every time a pointer is dereferenced, a special piece of hardware translates from the pointer's purported address (its virtual address) to some other location in memory where the data actually lives (the physical address). The operating system is built to manage and move the regions of memory to which virtual addresses are mapped, so if one program dereferences some address A it will map to a different location in physical memory than you would get if you dereferenced address A in a different process. In fact, any number of programs can all claim to use address A without hassle, since these virtual addresses all resolve to different physical addresses on the system.

like image 38
templatetypedef Avatar answered Oct 23 '22 10:10

templatetypedef