C and C++ Interview Tips 3 (2) - References and Pointers

First, the application of pointer arrays and array pointers

Look at the code below.

#include <stdio.h>
#include <stdlib.h>

intmain()
{

	char *str[] = { "Welcome", "to", "Fortemedia", "Nanjing" }; //第一行
	char **p = str + 1; // second line
	str[0] = (*p++) + 2; //third line
	
	str[1] = *(p + 1); //fourth line
	
	str[2] = p[1] + 3; // fifth line

	str[3] = p[0] + (str[2] - str[1]); // sixth line

	printf("%s\n", str[0]);
	printf("%s\n", str[1]);
	printf("%s\n", str[2]);
	printf("%s\n", str[3]);

	return 0;
}

The first line defines an array of pointers, each element is a pointer.

The second line defines a secondary pointer that points p to to 

At the end of the third line, p points to Fortemedia, and str[0] points to the next element of Nanjiing, so at this time *str[] becomes

*str[] = {"","to", "Fortemedia", "Nanjing"}

When the fourth line is executed, the position of p does not move, p + 1 points to Nanjing, and then assigns it to str[1]; then it becomes

*str[] = {"", "Nanjing", "Fortemedia", "Nanjing"}

In the fifth line, p[1] still points to Nanjing, p[1]+3 points to jing, and assigns it to str[2], then it becomes

*str[] = {"", "Nanjing", "jing", "Nanjing"}

When the sixth line is reached, p[0] points to jing, and the difference between the addresses of str[2] and str[1] is three, so str[2]- str[1] = 3, and then p[0] adds 3 points to 'g'. finally became

*str[] = {"", "Nanjing", "jing", "g"}
Second, the difference between function pointers and pointer functions

A pointer function is a function that returns a pointer type

int *func();

This is a pointer function whose return type is an int pointer.

A function pointer is a pointer to a function

int (*p)(int a);

p is a pointer to a parameter that has an int type and the return type is int.

3. What is a wild pointer

Wild pointers are pointers to "garbage" memory.

Reasons for the formation of wild pointers:

1. The pointer variable is not initialized

2. The pointer is not set to NULL after it is released by free or delete.

3. The operation of the pointer goes beyond the scope of the variable.

[Note]: We must pay attention to avoid the appearance of wild pointers when writing code.

Four, several ways of memory allocation

1. Allocate from static storage area . The memory is allocated when the program is compiled , and this memory exists during the entire runtime of the program , such as global variables

2. Create on the stack . When a function is executed , the storage units of local variables in the function can be created on the stack, and these storage units are automatically released when the function execution ends .

3. Allocate from the heap (dynamic memory allocation) . When the program is running, use malloc or new to apply for any amount of memory, and the programmer is responsible for when to use free or delete to release the memory. The life cycle of dynamic memory is determined by the programmer and is very flexible to use.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325183969&siteId=291194637