C language programming---memory management and command line parameters

Memory management

  • Memory management includes the header file <malloc.h> or <stdlib.h>
  • void *malloc() function, dynamically allocates memory. Specifies the memory size in bytes and returns a pointer to the allocated memory.
    void * Pointer of untyped type. C and C++ stipulate that the void * type can be cast to a pointer of any other type.
// 动态分配内存 4bytes
int* ptr = (int*)malloc(sizeof(int)); //动态数组只能存储一个整数?  No,可以自动动态扩展

// 动态分配内存,可以赋值任意大小的数据(自动扩展)
for (int i = 0; i < 5; i++) {
    
    
	ptr[i] = i;   // 自动扩展
}

for (int i = 0; i < 5; i++) {
    
    
	//输出动态数组的元素
	printf("数组的每个值:%d\n", ptr[i]);
}
  • void free() function releases memory.
// 释放内存
free(ptr)
  • The void* calloc() function dynamically allocates memory and initializes it to zero.
    Pass the number of memory blocks and the size of each memory block (in bytes), and return a pointer to the allocated memory.
// 使用方式类似malloc
char* ptr = (char*)calloc(5, sizeof(char)); //相当于5*sizeof(char)
  • void* realloc() function, reallocates memory.
    Accepts two arguments, a previously allocated pointer and a new memory size, and then resizes the previously allocated memory block. If the adjustment is successful, a pointer to the reallocated memory is returned, otherwise a null pointer is returned.
// 分配内存
int* ptr = (int*)malloc(5 * sizeof(int)); //整型的动态数组
//为ptr(指针)动态数组 重新分配内存
ptr = (int*) realloc(ptr, 30 * sizeof(int));
if(ptr){
    
    
	//重新分配成功;
}

Store data of any size:

char* ptr = (char*)malloc(3 * sizeof(char));

// 动态分配内存的指针,只能向其 拷贝 字符串数据;不能直接赋值
strcpy(ptr, "jack");  // (动态分配的内存)可以给任意大小的数据

strcat(ptr, " tom is cat after jack"); // 拼接

printf("result: %s\n", ptr);
  • sizeof operator, obtains the byte size of a data type or variable.

  • * Pointer operator, obtains the value of the memory address pointed to by the pointer.

  • & operator, obtains the memory address of a variable.

  • -> operator: used to access structure members by pointer, the syntax is pointer->member, which is equivalent to (*pointer).member.

  • The memcpy() function copies data from the source memory area to the target memory area. It accepts three parameters, namely a pointer to the target memory area, a pointer to the source memory area and the size of the data to be copied (in bytes).

  • The memmove() function is similar to the memcpy() function, but it can handle overlapping memory regions. It accepts three parameters, namely a pointer to the target memory area, a pointer to the source memory area and the size of the data to be copied (in bytes).

 

Command line parameters

  • lauf.exe param1 param2
    param1 param2 are command line parameters;
  • The command line parameters and the lauf.exe executable file name are passed together (in string form) main函数;
  • int main(int num, char* argv[ ]);
    num represents the number of parameters received;
    argv is the parameter name pointer array received;
int main(int num, char* argv[]){
    
    
	// num 表示收到的参数的个数
	if(num == 1){
    
    
		printf("没有其他参数:%s\n", argv[0]); // 只有一个可执行文件名
	}else if(num == 2){
    
    
		printf("一个命令参数:%s\n", argv[1]); // 表示第一个命令行参数
	}
}

Insert image description here
 

python calls C

Use python to call the main function in C in the previous step and pass the parameters;

  1. Compile C programs into (shared) dynamic link libraries;
gcc -fPIC lauf.c -shared -o lauf.dll

A lauf.dll dynamic link library will be generated

  1. Calling lauf.dll in python
from ctypes import c_int, c_char_p, windll

# 加载dll
c_module = windll.LoadLibrary("lauf.dll")

# 声明参数类型
c_module.main.argtypes = [c_int, c_char_p*3]
# 声明结果类型
c_module.main.restype = c_int

# 准备数据
num = c_int(2)
arr = c_char_p * 2  # 得到类型
# 类型实例化
arr_obj = arr() # 两个字符指针组成的数组对象
arr_obj[0] = c_char_p(b"param1")
arr_obj[1] = c_char_p(b"param2")

# 调用函数
c_module.main(num, arr_obj)

 
 
[Previous article]: C language programming—type conversion and error handling

Guess you like

Origin blog.csdn.net/weixin_45228198/article/details/131876516