Friday, October 7, 2022
HomeTech NewsHandling multiple operating systems

Handling multiple operating systems

Creating the header file

The problem with the “standard” header files is that they are anything but standard. For example, on Microsoft windows, the header file for containing all of the “standard” output functions is studio. Whereas on UNIX, the header file is unlisted. Imagine you’re compiling a program that can be used on either UNIX or Microsoft windows. The code in all your files might look like this:

This approach to coding is messy and inefficient: if you get a new compaler that implements the constants for the operating system differently, you will have to go through each and every file to update your code. As an alternative, you could simply include all the files in a single header file but that would force you to include a lot of header files that you really don’t need in many of your program files, which would increase the file bloat and could conceivably cause problems if you need to override some of the standard function names or types. Obviously, clutter is not a very good solution either way. What if instead of including the things you don’t want and having to compile conditionally around them  you could include only the “right” files for a specific operating system in the first place? That solution would certainly be closer to ideal. Fortunately, the  pre-processor offers a perfect way to solve this problem.

Testing the header file

After you create the class, you should create a test driver that not only ensures that your code is correct, but also shows people how to use your code. Here i show you how to create a test driver that illustrades various kinds of input from the user, and shows how the class is intended to be used.

As you can see, the compiler definitely knows that the operating system is not defined. The next step is to define one of the two constants, depending on the operating system of your choice. There are two differvent ways to define these constants. You can either put a #define statement at the top of the header file or you can pass the value into the compiler with the –d compile flag. Recompiling the program after this operation should result in no errors — and if that’s the case, you know the proper header file is now being included.

Mastering the evils of asserts

I t’s hard to talk about the  pre-processor without talking about the assert macro. This particular macro is used to test a given condition — and, if the condition is not logically true, to print out an error message and exit the program. Here’s where you can get (ahem) assertive with the problem of testing for problems, so a quick look at asserts is in order, followed by a simple technique for using them.

The assert problem

The purpose of an assert statement is to check for a problem at runtime. Assert statements have value during the initial debugging and validation of code, but they have limited value once the program is out in the field. For this reason, you should put in enough assert statements to be sure that the tests of your system will reveal all of the potential problems that you should check for and handle at run-time. Let’s look at a simple example of using an assert call in your program.

Read more: resurge reviews

Last word

The above is the output when you run the program after compiling with the dndebug flag for the compiler. As you can see, it is very different from the case where the assert macro is enabled. Note that there was no argument supplied to the program, so we are actually stepping all over memory at this point. Since the array of pointers is filled with the arguments to the application, we are restricted to the number of arguments passed in. If nothing is passed into the program, there will be nothing in the array of arguments, and the pointers in the agro array will be pointing at garbage. Fortunately, we didn’t try to do anything with the pointer except print it out, but it could easily have caused a program crash of its own. Imagine if this code had made it into a production system. The first time that an optimized (often called a “release”) build was created, the program would crash as soon as the user ran it without giving the program any arguments on the command line. Obviously, this is not an optimal solution when you are working in the real world. In the next section, i show you how to address this problem.



Please enter your comment!
Please enter your name here

Most Popular

Category List