Question
Asked Dec 12, 2019
2 views

Explain the difference in the C memory model between global and local variables. How is each allocated and accessed?

check_circle

Expert Answer

Step 1

C memory-model:

The C memory-model can be classified into three types:

  • Memory allocation in heap
  • Global memory allocation
  • Memory allocation in the stack

 

The memory allocation in Heap is used to allocate the memory to the variables dynamically.

The memory allocation in Stack is used to allocate the memory for local variables.    

When a variable is declared as global, it means that the variable can be accessed in the whole program.

Step 2

Global variables: The variables which can be accessed in any function defined in the program are known as global variables. They are declared outside the function (just after the header files).  

For ex-

#include <stdio.h>

int x = 4;

                Int y= 6;

                public int sum()

{

return x+y;

}

According to the above example, the variables x and y are declared as the global variable as they are defined outside the function int sum() but are accessed in the mentioned function. The values of the variables can be changed as they are not static variables. Also, these variables can be accessed within any function defined in the program.

Note: if the value of the global variable is changed in one function than this change is visible in the entire program.

Step 3

Local variables: The variables which cannot be accessed outside the scope of the function in which they are declared are known as local variables.

The local variables only exist until the function is under execution. After the execution, they are destroyed. The local variables can only be modified (change the value of the variables) in the function in which they are declared.

The local variables are created on Stack.

For ex-

public int sum()

{

int x = 6;...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: "A"     .-                      "N"     -.                      "1"                  .---- "B"     -...

A: Program Instructions:  ⦁ Create a C++ program and declare all required variable and include all requ...

question_answer

Q: Using C++ complete the following program 10.18: Phone Number ListWrite a program that has an array o...

A: Program Instructions:Declare a string array phoneDirectory[] and open the file named “phonebook.txt"...

question_answer

Q: What is the information system scope of Amazon Web Services?

A: Information system scope of Amazon web services (AWS)Amazon web service is a service cloud service p...

question_answer

Q: Can you think about situations where default values will improve data integrity, and situations wher...

A: Default values:Default values are used to avoid errors where there is no entry.Default values are va...

question_answer

Q: Book information: Textbook: Introduction to Java Programming and Data Structures, Comprehensive Vers...

A: Program Algorithm-STARTStep1: Import the essential java packages.Step2: Define the class Convert.Ste...

question_answer

Q: What is the minimum number of processors and amount of RAM required for the ATA Gateway?

A: Minimum number of processors and amount of RAM required for ATA gatewayThe Advanced Threat Analytics...

question_answer

Q: in java, using the numbers 1-10 write a program that will call a method (called f) to tabulate the f...

A: Create class TabulateUse method f(int x) to compute the given mathematical polynomial.Create object ...

question_answer

Q: a. Write the assembly language equivalent of the following MARIE machine language instructions:a) 00...

A: Assembly language equivalent to marie machine languageThe equivalent assembly instruction for given ...

question_answer

Q: What is the Oracle database SQL statement needed to show system privileges associated with my role? ...

A: This question is based on the Data Base SQL (Structured Query Language):Data Base: A data base is a ...