Characterize each of the following in terms of Name, Address, Value, Type, Lifetime, and Scope.
-Memory allocated on the heap in C
-C static character variable
-C global floating point variable
Answer:
1. Memory allocated on the heap in C:
Name: Any identifier
Address: Dynamic storage (large pool of memory, not allocated in contiguous order).
Value: Value to the variables are allocated and freed using functions like malloc() and free()
Type: pointer type
Lifetime: The lifetime of a dynamic object begins when memory is allocated for the object (e.g., by a call to malloc()) and ends when memory is deallocated (e.g., by a call to free()).
Scope: Global scope
2. C static character variable:
Name: Any identifier
Address: Takes 1 byte
Value: Variables declared static are initialized to zero (or for pointers, NULL) by default.
Type: Any local or global variable can be made static depending upon what the logic expects out of that variable.
Lifetime: Its lifetime is the entire duration of the program's execution.
Scope: local to the block in which the variable is defined.
3. C global floating point variable:
Name: Any identifier
Address: Takes 4 bytes in memory(allocated in contiguous order).
Value: Variables are initialized to zero (or for pointers, NULL) by default.
Type: It is used to store decimal numbers (numbers with floating point value) with single precision.
Lifetime: Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
Scope: The scope of global variables will be throughout the program.
Please give thumbsup, if you like it. Thanks.
Characterize each of the following in terms of Name, Address, Value, Type, Lifetime, and Scope. -Memory...
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); }
Each of the following items has a different lifetime inside a computer. Considering process execution, rebooting the machine and disk storage, please rank the items from shortest lifetime to longest. Shared Memory Segments Non-static local variables inside a function Executable files Dynamic memory allocated and deallocated in main() Global and Static Variables
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...
Most systems allow a program to allocate more memory to its address space during execution. Allocation of data in the heap segments of programs is and example of such allocated memory. What is required to support dynamic memor allocation in the following schemes? a. Contiguous memory allocation b. Pure segmentation c. Pure paging Explain each of the above schemes and provide sources to back your answers.
3) (A) When the scope resolution operator is used before a variable name, the compiler is instructed to use a(n) ______variable a) integer b) global c) local d) character B) There are _______ storage classes that can be associated with a variable in c++ a) 1 b) 2 c) 3 d) 4 C) In c++ register variable have the same time duration as __________variables. a) auto b) static c) extern d)all other D) In c++ once a global variable is...
I. (10 points, 1 point each) Match each of the terms on the left by choosing the upper case letter of the best answer on the right and putting it next to the lower case letter in the space provided A. a value passed to a method B. holds multiple values of the same type C. the part of a program where a declaration is valid D. group of statements with a name E. a single command that can be...
C programming language
4. (Memory allocation) Consider typedef struct char name [20]; int balance; int gain; ) Player; int ni Player pi scanf ("%d", &n); Complete the code above by allocating a memory for n players each of which of type Player and making p point to the start of that memory (or in other words, making p have the value of the starting address of that memory). [Recall the slides on dynamic memory allocation function malloc.]
2-1. Outline briefly what is meant by the following terms: (a) low memory (b) address space (c) stored program (d) (e) (f) self-modifying program central processing unit instruction decoding
List four of the six possible binding times, and for each binding time give an example of a binding that occurs at that time. • Language design time -- bind operator symbols to operations • Language implementation time-- bind floating point type to a representation • Compile time -- bind a variable to a type in C or Java • Load time -- bind a C or C++ static variable to a memory cell) • Runtime -- bind a nonstatic...
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...