Data Structures and Algorithms-----Pointers and Structures

 Table of contents

Preface

pointer

definition

Example 1 (access modified data):

Example 2 (wild pointer):

Example 3 (dynamic space allocation problem):

Example 4 (string assignment error problem):

Example 5 (space release problem):

Structure

definition

Structure pointer space allocation


Preface

        Today we will start learning data structures and algorithms. This is a new column I have opened. Next, I will share my personal insights and opinions through csdn while learning data structures and algorithms. I hope you will support me!

        The learning content of this period is pointers and structures in C language. Because pointer structures are one of the important components of C language and play an important role in later learning data structures and algorithms, we will start learning from pointer structures. We have learned the relevant knowledge of C language before, so today we will not go into too much in-depth text description. Below, I will go over these contents through examples.

pointer

definition

The address of data in memory is also called a pointer . If a variable stores a pointer to a copy of data, we call it a pointer variable .

In C language, a variable is allowed to store a pointer, which is called a pointer variable. The value of a pointer variable is the address of a certain piece of data. Such data can be an array, a string, a function, or another ordinary variable or pointer variable.

A pointer is a variable . Through the pointer, we can point to the memory address of a data, and then go to the data in the range. Through the pointer, we can operate the data.

The pointer variable itself occupies space , 4 bytes in 32-bit operating systems and 8 bytes in 64-bit operating systems.

//定义指针变量与定义普通变量非常类似,不过要在变量名前面加星号*,格式为:
datatype *name;

Example 1 (access modified data):

#include<stdio.h>
int main()
{
	int* a;
	int c=10;
	a = &c;
	printf("%d %d %d\n", *a, a, c);
	//结果:10 8912028 10
	*a = 15;
	printf("%d %d %d", *a,a,c);
	//结果:15 8912028 15
}

The result of the data is modified, but the address does not change. This is the basic operation of pointers - access and modify data.

Example 2 (wild pointer):

  1. Pointer is not initialized

  2. Pointer out-of-bounds access

  3. Release the space pointed to by the pointer

The above are common situations of wild pointers

#include<stdio.h>
int* test()
{
	int a = 10;
	printf("%p\n", &a);
	return &a;          
}
int main()
{
	int* p = test();
	return 0;
}
//程序开始后首先进入主函数,执行第一步,调用test函数将返回值赋给p, test函数的返回值是局部变量a的地址
//,假设a的地址为0x0093F830, 由于a只在test函数内有效,出了test函数其内存空间就被释放,
//也就意味着a的地址编号不存在,短时间内如果再次利用这块地址,它的值还未被改变也就是0x0093F830还存在,
//p的值为0x0093F830,但此时p为野指针,因为p里面所存放的地址是无效的。

Example 3 (dynamic space allocation problem):

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a = 15;
	int* p=(int *)malloc(sizeof(int));
	p = &a;
	free(p);
}

Obviously, the result will be an error, because the pointer p starts to point to a dynamic space, and the integer a is stored in an allocated space. When the pointer p is pointed to the address of a, free (p) The original space of p was not released, but the space of a was released. However, the space of a has been allocated and cannot be released. This will lead to two errors, one is a memory leak and the other is a space release error. .

Example 4 (string assignment error problem):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char a[10] ;
	char* p=(char *)malloc(sizeof(char)*10);
	char* q = (char*)malloc(sizeof(char) * 10);
//1
	strcpy(a, "hello");
	strcpy(p, "hello");
	strcpy(q, "hello");
//2
	a = "hello";
	p = "hello";
	q = "hello";
}

There are two ways of assignment above, but the first one is correct, and the second one is wrong assignment.

Example 5 (space release problem):

Frequently asked questions are as follows:

1. Forget to release, memory leak

2. Release the same space multiple times

3. Release non-dynamic space

4. Partial release

 partial release

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int* p = (int*)malloc(sizeof(int) * 10);
    p++;
    free(p); //这里的p的地址并不是起始地址,只是进行了部分的释放
    p = NULL;
}

multiple releases

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int* p = (int*)malloc(sizeof(int) * 10);
    int* q = (int*)malloc(sizeof(int) * 10);
    p = q;
    free(q);
    free(p);
}
//这里p指向了q的地址,下面释放了q,又去释放p,这就会造成多次释放

Structure

definition

C arrays allow you to define variables that can store data items of the same type. Structures are another user-defined data type available in C programming that allow you to store data items of different types.

The data members in the structure can be basic data types (such as int, float, char, etc.), or other structure types, pointer types, etc.

statement

struct 结构名{
//成员列表
};
typedef struct {
    //成员
}结构体名字;

In the future, it is recommended to use the second declaration method. This method makes it easier for us to name the structure and distinguish it easily.

Structure pointer space allocation

#include<stdio.h>
#include<stdlib.h>
typedef struct {
    char* a;
    int num;

}Student_data;
int main()
{
    Student_data* p=(Student_data*)malloc(sizeof(Student_data));
    p->a = (char*)malloc(sizeof(char) * 10);
}

A structure pointer is defined here. The implementation requires space allocation for this structure pointer. Secondly, there is a pointer inside that also needs to be allocated space again so that data can be stored.

The above is the entire content of this issue. It is mainly just a general overview. Later, we will officially enter the study of data structures and algorithms. See you in the next issue!

Share a wallpaper:

Supongo que te gusta

Origin blog.csdn.net/m0_73633088/article/details/132766850
Recomendado
Clasificación