Expert C Programing Reading Notes

zhaozj2021-02-16  68

All of the following is taken from Expert C Programming Book

Long Int:

The Maximum Value of Long Int Can't Be Any Less Than 2,147,483,647 (i.e., Long Integers Are AT Least 32 Bits).

The maximum value of Long Int is not less than 2,147,483,647, that is, the LONG type integer should not be less than 32 bits.

.

Each parameter should have its own type so that it can assign a value of the type of the type of parameter corresponding to it.

This Says That Argument Passing is supposed to behave like assignment.

This means that the parameter transfer process is similar to assignment.

ANSI C Use Value PreServing Approach.

The ANSI C standard adopts a value reservation principle.

When a Signed Int IS Comparing with unsinged int, the sign int is promoted to unsigned int.

When Signed Int is compared with unsinge Int, Signed Int is upgraded to unsigned int.

Static:

Inside a function, retains its value between calls.

Inside the function, the value of the variable is extended in each of the calls.

At The Function Level, Visible Only In this file.

At this stage, it means that the function is only visible to this document.

EXTERN:

Applied to a Function Definition, HAS Global Scope (and is redundant).

Used for function definitions, indicating global visible, belonging to redundancy.

Applied to a variable, defined elsewhere.

Used for variables indicating that it is defined elsewhere.

Void:

As The Return Type of a Function, Doesn't Return a Value.

As the return type of the function, it means that no value is returned.

IN a Pointer Declaration, The Type of a Generic Pointer.

In the pointer statement, it represents the type of the general pointer.

IN a parameter list, takes no parameters.

In the parameter list, there is no parameter.

BSS Segment:

It gets its name from abbreviaating "block started by Symbol".

Local Variables Don n't Go In Object File, But Are Created At Runtime.

The Test Segment Contains The Program Instructions.

The Data Segment Contains The Initialized Global and Static Variables, Complete with Their Assigned.

The name of the BSS section is an abbreviation for "Block Started By Symbol". The local variable does not enter the target file, and they are created at runtime. Text section contains instructions for the program. Data segments contain global and static variables that have been initialized and their values. Automatic Variables Are Local Varialbes Declared Inside Funcitons.

Automatic variables are local variables defined within the function.

Memory Leak

An Addional Point To Note Is That a Leak Will Usually Be Larger Than The Size of The Forgoten Data Structure, Because Malloc () Usually Rounds Up A Storage Request to the next larger power-of-two.

In addition, it is important to note that the leak-leaking memory is often larger than the data structure that is released, because Malloc () allocated by Malloc () is typically rounded into an integer of the next number of applications.

Alignment Means That Data Items Can Only Be Stored At An Address That Is A Multiple of Their size.

Alignment means that the data item can only be stored at the address of the integer size of the data item size.

Common Immediate Causes of Segmentation Fault:

Dereferencing a Pointer That Doesn't Contain A Valid Value;

Dereferencing a Null Pointer (OFTEN BECAUSE The Null Pointer Was Returned from a system routine, and used welch checking);

Accessing Something Withnout The Correct Permission - for Example, Attempting to Store A Value Into a Read-Only Text Segment Would Cause This Error

Running Out of Stack OR Heap Space (Virtual Memory IS HUGE But Not Infinite).

Several direct causes of paragraph errors are usually:

Unlike a pointer that contains illegal values;

Unlike an empty pointer (often used from the system program);

Access is permissions when you are not getting correct permissions. For example, trying to a read-only text segment stored value will cause a paragraph error;

After using the stack or stack space (although virtual memory is huge but not unlimited).

How to free elements in a linked list:

Struct node * p, * start, * TMP;

For (p = start; p; p = TMP)

{

TMP = P-> next;

Free (p);

}

Kbhit ()

#include

#include

#include

#include

Int kbhit (void)

{

INT I;

IOCTL (0, Fionread, & I);

Return I;

}

Void main (void)

{

INT i = 0;

INT C = '';

System ("stty raw");

Printf ("Enter 'q' to quit / n");

For (; c! = 'q'; i )

{

IF (kbhit ())

{

C = getchar ();

Printf ("/ n got% C, ON ITERATION% D / N", C, I);

}

}

System ("stty cooked");

Return;

}

ReWriting The Main Function Above To Use Curses Instead of Stty Gives:

Void main (void)

