Q- How are variables accessed within a frame in the stack? Ans: Well as there is a pointer known as stack pointer to manage different operations on stack, the same way there is a frame pointer to access different variables with a frame. The stack pointer points to the top of the stack while the […]
C language
Q5: Memory allocation
As address of the variable y is returned and which gets stored in x and the memory block of function abc() is de-allocated. Next we try to store a value in the memory which has been de-allocated. As memory has been de-allocated and hence operating system may have allocated this memory to some other process. […]
Q4: Memory Allocation
What is the output of the following code? (a) 3 (b) 5 (c) Can’t say Ans: Many would answer it as 3 but it is wrong. The way we have tried to do it is wrong. As we enter into main we have a memory allocated for variable x and then function abc is called. Now […]
Q3: Memory allocation
Consider the C code Explain the allocation of memory in stack for the above program. Ans: firstly a frame in stack is created and memory is allocated to store variable x with value 3. And then function call is made to function fun() and a new frame is created in the stack. Firstly return address to […]
Q2: Memory Allocation
Consider the C code: Now we have to picturize the whole scenario in HEAP and STACK memory. Ans: We have no dynamically allocated variables in this program. Hence there is not much role of HEAP memory. We analyze the memory allocation and de-allocation in STACK on next page. We know stack memory is used to […]
Q1: Memory allocation
Now we have the following statements and we see how STACK & HEAP memory is allocated to different variables. We have the empty HEAP memory & STACK memory as: NOTE: For this question we have used a heap memory & allocated space serially while in real, memory is allocated randomly by operating system. char* x; x= […]
Functions to manage heap memory
The functions to manage heap memory which are included in the directory #include <stdlib.h> are: malloc(): void * malloc (size_t size); As we see from the prototype of the malloc function that we have to pass the number of bytes required as parameter to the malloc function and the function returns the starting address of the […]
Program Memory – Introduction
This article is very important for the person who wants to understand c language. Anyone who is going for any interview for a good software company must read the following article as the questions asked in the tests and interviews need the candidate to be clear in the following concepts. This article would ensure that […]
NULL pointer
It is the simplest pointer. The NULL pointer is a pointer which points to no where.. We can also use integer 0 in place of constant NULL. Int* x=NULL; The above statement creates a NULL pointer x. DEREFERENCING NULL As we know NULL pointer is actually a pointer which points to nowhere and hence we […]
Pointer arithmetic
Addition of a number to pointer: Pointer+ number When we add some integer to the pointer then the address stored in the pointer is incremented by number * sizeof (datatype of pointer) Assume space required for data types int & char are 2 & 1 bytesresp. And we represent a particular block of memory filled […]