[SOLVED]different type of linkage

Thread Starter

Kittu20

Joined Oct 12, 2022
462
I am trying to understand how compiler know the correct location of variables when there are multiple variables with same name is given in program. I found the scope of variables depends on linkage.

Take a look at code. I have written the linkage of each variable in the comments.

C:
#include<stdio.h>

int x;           // variable x with external linkage         
static int y;    // variable y with internal linkage
extern int y;    // variable y with external linkage

void foo()
{
  int x;         // variable x with internal linkage.
              
  static int y;  // variable y with internal linkage
}

int main()
{
   int x;          // variable x with no linkage

  return 0;
}
 

BobTPH

Joined Jun 5, 2013
8,804
The compiler does not generally know the location of a variable. That is the function of the linker for statically allocated variables.

For local (auto) variables, the location is not determined until the function is called at runtime, and might change from one function call to the next.

Just be assured that the complier knows how to address the variable in all cases because compiler writers are very smart.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
462
why program doesn't give output y = 5 ? Why this output shows on commenting line number 7 in code ?

hello.c
C:
#include<stdio.h>

// x can accessible in hello.c temp.c file
int x;           // variable x with external linkage

// y can only accessible in hello.c file       
static int y = 10;    // variable y with internal linkage

// y can accessible in hello.c temp.c file
extern int y;    // variable y with external linkage

void foo()
{
  // x and y can only accessible in function foo

  int x = 2;         // variable x with internal linkage.           
  static int y = 3;  // variable y with internal linkage

}

int main()
{
   // x can only accessible in main function

   int x = 1;          // variable x with no linkage

   printf("y = %d", y);

  return 0;
}
temp.c
Code:
int y = 5;
 
Last edited:

WBahn

Joined Mar 31, 2012
29,976
I am trying to understand how compiler know the correct location of variables when there are multiple variables with same name is given in program. I found the scope of variables depends on linkage.

Take a look at code. I have written the linkage of each variable in the comments.

C:
#include<stdio.h>

int x;           // variable x with external linkage        
static int y;    // variable y with internal linkage
extern int y;    // variable y with external linkage

void foo()
{
  int x;         // variable x with internal linkage.
             
  static int y;  // variable y with internal linkage
}

int main()
{
   int x;          // variable x with no linkage

  return 0;
}
Why do you think that the variable 's' in foo() and main() have different linkages?

Since none of the variable are actually used, the only one the compiler/linker is obligated to allocate memory for compiler is free is the first one (since it is defined here and might be declared as external variables in another file).

You then have a conflict in your two declarations/definitions of 'y'. The first one says that 'y' is a variable that is defined here and whose scope does not extend beyond this file, while the second says that 'y' is only declared here and that it is defined in some other file and is reachable from this file. The compiler should throw an error over this.

In your two functions, 'x' is a local variable allocated on the stack. It shadows the 'x' at the top of the code.

In foo(), your 'y' is allocated one on the heap and is visible only to foo(). It shadows whatever statement defining/declaring 'y' you decide to keep.

The compiler keeps track of all of these by the use of a symbol table that has an entry for every variable that is declared/defined. How that symbol table is organized is completely up to the compiler writer. One might keep a separate symbol table for every scope, while others might keep a unified symbol table in which each entry has additional information to establish scope.

One useful way to think of it, whether or not this is actually done by the compiler, is that every variable's name gets mangled with this information. So if this is in file myprog.c, the 'x' in foo() becomes myprog_auto_foo_x, while the 'x' in main becomes myprog_auto_main_x. The "global" 'x might be renamed external_x, while the static 'y' might be myprog_file_y. The external 'y' would also become external_y, the only difference between it and external_x is that memory would be allocated for external_x but not for external_y -- that allocation happens in whatever file y is defined.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
462
Why do you think that the variable 's' in foo() and main() have different linkages?
Variable or functions declared with "static" will have internal linkage.
variable declared inside function will have no linkage
Variable or functions declared with "extern" will have external linkage,

I have written comments for each line in code

C:
#include<stdio.h>

int x;               // x can accessible in hello.c temp.c file      

     
static int y = 10;  // y can only accessible in hello.c file
                    // internal linkage


extern int y;       // y can accessible in hello.c temp.c file
                    // y is only declared here and it is defined in other file temp.c
                    // variable with external linkage
                    // y in hello.c and y in temp.c are same

void foo()
{                  
                   
  int x = 2;         // x and y are local variables        
  static int y = 3;  // x and y can only accessible in function foo
                     // no linkage
}

int main()
{
   int x = 1;         // x can only accessible in main function, no linkage
   printf("y = %d", y);

  return 0;
}
 
Last edited:

WBahn

Joined Mar 31, 2012
29,976
But look at your original code post. You said that the variable 'x' in foo() had internal linkage, while the variable 'x' in main() had no linkage.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
462
But look at your original code post. You said that the variable 'x' in foo() had internal linkage, while the variable 'x' in main() had no linkage.
That was my mistake which I have corrected in post #8
variable 'x' in foo() having no linkage
 

ApacheKid

Joined Jan 12, 2015
1,533
I am trying to understand how compiler know the correct location of variables when there are multiple variables with same name is given in program. I found the scope of variables depends on linkage.

Take a look at code. I have written the linkage of each variable in the comments.

C:
#include<stdio.h>

int x;           // variable x with external linkage
static int y;    // variable y with internal linkage
extern int y;    // variable y with external linkage

void foo()
{
  int x;         // variable x with internal linkage.
    
  static int y;  // variable y with internal linkage
}

int main()
{
   int x;          // variable x with no linkage

  return 0;
}
How this works varies from language to language and implementation to implementation. What the compiler typically does though is determine the size of all declared items that have a known size at compile time. If something has a size that is not known until run time then it generates code to calculate that size at runtime. The data structure that contains all size and offset information for every declaration, is created as the compiler progresses and is called the symbol table.

The compiler (or rather some phase within the compiler) then sets about determining the relative offset of every static variable and every automatic variable declared inside functions. In the case of static items that offset will be used at runtime relative to some externally defined address set by a linker or loader.

In the case of automatic items, the size of a functions stack frame is calculated from the sizes of all of the automatic items declared in the function, that's usually a constant size, determined at compile time but in some cases might be variable and only known at runtime (like dynamically sized arrays), this is used to create a stack frame when a function is invoked.

The offsets are based on the size of preceding items and the offset needs of the declared item, so the total size of the storage includes some "wasted" space to ensure that all declared items are offset as appropriate for the their type.

The automatic variables themselves are accessed via offsets from the stack frame's frame pointer, when the function eventually returns, the space that was created on the stack for these variables is reclaimed by setting the stack pointer to it value prior to the call.

So the specific absolute addresses of things is pretty much unknown (except perhaps for static items once the code is linked) but the size and offsets of everything is usually known, calculated during compilation, prior to code generation, this is typically how things are done anyway.

This is a good diagram for the stack frame stuff, you'll need to study it to fully understand it but it's a very neat design and used by many languages on many platforms:

1673646801005.png
 
Last edited:
Top