1. char * name = malloc (20 );
Name = "abcdef ";
These two statements together will cause memory leakage, because the name first points to the heap and then points to the constant area.
2. All members of the shared object share a memory segment:
Union data {
Short int I;
Char ch;
} Share;
Int a = share. ch; represents the space occupied by the shared body in the (char) format. A. There is only one variable at any time. B. You cannot assign values when defining them. C. Shared body variables of the same type can be assigned a value as a whole. D. The shared body variable acts as the last Member to be stored.
Example:
[Root @ localhost test] # gcc 12_13_2.c
[Root @ localhost test] #./a. out
65
[Root @ localhost test] # cat 12_13_2.c
# Include <stdio. h>
Union data
{
Short int I;
Char ch;
} Share;
Int main ()
{
Int;
Share. I = 65 + 256;
A = share. ch; // indicates the space occupied by the shared body in the format of (char ).
Printf ("% d \ n", a); // The result is 65
}
3. Data is not initialized after malloc () is allocated. After calloc () is allocated, the data is initialized to zero. The pointer returned after realloc () allocation is not necessarily the original pointer.
4. Alignment of struct:
The elements in the structure have alignment characteristics (usually 4-byte alignment), and the structure itself also has alignment characteristics (alignment with the size of the element occupying the largest memory ). The length of the entire struct must be a multiple of the homogeneous alignment.
Two structs of the same type can be assigned an integral value, as shown in figure
Struct AnyOne a, B;
A = B; // This is acceptable.
5. Definition of NULL in C and C ++:
# Undef NULL
# If defined (_ cplusplus)
# Define NULL 0
# Else
# Define NULL (void *) 0)
# Endif
6. View heap allocation and release:
[Root @ localhost test] # valgrind -- tool = memcheck -- leak-check = full./a. out
7. umask function: obtains the inverse of the corresponding bits. If umask is set to 022, w permissions of the Group and other users are cleared.
8. Determine whether a variable is unsigned:
# Define IS_UNSIGNED (a) (a> = 0 )? (~ A> 0? 1: 0): 0)
9.
[Root @ localhost test] # cat 12_13_4.c
# Include <stdio. h>
# Include <string. h>
Void func (char str [50])
{
Printf ("% d, % d \ n", sizeof (str), strlen (str ));
}
Int main ()
{
Char s [50] = "abcdefght ";
Func (s );
}
[Root @ localhost test] # gcc 12_13_4.c
[Root @ localhost test] #./a. out
4, 9
[Root @ localhost test] #
Void func (char str [50]) here 50 does not make any sense. Sizeof (str) Here str represents the address of the first element of the array.
10.
[Root @ localhost test] # cat 12_13_5.c
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Int main ()
{
Char stra [] = "HelloWorld ";
Char * strb = stra;
Printf ("% d, % d \ n", sizeof (stra), sizeof (strb ++); // stra represents the entire array, 11 is calculated as '\ 0'
Printf ("% d, % d \ n", strlen (strb), strlen (strb ++); // calculate the parameter from right to left.
// Sizeof () is a keyword. The size has been calculated during pre-compilation without computation.
}
[Root @ localhost test] # gcc 12_13_5.c
[Root @ localhost test] #./a. out
11, 4
9, 10
[Root @ localhost test] #
11.
Function pointer: