2,114 views
1 1 vote
Consider storage class of a variable declared as static. Where the memory will be allocated?

(A) Code Segment

(B) Stack Segment

(C) Heap Segment

(D) Data Segment

2 Answers

Best answer
2 2 votes


Code segment or text segment: Code segment contains the code executable or code binary.
• Data segment: Data segment is sub divided into two parts
– Initialized data segment: All the global, static and constant data are stored in the data segment.
– Uninitialized data segment: All the uninitialized data are stored in BSS.
Heap: When program allocate memory at runtime using calloc and malloc function, then memory gets
allocated in heap. when some more memory need to be allocated using calloc and malloc function,
heap grows upward as shown in above diagram.
• Stack: Stack is used to store your local variables and is used for passing arguments to the functions
along with the return address of the instruction which is to be executed after the function call is over.
When a new stack frame needs to be added (as a result of a newly called function), the stack grows
downward.

• selected by
3 3 votes

All variables that are defined inside a function are normally created on the stack each time the function is called. These variables die as soon as control goes back from the function. However if the variables inside the function are defined as static then they do not get created on the stack. Instead they are created in a place in memory called "Data Segment". Such variables die only when progran execution comes to an end.

Position:
Show:

Related questions

6 6 votes
3 3 answers
275
275 views
GO Classes asked Jun 16
275 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int step = 1; if (n <= 1) return n; step++; return n + fun(n - step); } int main() { pr...
7 7 votes
3 3 answers
322
322 views
GO Classes asked Jun 15
322 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int x = 0; int y; if (n == 0) return 0; x++; y = fun(n - 1); return y + x; } int main()...
6 6 votes
2 2 answers
274
274 views
GO Classes asked Jun 13
274 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int count = 0; if (n == 0) return count; count = count + n; return fun(n - 1); } int ma...
5 5 votes
3 3 answers
241
241 views
GO Classes asked Jun 11
241 views
What is the output of the following code?#include <stdio.h int add(int n) { static int total = 1; total = total + n; return total; } int main() { int i, ans = 0; for (i =...