{

INT i = 0;

INT C = '';

INITSCR ();

CBREAK ();

Noecho ();

MVPrintW (0, 0, "Press 'q' to quit./N");

Refresh ();

While (c! = 'q')

{

IF (kbhit ())

{

C = getCH ();

MVPrintW (1, 0, "got char '% c' on ity);

Refresh ();

}

}

Nocbreak ();

echo ();

Endwin ();

Return;

}

Declaring analyzer with FSM

#include

#include

#include

#include

#define maxToKens 100

#define maxTokenlenlen 64

ENUM TYPE_TAG {Identifier, Qualifier, TYPE};

Struct token

{

CHAR TYPE;

Char string [maxTokenlen];

}

INT TOP = -1;

Struct token stack [maxToKens];

Struct token this;

#define pop stack [top -]

#define push (s) stack [ TOP] = s

ENUM TYPE_TAG Classify_String (Void)

{

Char * s = this.string;

IF (! STRCMP (S, "const"))

{

STRCPY (S, "Read-Only");

Return qualifier;

}

IF (! strcmp (s, "volatile)) Return Qualifier;

IF (! STRCMP (S, "Void")) Return Type;

IF (! STRCMP (S, "char")) Return Type;

IF (! STRCMP (S, "SIGNED")) Return Type;

IF (! strcmp (s, "unsigned") RETURN TYPE;

IF (! STRCMP (S, "Short")) Return Type;

IF (! STRCMP (S, "INT")) Return Type;

IF (! STRCMP (S, "long")) Return Type;

IF (! STRCMP (S, "float")) Return Type;

IF (! strcmp (s, "double")) Return Type;

IF (! STRCMP (S, "Struct")) Return Type;

IF (! STRCMP (S, "Union")) Return Type;

IF (! STRCMP (S, "Enum")) Return Type; Return Identifier;

}

Void gettoken (void)

{

charr

* p = this.string;

While (* p = getchar ()) == ');

IF (isalnum (* p))

{

While (isalNum (* p = getchar ()));

Ungetc (* p, stdin);

* p = '/ 0';

THIS.TYPE = Classify_string ();

Return;

}

IF (* p == '*')

{

STRCPY (this.String, "Pointer to");

THIS.TYPE = '*';

Return;

}

THIS.STRING [1] = '/ 0';

this.Type = * P;

Return;

}

Void read_to_first_identifier (void)

{

GetToken ();

While (this.Type! = Identifier)

{

Push (this);

GetToken ();

}

Printf ("% s IS", this.string;

GetToken ();

}

Void deal_with_Arrays (void)

{

While (this.Type == '[')

{

Printf ("array");

GetToken ();

IF (isdigit (this.string [0])))

{

Printf ("0 ..% D", ATOI (this.String) -1);

GetToken ();

}

GetToken ();

Printf ("of");

}

}

Void deal_with_function_args (void)

{

While (this.Type! = ')')

{

GetToken ();

}

GetToken ();

Printf ("Function Returning");

}

Void deal_with_points (void)

{

While (stack [top] .type == '*')

{

Printf ("% s", stack [top] .string);

}

}

Void deal_with_declarator (void)

{

Switch (this.type)

{

Case '[':

DEAL_WITH_ARRAYS ();

Break;

Case '(')

DEAL_WITH_FUNCTION_ARGS ();

Break;

DEFAULT:

Break;

}

DEAL_WITH_POINTERS ();

While (TOP> = 0)

{

IF (Stack [TOP] .Type == '(')

{

POP;

GetToken ();

DEAL_WITH_DECLARATOR ();

}

Else

{

Printf ("% s", stack [top] .string);

}

}

}

Int main (void)

{

Read_to_first_identifier ();

DEAL_WITH_DECLARATOR ();

Printf ("/ n");

Return 0;

}

When Arrays Are Pointersthe C Standard Has The Following To Say About The Matter:

.

2. A Subscript is always equivalent to an offset from a pointer.

.

When do you have the same group and a pointer

C language standards have been described below:

Rule 1. The array name (different from the declaration) in the expression is a pointer to the first element of the array;

Rules 2. The subscript is always the same as the offset of the pointer;

Rules 3. In the declaration of the function parameters, the array name is compiled as a pointer to the first element of the array.

. The compiler take care of scaling before adding in the base address This is the reason why pointers are always typed - constrained to point to objects of only one type - so that the compiler knows how many bytes to retrieve on a pointer dereference, and it Knows by How much to scale a subscript.

The compiler will be responsible for calculating the increased step size before the start address is active. That's why the pointer always has type limits, each pointer can only point to a type of reason - because the compiler needs to know the pointer to release the reference operation, take a few bytes, and the steps of each subscript should be taken. Several bytes.

. The array / pointer equivalence for parameters was done for efficiency All non_array data argument in C are passed "by value" (a copy of the argument is made and passed to the called function; the function can not change the value of the actual Variable Used as an Argument, Just The Value of The Copy It has.

The arranging arguments and pointers equivalent are considered for efficiency reasons. In the C language, all non-array forms of data refer to the value form (to actually participate in a copy and passed to the called function, the function cannot modify the value of the actual variable as the argument, but can only modify the delivery That copy to it).

Arrays and Pointers InterchangeAbility Summary:

1. An Array Access A [I] is always "Rewritten" or interpreted by the compiler as a pointer access * (A i);

2. Pointers are always pointers; they are never rewritten to arrays You can apply a subscript to a pointer;. You typically do this when the pointer is a function argument, and you know that you will be passing an array in.3 An. array declaration in the specific context (only) of a function parameter can equally be written as a pointer. An array that is a function argument (ie, in a call to the function) is always changed, by the compiler, to a pointer to The start of the array.

4. Therefore, you have the choice of defining a function parameter with is an array, either as an array or as a pointer. Whichever Way you define it, you actually get a pointer inside the function.

5. In All Other Cases, Definitions Should Match Declarations. If you define it as an array, you exten declaration shouth be an array. And likewise for a pointer.

Summary of array and pointer exchangeability

1. Accessing arrays in the form of A [i] Alternative to array is always "rewritten" or interprets pointers such as * (A i).

2. The pointer is always the pointer. It must not be rewritten into a number of groups. You can use the subscript form to access the pointer, usually as a function parameter, and you know that the actual passed to the function is an array.

3. In a particular context, it is also the parameter of the function (also only this), and a declaration of an array can be seen as a pointer. As an array of function parameters (that is, in a function call), it will always be modified by the compiler into a pointer to the first element of the array.

4. So, when an array is defined as a function of a function, you can choose to define it as an array, or it can be defined as a pointer. Regardless of which method is selected, it is a pointer within the fact that the function is in fact.

5. In all other cases, definitions and statements must match. If an array is defined, you must declare it as an array when declaring it, the pointer is also the case.

Notice how the initialization is identical to the arrays of characters initialization. Only string literals have this privilege of initializing pointer arrays. Arrays of pointers can not be directly initialized with non-string types.

Note that the initialization portion of the pointer array is the same as the initialization portion of the character "array". Only string constants can initialize the argument array. The pointer array cannot be directly initialized by the type of non-string.

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

New Post(0)