Why use extern?

2

According to what I researched , the reserved word extern in C is used to inform that the declared variable is somewhere else in the program. A variable extern is useful for saving memory, because extern variables, although declared, are not initialized. However, I have seen in several places the words that "memory is no longer a problem these days", and this makes me wonder why using extern . In general, I would like to know if, between the two examples below, there is some strong reason to use one or the other.

Code 1:

example.h

extern int foo;

example.c

#include <stdio.h>    
#include "example.h"

int foo = 10;
void main(void) {
    printf("%d\n", foo);
}

Code 2:

example.h

int foo;

example.c

#include <stdio.h>
#include "example.h"

void main(void) {
    foo = 10;
    printf("%d\n", foo);
}
    
asked by anonymous 05.04.2017 / 10:52

2 answers

2
  

"Memory is no longer a problem these days"

This is a fallacy. If this were true we would never close a program in life.

Even if it is not to overdo it you still have many reasons to save memory. Memory is slow, so there are cache levels in the processors. Each level is faster but also has smaller capacity. The fastest cache has something like 64KB and this has not changed in a long time. Is that enough memory? The more memory you waste the more chance something is not in the cache and therefore there will be loss of performance.

One thing I always say is that people give sentences without explaining why. Usually they say this is "good practice," which is a great way to "sell an idea" that does not make sense or does not dominate.

So every time someone says something and does not say why, ignore it, ask why and go search other sources to see if that makes sense. But you have to choose the fonts and it is not common for people to know how to do this because when they know it, they do not need to do it anymore because search .

This case is tragic because you find a lot, but a lot of people even say it. And a lot of experienced people. Only they do not tell the whole story and leave the less experienced people believing in a lie or at least in a semi truth.

In these two examples there is already a problem that is using a global variable and this should be avoided . Worse, at least in these examples there is no reason to tr this global variable. So if you solve this problem there is no need for extern . But if you insist on this it is not only a question of saving memory, it is a question of saying that it is the same variable. If it is the same, you are not putting both statements in the same memory storage location to save memory, you are doing this because it is the same thing, there can not be two.

% w / w% is now used more for functions that are in another build unit.

You have almost a book about it in answer in SO . The summary there is: if you are not a beast in programming do not use extern in variables, or global in general.

    
05.04.2017 / 13:25
1

The use of the word extern is used when you want to use a shared variable in multiple files. A well-known usage is stdin . If you do not extern in the file .h an error can occur when link-editing is done or there may be a duplication of the variable, thus losing the purpose of being a shared variable between modules. In the above example it makes no difference because it is just a .h file and a .c file.

    
05.04.2017 / 14:26