Question

In the following C program, indicate the variable type (global, local, or static local), the lifetime (when the program is run or when the function is called), and the memory allocation (from stack or from heap) for each variable (x, y, z, and w) defined in the program int w void fun (int x) int y; static float z; void main fun(w);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int w: w is the global variable.

x,y are the local variables or automatic variables .

z is the static local variables

the lifetime and scope of above variables are

The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program.

static storage class provides a lifetime over the entire program, however; it provides a way to limit the scope of such variables, and static storage class is declared with the keyword static as the class specifier when the variable is defined.

the scope of global variables will sustain throughout the program.

Memory Layout of C Programs A typical memory representation of C program consists of following sections. 1. Text segment 2. I

the memory layout of c is

Add a comment
Know the answer?
Add Answer to:
In the following C program, indicate the variable type (global, local, or static local), the lifetime...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • problem 2 and 3 1. For each variable (a-g) describe whether the variable itself is global...

    problem 2 and 3 1. For each variable (a-g) describe whether the variable itself is global or local, static or not static, and initialized or uninitialized. (Hints: Global variables are always static. Local variables become static when declared with 'static'. For pointers, consider the pointer itself, not anything the pointer might point to.) 2. For each variable (a-g), describe where it is located when DoFunctionB is running. That is, for each variable, describe whether it is on the stack, in...

  • Run the C program below and complete the table showing all the variables. Add printf function...

    Run the C program below and complete the table showing all the variables. Add printf function calls to obtain the addresses and values of all 13 variables. (Count the array (ca) as 3 variable/values and also include argc and argv in the table). The table must show the Address, Name, Datatype, Scope, and Value of each variable on the stack. (Hint: use the sizeof function to double check your addresses.) Explain how different the actual memory allocations are from what...

  • Help fast :Assume a variable called orange exists of type Fruit pointer. The Fruit structure is...

    Help fast :Assume a variable called orange exists of type Fruit pointer. The Fruit structure is defined below. For the variable orange, write a statement that assigns a member variable named hasSeeds to 1 typedef struct Fruit struct f a. &(orange.hasSeeds) 1; b. orange.("hasSeeds) 1 c. orange->hasSeeds 1; d. orange.hasSeeds 1; // menber variables Fruit; Q8: When the malloc function is called, the malloc function returns a(n) a. address on the heap b. address on the stack c. address on...

  • Options for both are: - .rodata - .bss - .data - heap - stack Consider the...

    Options for both are: - .rodata - .bss - .data - heap - stack Consider the following code snippet: int arrt 8 1 int* p - arr; int val - void main(O int x; static int y; printf("end of main\n") The specific memory areas variable arr and variable p will be stored in are Select] andSelect respectively

  • 16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int...

    16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int main) int x-1,y 0,z-2; x-fun2(y,z); cout sx fun 1 (z); cout (#xtytz(endl; y-fun2(x,x); cout <exty+zscendl; system("pause"); void fun 1 (int a) int fun2(int a, int b) int static c2; return atx; 16 Points) Question 3 Write down the outputs of the following program into the provided table...

  • a. What is the referencing environment at location 1 in the program below? Use static scope...

    a. What is the referencing environment at location 1 in the program below? Use static scope rules. Include what functions the variables are declared in. void g ( int w) { var b = 1; var x = 8;         void f()         { varb = 3;                       x = b + 1; print “f: x = “ , x ;                        ….             < - - - - - - - - - - //Used in Prob a //location 1...

  • 5. (10%) Consider the following C code segment: int fun (int a, int b) { static...

    5. (10%) Consider the following C code segment: int fun (int a, int b) { static int count; int x, *p; x= a +b; p-malloc (sizeof (int)); a) What is the lifetime of the variable count? b) What is the scope of the variable count? c) Which part of memory is the storage allocated for x? d) What is the lifetime of the variable x? e) Which part of memory is the storage allocated for the integer pointed to by...

  • please answer all 5 questions, thanks. 1. What are the two memory pools for programs running...

    please answer all 5 questions, thanks. 1. What are the two memory pools for programs running a modern operating system? 2. Given the following program: #includec iostream» using namespace std; void func1(int); int main( int a 5, b 10; func1(a): return e; void func1 (int a) int z[2]; int y a. What is on the stack before the first function call (assume a memory starting address of 0x1018)? b. What is on the stack after the function call is executed...

  • IN JAVA: List or draw out the memory contents for BOTH of the slides below. (you do not need to s...

    IN JAVA: List or draw out the memory contents for BOTH of the slides below. (you do not need to show addresses)Clearly list out what memory is stored in each region under the bolded (Stack, Heap, or Globals). Assume the garbage collector has not yet run. Multiple Objects Stack //Multiple dynamic objects class objx f int i void print ) f system.out.println ("i-" + i)i Heap / same as before public class DynamicExample3 f public static void main (Stringl] argv)...

  • Write a C program for a program to implement a recursive main. Include a static local...

    Write a C program for a program to implement a recursive main. Include a static local variable count initialized to 1. Post increment and print the value of count each time the main is called. The loopcount included in the usage is a positive integer number that will indicate how deep the recursion should go. Usage: lab3 loopcount Code should be nicely indented and commented. Create a simple Makefile to compile your program into an executable called lab3. You should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT