DEV Community

NightBird07
NightBird07

Posted on

C has some API

I took part in a contest that challenged me to write a very large and complex file. After finishing the first phase, I had to refine my code. I faced a problem with modularity design, which is how to split my code into smaller and simpler modules that use memory effectively. Before I go into more detail about this concept, let me explain some terms that you might need to know.

Memory management is an important aspect of programming, as it affects how well a program can use the available memory resources. Memory management involves allocating, using, and freeing memory for different purposes, such as storing data, executing code, and communicating between processes.

In this article, we will compare two memory management techniques: extern and shared memory. Extern is a keyword that allows a program to access a global variable or a function that is defined in another file. Shared memory is a mechanism that allows multiple processes to access the same memory region for inter-process communication.

Extern:

The “extern” keyword is used in programming languages like C and C++ to declare variables or functions that are defined in other files. When a variable or function is declared as “extern,” it means that its definition can be found in another file within the project. The purpose of using “extern” is to provide a forward declaration so that the compiler knows that the variable or function exists and can be used, even though its definition is located elsewhere.

Using the “extern” keyword allows for variable sharing across files in a project. For example, if you have a variable defined in one source file and you want to access it in another source file, you can declare it as “extern” in the second file. This tells the compiler that the variable is defined in a different file and should be linked appropriately during the compilation process. By using “extern,” you can share variables and functions across multiple files, enabling modular programming and code reuse.

Shared Memory:

Shared memory refers to a region of memory that can be accessed by multiple processes or threads simultaneously. In concurrent programming and inter-process communication (IPC), shared memory is used to exchange data between processes efficiently. Instead of using message passing or other forms of communication, processes can directly read from and write to the shared memory region.

both are the same

though you may conclude that the extern is a safe API to use shared memory with restrict constraints on how to use the extern.

When to Use "Extern":

Use "extern" when sharing variables within a single program, especially when the data needs to be accessed by multiple source files.
It simplifies data sharing and ensures consistency by using a single instance of the variable throughout the program.
"Extern" is suitable for scenarios where multiple components of the program need access to common data without creating separate copies.

When to Use Shared Memory:

Use shared memory when communication is required between independent processes running on the same machine.
It enables data exchange and cooperation between separate programs by allowing them to access and share a common memory space.
Shared memory is beneficial in scenarios where inter-process communication is needed for tasks like data exchange, coordination, or parallel processing.

Final Thoughts and Recommendations:

Both "extern" and shared memory serve important roles in different scenarios. Choose "extern" when focusing on data sharing within a single program to improve code modularity and efficiency.
On the other hand, opt for shared memory when dealing with inter-process communication and cooperation between independent processes.
Understanding the distinctions between these mechanisms will help developers make informed decisions about which approach to employ based on the specific requirements of their projects.

Top comments (0)