9, initialization of variables

xiaoxiao2021-03-06  85

------------

Connecting a top, the variable must be initialized and reused. The C / C compiler will not help you initialize like Java, all need you, if you use no initialized variables, the result is unknown. Good programmers have never initialized the variables before using variables. Such as:

1) MEMSET cleared operations for Malloc allocation. (You can assign a total memory using Calloc)

2) Initialize the Struct or array assigned on some stacks. (It is best to clear)

But then it is back, initialization will also cause a certain overhead of the system running time, so don't initialize all variables, this is meaningless. A good programmer knows which variables need to be initialized, which is not required. Such as: The following situations are not required.

Char * pstr; / * A string * /

PSTR = (char *) Malloc (50);

IF (pstr == null) exit (0);

STRCPY (PSTR, "Hello Wrold");

But if it is the case, it is best to perform memory initialization. (The pointer is a dangerous thing, must initialize)

Char ** PSTR; / * A string number * /

PSTR = (char **) Malloc (50);

IF (pstr == null) exit (0);

/ * Let the pointer in the array points to null * /

MEMSET (PSTR, 0, 50 * Sizeof (char *));

For global variables, and static variables, it must be initialized when declaring. Because you don't know where it will be used for the first time. So the initial use before use is more unrealistic, so you must initialize them when declaring. Such as:

LINKS * PLNK = NULL; / * For global variables, PLNK is initialized to null * /

转载请注明原文地址:https://www.9cbs.com/read-107742.html

New Post(0)