Submit Programming Language Questions to Computer Education Techniques

Submit Programming Languages Questions Computer Education Techniques

Submit Programming Language Questions

Home

Submit Programming Languages Questions

Technology Update

Knowledge Base


Submit Programming Language Questions to Computer Education Techniques Advanced search
     

C Language Questions C++ Language Questions
Easytrieve Questions PL/1 Questions

Interrelated Information Technology

COBOL Questions Java Questions MS .NET Questions MS Application Development Questions
MS Phone 7 REXX Questions SAS Questions XML Questions

The Computer Education Techniques knowledge base is a service for answering questions, inclusive of the research and validation of the accuracy of information in the public domain. Citation of source documentation and examples are used to provide answers to the questions. Utilization of the information of this service and reliance on the answers, information or other materials received through this web site, is done at your own risk.

C Language Questions

Q My compiler is issuing errors relating to an "invalid initializer".

What is wrong with this initialization:

   char *p = malloc(10);

A Is the declaration of a static or non-local variable? Function calls are allowed in initializers only for automatic variables. The declaration must be for local, non-static variables.
Q What is the difference between these two declarations?

   struct x1 { ... };
   typedef struct { ... } x2;

A The first form declares a "structure tag"; the second declares a "typedef". The main difference is the subsequent reference to the first type is "struct x1" and the second type is as "x2".

The second declaration is a slightly more abstract type; its users do not necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.

Q Is there a way to compare structures automatically?
A No, there is not a good way for a compiler to implement structure comparison which is consistent with a low level language.
  • A simple byte-by-byte comparison would have problems on random bits present in unused "holes" in the structure.
  • A field-by-field comparison might require unacceptable amounts of repetitive code for large structures.

In order to compare two structures, it will be necessary to write a function, field by field, to do so.

Q Why doesn't the code the following code work?

   int a = 1000, b = 1000;
   long int c = a * b;

A Under C's integral promotion rules, the multiplication is carried out using int arithmetic, and the result may overflow or be truncated before being promoted and assigned to the long int left-hand side.
 

An explicit cast can be used to force long arithmetic:

   long int c = (long int)a * b;

Q Does C have "pass by reference"?
A C always uses pass by value. Accordingly, the answer is that C does not truly have a pass by reference.

It is possible to simulate pass by reference, by defining functions which accept pointers and then using the & operator when calling. The compiler will essentially provide the simulation when an array is passed to a function by passing a pointer instead.

Q How can an array's size be set at run time? How can I avoid fixed-sized arrays?
A The equivalence between arrays and pointers allows a pointer to malloc'ed memory to simulate an array.

After executing:

   #include <stdlib.h>
   int *dynarray;
   dynarray = malloc(10 * sizeof(int));

and if the call to malloc succeeds, a reference can be made to dynarray[i] (for i from 0 to 9) as if dynarray were a conventional, statically-allocated array (int a[10]).
 

The difference is that sizeof will not give the size of the "array".

Q

Are there any free C compilers?

A A list of free c compilers includes:
 
Apple's Macintosh Programmer's Workshop (C and C++ compilers) Apple Xcode for Mac OS X
BDS C Compiler Bloodshed Dev-C++ C++ Compiler

Cygwin Project (C & C++ Compilers)

DeSmet C
Digital Mars C/C++ Compiler (Symantec C++ Replacement) GNU C/C++ Compiler
Intel C++ Compiler for Linux Non-Commercial Version Microsoft Visual C++ 2008 Express
Open64 Compiler Tools Open Source Watcom / OpenWatcom C/C++ Compiler
Sun Studio Compilers and Tools Turbo C++ Explorer

C++ Questions

Q

We are uncertain as to whether our organization's compiler supports ANSI-C++?  

A

Experience with another programming language is not a prerequisite for learning C++.  As a result of the extensive use of special characters - {}[]*&!|..., code written in C++ gives the appearance of being more cryptic than some other languages. However, professional training will demonstrate that C++ is quite reasonable in its use of expressions and can be equally if not more straightforward than languages which rely on English words.

Example:

This code fragment uses the bool type, namespaces, and templates.

#include <iostream>

using namespace std;

template <class T>

bool ansisupported (T x) { return true; }

 

int main() {

  if (ansisupported(0)) cout << "ANSI OK";

  return 0;

}

If the compiler is able to compile this program, then it will be able to compile most of the existing ANSI-C++ code.

Q

What is an inline function?

A

When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream. This is similar to what happens with a #define macro. 

There are several ways to designate that a function is inline, some of which involve the inline keyword.  No matter how a function is designated as inline, it is a request that the compiler is allowed to ignore.  The compiler might inline-expand some, all, or none of the calls to an inline function. This flexibility allows the compiler to treat large functions differently from small ones.  It also allows the compiler to generate code that is easier to debug; this assumes that the appropriate compiler options have been selected.

Q

Do inline functions improve performance?

 

