1

As I understand, when function is called from translation unit and this function uses global variable of this translation unit, variable address is unchangable. In case of recalling function from this module address does not change. How can I provide, every entering into module's function will update variable address? Is it real to allocate variable at the stack, not at the data segment?

For example, we have module MODULE and function FUNC of this module. Also we have MODULE2 and FUNC2 of this module. Variable VAR is global variable of MODULE.

  1. Call FUNC of MODULE
  2. Call FUNC2 of MODULE2
  3. Call FUNC of MODULE. How can I provide to reallocate VARIABLE location in every entering into MODULE (actually every FUNC calling)?
8
  • 1
    "Modules" (or translation units which is their correct term) are only for the compiler itself. Once you have linked everything into a single executable program file, all global variables, and all functions, are using common chunks of memory. So from the executable programs perspective, all functions are available to call, and all global variables are available to access. Commented Feb 25, 2024 at 18:02
  • As a C programmer you should never wonder what a stack, data segment, or heap are. What you handle are automatic, dynamic, and static durations and block, translation unit or global scopes. Segments are the compiler problem, not yours. Commented Feb 25, 2024 at 18:03
  • @SergeBallesta unless you are a system or embedded system programmer Commented Feb 25, 2024 at 18:07
  • 1
    On a related note, please try to avoid global variables. You can pass local variables to external functions. And if the external function needs to modify the variable then use the pointer-to operator & to pass a pointer to the variable. Commented Feb 25, 2024 at 18:08
  • 1
    @SergeBallesta good luck in programming MCUs abstracting from the implementation Commented Feb 25, 2024 at 18:16

2 Answers 2

2

How can I provide, every entering into module's function will update variable address?

I take it as you wish for each translation unit to have its own instance of this global variable.

If so, declare it static in a header file that is included by all .c files that needs the variable and pass a pointer to the variable to the functions that are supposed to use it.

Sign up to request clarification or add additional context in comments.

Comments

1

IMHO, you are talking about calling a function, let's say f(), when f() calls g(), and at some point, g() calls again f() (what is called a recursive call)

If that's the problem, you have to declare your variable as local to the function. All variables in the inside of the external block (pair of curly brackets) are local (except if declared as static in which case they are global to the program, but not seen outside of the scope of the function block) and as local, they are reinstated (reallocated in a new stack frame) when the function is called (normally or recursively) so you have nothing to do, but declare the variables of interest as local, inside the function.

I'm not sure if this answers your question because you have given poor details to describe how will you be using the variables.

One function that has only automatic variables (variables that are created on entry and freed on return from the function) and has no global variables is called reentrant. This means the function can be called again while in the middle of its execution, of even worse, from two different threads of the same program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.