Viewing the characteristics of c,c++,c#,java,php by static local variables

Source: Internet
Author: User
0 origin of the problem

The consideration of this problem comes from the implementation of the single-instance design pattern in object-oriented design.

The standard code for the singleton pattern implementation in C + + is:

#include 
 
  
   
  int init () {    printf ("init () \ n");    return 22;} int gettheonly () {    static int x = init ();      return x;} int main () {    int only = Gettheonly ();    return 0;}
 
  

In the Get instance function gettheonly (), the static local variable user stores a unique instance and initializes it dynamically using the init () function directly.

It seems so simple, but the same code is compiled as C but cannot pass , the compiler compiles the static int x = init () This line times wrong:

Error: initializer element is not constant

Itcan be seen that static local variables in the C language must be initialized with constants, meaning that the initial value must be determined by the compiler.

Consider, in order to invoke a function to initialize a static variable, C + + must guarantee init (); For this purpose, the C + + compiler must add extra code, and I can think of the C + + compiler for the static int x = init (); The pseudo-code that may be added is as follows:

    static int x;    static char flag = 0;    if (flag = = 0) {        x = init ();
        flag = 1;    }    return x;
As you can see, the C + + compiler privately generates redundant assembly code for us. The result is that C + + is easy to use, but not easy to understand.

In order to see how the other languages dealt with the problem, a similar experiment was followed using c#,java,php, and a small conclusion was drawn.

1 C#,java does not support static local variables at all

Both support static member variables and do not support static local variables within functions. Think about it too, static local variables can almost always be replaced with static member variables.

C # test Code:

Using System;namespace consoleapplication2{    class program    {        static int x = 0;        static int init ()        {            Console.WriteLine ("Init ()");            return;        }        static int gettheonly ()        {            //static int x = 0;  Static local variable is not supported by C #.            if (x = = 0)            {                x = init ();            }            return x;        }        static void Main (string[] args)        {            gettheonly ();            Gettheonly ();}}}    

Java Test Code:

public class t{public    static int init () {        System.out.println ("init () \ n");        return;    }    private static int x = 0;    public static int gettheonly () {        //static int x = 0;  This line cannot was compiled, static local variable is not supported by Java        if (x==0) {            x = init ();        }        return x;    }    public static void Main (string[] args) {        gettheonly ();        Gettheonly ();    }}

2 PHP treats static local variables the same as C, only supports constant initialization

PHP Test Code:

 
31-Point thinking

The characteristics of these languages can be found through the very small language details of static local variables. The

C + + compiler is a full-time, hardworking and versatile person who tries to provide more language functionality to the user, which must be secretly generated code for the user, resulting in the complexity of the C + + language and the "iceberg effect." (Think of C + + multiple inheritance, stack objects, copy constructs ...)


Java and C # focus on ease of use, avoid ambiguity, and for the same function, give the user only the right choice. (Think of single inheritance, objects can only be built on the heap, garbage collection)


C always keep its simplicity and transparency, the compiler wussy, and see that the code is basically predictable to the generated assembly.


The structural part of PHP mimics the syntax of C, so many features are similar, but after all it is explanatory language, especially variable name, class name and so on itself can be used as the explanatory language characteristics of variables, so that it becomes extraordinarily flexible. The object-oriented part imitates the Java syntax, and it fully embodies the characteristics of the interpretation language.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.