Explanation

There are a significant number of considerations which will impact the degree of optimization, if any, in called code.

Why inline functions might make code faster.

  • Procedural integration might remove unnecessary instructions, which can result in a more expedient execution of code.

Why inline functions might make the code more concise and smaller.

  • The compiler often generates more code to push/pop registers/parameters than it would by an inline-expansion of the function's body. This typically occurs with a very small function. 

  • It also happens with large functions when the optimizer is able to remove redundant code through procedural integration.  This is when the optimizer might be able to make the large function smaller.

Why inline functions might decrease the number of cache misses.

  • Inlining usually improves locality of reference within the binary code, which might decrease the number of cache lines needed to store the code of an inner loop. This ultimately could cause a CPU-bound application to run faster.

Why inline functions might prevent thrashing.

  • The number of pages that need to be in memory at once might go down even if the executable size goes up.

Why inline functions might make the code slower.

  • When there is too much inlining of functions, this may result in code bloat, which may in turn cause thrashing on demand-paged virtual-memory systems.

  • Inlining functions might increase the size of the binary executable, and inline functions might cause thrashing.

Why inline functions might increase the number of cache misses.

  • Inlining might cause an inner loop to span across multiple lines of the memory cache, and that might cause thrashing of the memory-cache.

Why inline functions might be irrelevant to speed.

  • Most systems are not CPU-bound; rather they are I/O-bound, database-bound or network-bound.  The bottleneck in the system's overall performance will be the file system, the database or the network.

  • Unless a CPU utilization is at at or near 100%, inline functions probably will not make a system faster.

A

There are no general rules which will yield an optimal unambiguous positive result. The use of inline functions might make the code faster, it might make it slower. The executable larger can be larger or smaller.

It might cause thrashing; it may prevent thrashing. Inline functions can have an unpredictable impact or be irrelevant in terms of speed.

Q Is there a significant difference between List x; and List x();?
 

Explanation by Example

Assume that List is the name of some class.

The function f() declares a local List object called x.

  void f()

 {

   List x;     // Local object named x (of class List)

   ...

 }

However, the function g() declares a function called x() that returns a List.

 void g()

 {

   List x();   // Function named x (that returns a List)

   ...

 }

A There is a considerable difference in the two functions.
Q Why are classes with static data members getting linker errors?
A

Classes with static members are getting linker errors because static data members must be explicitly defined in one compilation unit. When this is not done, there in all likelihood will be an "undefined external" linker error.

Example:

 // Fred.h

  class Fred {

 public:

   ...

 private:

   static int j_;   // Declares static data member Fred::j_

   ...

 };

 The linker would state ("Fred::j_ is not defined") unless the definition has been made Fred::j_ in one of the source files.

Example:

 // Fred.cpp

 #include "Fred.h"

 int Fred::j_ = some_expression_evaluating_to_an_int;

 // Alternatively, if you wish to use the implicit 0 value for static ints:

 // int Fred::j_;

The usual place to define static data members of class Fred is file Fred.cpp or Fred.C or the  source file extension in use.

Q What is "self assignment"?
A

Self assignment is when someone assigns an object to itself.

Example:

#include "Fred.h"  // Defines class Fred

 void userCode(Fred& x)

 {

   x = x;           // Self-assignment

 }

This explicit self assignment is not practical; however, since more than one pointer or reference can point to the same object, which is known as aliasing, it is possible to have self assignment without knowing it.

 #include "Fred.h"  // Defines class Fred

  void userCode(Fred& x, Fred& y)

 {

   x = y;           // Could be self-assignment if &x == &y

 }

 int main()

 {

   Fred z;

   userCode(z, z);

   ...

 }


Easytrieve Plus

Q How is the program compilation managed in a CA-Easytrieve program.
A The PARM statement is used for customizing the operating environment during program compilation. The PARM statement, is an optional statement, it must be the first statement in your program. When Easytrieve is installed, compiler options are specified for the entire site. The PARM statement is used for overriding site options for a specific program.
Q Easytrieve has a DMAP. Is this the same DMAP that is used in COBOL?
A The DMAP is a data map which provides the location of file and work fields. The DMAP also reports the attributes of all files and fields in a Easytrieve program. The  DMAP does perform the same function in COBOL; however, the Easytrieve program contains different information.
Q How does Easytrieve control the printer after each detail line is printed?
A The AFTER-LINE procedure can be coded for invocation after each individual line in a line group. The system-defined field LINE-NUMBER contains the number of the line in the group being processed. When the AFTER-LINE procedure is invoked, the detail line for the report has already been built. The contents of the detail line can not be modified with an AFTER-LINE procedure. There is also a BEFORE-LINE procedure that is invoked before each individual line in a line group.
Q Does Easytrieve have a statement similar to the COBOL EVALUATE statement?
A Yes. It has the CASE statement. The CASE and END-CASE statements are used for conditionally executing one of several alternative groups of statements based on the value of a specific field. A CASE statement can be nested within a CASE statement. Other conditional execution statements can also be nested within a CASE statement.
Q Which command is used to create break processing in Easytrieve?
A The CONTROL statement identifies control fields used for a report. A control break occurs whenever the value of any control field changes or end-of-report occurs. The control break at end-of-report is equivalent to the final break. A break level is also assigned to each control field.
Q What are the FILE Fields provided by Easytrieve?
A Easytrieve automatically provides the following special data fields:

RECORD-LENGTH
RECORD-COUNT
FILE-STATUS
PATH-ID
IDMSCOM
SLC
SQLCA

Q What is the purpose of the EXECUTE statement?
A The EXECUTE statement invokes a JOB, SORT, or SCREEN activity from either a PROGRAM or SCREEN activity. The EXECUTE statement transfers control to an activity. After the activity is executed, control returns to the next executable statement following the EXECUTE.

A JOB, SORT, or SCREEN activity cannot be invoked in a JOB or SORT activity. EXECUTE statements in a SCREEN activity can invoke other activities; this is known as activity nesting.


PL/1 Questions

Q Are there advantages associated with using the OPTIMIZER compiler option?
A

The advantages are:

1. Specifying the OPTIMIZE option will improve the speed of a program.

2. Choosing OPTIMIZE(2) directs the compiler to generate code for better performance.

  • The resultant code typically will be shorter than when the program is compiled under NOOPTIMIZE.
  • However, sometimes a longer sequence of instructions runs faster than a shorter sequence such as when a branch table is created for a SELECT statement where the values in the WHEN clauses contain gaps.  The increased number of instructions generated is usually offset by the execution of fewer instructions in other places.

3. Choosing OPTIMIZE(3) directs the compiler to generate even better code.

  • However, the compilation will take longer than when specifying OPTIMIZE(2) and can be significantly longer.
Q Senior members of our staff have informed me that the use of PUT DATA commands is very inefficient. What then is the reason for using PUT DATA commands?
A PUT DATA statements can be invaluable for purposes of debugging at the cost of decreased performance. This cost to performance is usually very high when either GET DATA or PUT DATA is used without a variable list.
  Explanation by Example:

A reasonable practice is to use PUT DATA statements in ON ERROR code.

on error begin;
          on error system;
          . . .
          put data;
          . . .
end;

The program would be further optimized by including a list of selected variables with the PUT DATA statement.

Q What is the REDUCIBLE option on a function?
A REDUCIBLE indicates that a procedure or entry does not need to be invoked multiple times if the argument(s) stays unchanged, and that the invocation of the procedure has no side effects.
  Guideline:
  • A user-written function that computes a result based on unchanging data should be declared REDUCIBLE.
  • A function that computes a result based on changing data, such as a random number or time of day, should be declared IRREDUCIBLE.
Q Should, UNION or DEFINED be used in Enterprise PL/1?
A The UNION attribute is more powerful than the DEFINED attribute and provides greater functionality. Some of the reasons for using UNION as opposed to DEFINED include:
  • Code that uses UNIONs instead of the DEFINED attribute is subject to less misinterpretation.
  • Variable declarations in unions are in a single location making it easy to realize that when one member of the union changes, all of the others change also.
  • The compiler will generate better code for union references.
  • Dynamic change that use DEFINED variables are less obvious in declarations since the declare statements can be several lines apart.
Q In PL/1 should named constants or static variables be used?
A Named constants are defined by declaring a variable with the VALUE attribute. If static variables are used with the INITIAL attribute and the variable is not altered, the variable should be declared a named constant using the VALUE attribute.

The compiler does not treat NONASSIGNABLE scalar STATIC variables as true named constants.

The compiler generates better code whenever expressions are evaluated during compilation; therefore the use of named constants can be used to produce efficient code with no loss in readability.

Q What is the JNI: Java Native Interface?
A JNI is the Java interface to native programming languages and is part of the Java Development Kits. Programs written to use the JNI, ensure that code is portable across many platforms. JNI allows Java code that runs within a JVM: Java Virtual Machine to operate with applications and libraries written in PL/1 and other languages. The Invocation API allows a Java Virtual Machine to be embedded into native PL/I applications.
Q What XML parsers are available for PL/1?
A There are two major types of interfaces for XML parsing: event-based and tree-based.

For an event-based API, the parser reports events to the application through callbacks such as the start of the document, the beginning of an element, etc. The application provides handlers to deal with the events reported by the parser. The Simple API for XML or SAX is an industry-standard event-based API.

For a tree-based API, the parser translates the XML into an internal tree-based representation. Interfaces are provided to navigate the tree. IBM PL/I provides a SAX-like event-based interface for parsing XML documents. The parser invokes an application-supplied handler for parser events, passing references to the corresponding document fragments. The Document Object Model or DOM is a tree-based API.


Schedule