c-学习3
-目录
- 1 命令行参数
- 2 内存管理
#1 命令行参数
]#include <stdio.h>
void main(int argc, char *argv[]){
        printf("%d \n",argc);
        printf("%s\n",argv[0]);
        return;
}
gcc ./practice/commond_arg.c -o ./o/commond_arg
 ./o/commond_arg
#1
#./o/commond_arg
 ./o/commond_arg aaa
#2
#./o/commond_arg
其中 argv[1] 为 aaa
#2 内存管理
本章将讲解 C 中的动态内存管理.C 语言为内存的分配和管理提供了几个函数.这些函数可以在 <stdlib.h> 头文件中找到.
| 序号 | 函数和描述 | 描述 | 
|---|---|---|
| 1 | void *calloc(int num, int size); | 该函数分配一个带有 function allocates an array of num 个元素的数组,每个元素的大小为 size 字节。 | 
| 2 | void free(void *address); | 该函数释放 address 所指向的h内存块。 | 
| 3 | void *malloc(int num); | 该函数分配一个 num 字节的数组,并把它们进行初始化。 | 
| 4 | void *realloc(void *address, int newsize); | 该函数重新分配内存,把内存扩展到 newsize。 | 
cat ./practice/memory_dynamic.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
        char name[100];
        char *description;
        strcpy(name,"Michael Gzy");
        /*动态分布内存*/
        description = malloc(200*sizeof(char));
        if (description == NULL){
                fprintf(stderr,"error - unable to alloate required memory \n");
        }
        else{
                strcpy(description,"Michael gzy a DPS sutdent in class 10th \n");
        }
        printf("Name=%s\n",name);
        printf("Descripiton: %s\n",description);
}