where the global and local variables are saved in memory in C

sophie's picture

Offline

Joined: May 21, 2019

Replies: 0
where the global and local variables are saved in memory in C
May 21, 2019 - 3:17pm

I work with the Aurix MUC, I tried to read the contents of the memory after the execution of a program, to see what he wrote in the memoir

I noticed that when I use a global variable in a function, the new value of this global variable after processing in the function, is not written in memory.

Here is an example:

int a = 100;

void plus (int a)

{

  a = a + 17;

}

int main (void)

{

plus(a);

return 0;

}

when I display the contents of the memory I find the value 100 of a

and I do not find the new value of a which is normally 117.

I tried to declare the variable a as volatile, but it does not change anything

on the other hand if I do the calculation directly in the main like this

int a = 100;

int main (void)

{

a = a + 17

return 0;

}

like that I find the value 117 in the memory.

so I need to understand where are there save variables values used in the call functions?

and why the new variable of a is not written in memory,

and why the variables declared in local are not also written in the memory?

Shivkumar's picture

Offline

Joined: May 28, 2019

Replies: 4
where the global and local variables are saved in memory in C
May 30, 2019 - 6:04pm

Code for generate global and local variables memory in C

#include <stdio.h> 
  
int global; /* Uninitialized variable stored in bss*/
  
int main(void) 

    static int i; /* Uninitialized static variable stored in bss */
    return 0;