1. "Exit ()"
Exit is to force the program to quit at the call, and the program ends once. Exit (0) indicates a normal exit. Exit (1) indicates an exception exit, and this 1 is returned to the operating system. Whether it's written in the main function or in other functions, the program exits. Generally think that 0 is normal exit, other numbers are abnormal exit. The header file is stdlib.h. The meaning of the return value is actually the same as returning in the main function, 0 is normal, and Non-zero represents an exception.
2. "Memset"
The declaration of the function is: memset (void *, int, size_t N). The function is to set the contents of the first n bytes in a piece of memory pointed to by void * (size specified by size_t, which can be computed using the sizeof function) to int value. This function typically initializes the new application's memory. And if it's in the list, when we apply for a new node, we also use this function to initialize the node. The sample code is as follows:
#include "stdio.h"
#include "string.h"
int main (int argc,const char* argv[]) {
char str[] = "ABCDE";
printf ("%s\n", str);
memset (str, 0, strlen (str));
printf ("%s\n", str);
return 0;
}
Print the results as follows:
。
You can see that the array is actually empty, instead of placing the elements in the array to 0.
#include "stdio.h"
#include "string.h"
int main (int argc,const char* argv[]) {
char str[] = "ABCDE";
printf ("%s\n", str);
memset (str, ' 0 ', strlen (str));
printf ("%s\n", str);
return 0;
}
Print the results as follows:
。
In this case, replace the elements in the array with the character ' 0 '.
"3.malloc"
Need to include header file stdlib.h. function declared as void *malloc (int size). malloc allocates a size byte of memory space to the system request. The return type is a void * type. Void* represents a pointer to an indeterminate type, that is, a pointer to this block of memory. can be cast to any other type of pointer, and an error will be made if no type conversion occurs. malloc is only allocating memory, and does not initialize memory, so a new memory is obtained, the value will be random. If the allocation fails, an empty pointer (NULL) is returned. Lack of space is one of the reasons for the failure of allocation. Note that after you have applied for memory space, you must check to see if the assignment was successful.
The sample code is as follows:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main (int argc,const char* argv[]) {
char *p;
p = (char *) malloc (sizeof (char));
if (p)
printf ("Memory allocation Address: 0x%x\n", p);
else
printf ("Allocate memory failed!\n");
Free (p);
return 0;
}
Print the results as follows:
。
In the above code I'm actually just assigning a char character space. If you want n characters, it is directly as follows: (char *) malloc (n * sizeof (char)).
"4.sizeof"
The number of bytes in the calculated space. Data types and variables can be evaluated. In fact, the size of the sizeof compute object is also converted to the calculation of its object type. In other words, the sizeof values of different objects of the same type are consistent. The sample code is as follows:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main (int argc,const char* argv[]) {
int A[10];
Because each int takes 4 bytes of
printf ("Array bytes:%d\n", sizeof (a));
printf ("char occupied bytes:%d,int bytes:%d,double occupied bytes:%d\n", sizeof (char), sizeof (int), sizeof (double));
int *p;
You can see that here a pointer occupies 8 bytes
printf ("pointer occupies bytes:%d\n", sizeof (p));
Char *str1 = "ABCDE";
This print is also a pointer, note that char is the smallest data type we can program to
printf ("String 1 bytes:%d\n", sizeof (STR1));
Char str2[] = "IIII";
Here print the length of the array
printf ("String 2 occupies bytes:%d\n", sizeof (STR2));
return 0;
}
Print the results as follows:
。
"5.free"
With the malloc function mentioned earlier, the pairing must refer to the free () function, and free is included in the Stdlib.h header file. The function is to release the memory space allocated by the malloc to the pointer variable. Note that the use of this pointer variable must be pointed back to null, to prevent the presence of wild pointers, effectively avoid misoperation. For free (p) This statement, if itself p is a null pointer, then free to p no matter how many times there will be no problem, if the p is not a null pointer, then free to p continuous operation two times will cause the program to run wrong. malloc and free should be paired use, if the application is not released after the memory leak, if the release is not done nothing.
What is free in the end. Free frees the memory that the pointer points to, noting that memory is released, not pointers. The pointer is not freed, and the pointer still points to the original storage space. The pointer is a variable that is destroyed only when the program ends. After freeing up the memory space, the original pointer to this memory still exists, except that the content that the pointer is pointing to is now indeterminate. That's what I said above. To set the pointer to null.
"6.assert"
In fact, assert assertions are not functions, but macros. But in the way it's used like a function, so it's also sorted here. When writing code, make assumptions that the assertion is used to capture these hypotheses, or it can be considered an exception handling. It is generally a conditional expression that terminates program execution if the condition returns an error. Assertions can generally be enabled at test time, and assertions are disabled at deployment time.