iOS开发之c语言基础-动态内存分配与管理

//

//  main.m

//  C10- 动态内存分配

//

//  Created by dllo on 15/10/19.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "arrayoperation.h"


int i = 0;


void test(void)

{

    for(i = 0; i < 6 ;i++) {

        printf("$");

    }

}




//char  *test1(void)

//{

//    char str[] = "qingchun";

//    return str;     //报错的原因是局部变量在函数结束的时候释放空间

//}

int main(int argc, const char * argv[]) {

//    student a[5] = {

//    {"yuhais", 'm', 23, 97},

//    {"yuhais", 'm', 23, 98},

//    {"yuhdais", 'm', 23, 93},

//    {"uhais", 'm', 23, 99},

//    {"yhais", 'm', 43, 95}

//    };

//    printstudent(a, 5);

//    sortstudent(a, 5);

//    printstudent(a, 5);

//    for (i = 0; i < 5; i++) {

//        if (i >= 3) {

//            test();

//        }

//        printf("*");

//    }

//

    

    

//    int  a = 0;

//    printf("栈区:%p ",&a);

//    

//    int *p = malloc(sizeof(int));

//    printf("堆区:%p ", p);

//    

//    static int b = 0;

//    printf("静态区%p ",&b);

//    

//     char *q = "qingchun";

//    printf("常量区:%p ",q);

//    

    

//     printf("%c ", q[1]);

//    printf("%s ", &q[1]);

    

//    char *q = "qingchun";

//    q[1] = '0';

//报错 因为qingchun在常量区,常量区只可以读不可以改

    

//    char str[] = "qingchun";

//    char *m = str;

//    m[1] = '0';

    //先将iqngchun复制给数组,在栈区,指针在栈区,可以操作没有报错

    

    

   // printf("代码区:%p ", test);

    

    

    //堆内存分配函数

//    void *ret = malloc(开辟空间的大小(单位字节))

//    功能: 在堆区开辟指定大小的空间

//    返回值: 返回开辟的空间的首地址

//    参数 : 指定开辟空间的大小, 单位字节

    

    

  //  在栈区开辟一个int类型的空间

    

 //

    

  //  int  a = 0;

 //   在堆区开辟一个int类型的空间

    

    //int *p = malloc(sizeof(4));

//    int *p1 = malloc(sizeof(int));//并不关心什么类型,只关心有多大.不同类型指针接受的意义不同

//    *p1 = 3;

//    free(p1);

    //注意!!!堆内存的空间不会自动的释放,需要手动释放,手动释放函数为free()

//    有一个malloc()就要配对一个free函数

//    free(需要释放空间的首地址)

    

    //守护程序,一直都运行

    

    //int arr[5]

//    int *p = malloc(sizeof(int) * 5);

//    p[1] = 3;

//    *(p + 2) = 4;

    //int  arr[5]  另一种方式(拓展)

//    int *p[5] = {NULL};

//    for (int i = 0; i < 5; i++) {

//        p[i] = malloc(sizeof(int));

//    }

//注意

  //  *(p[1]) = 3;

    

    //返回的空间当做什么类型变量用,取决于用什么类型指针取指向

    //int a

//    int  *p2 = malloc(sizeof(int));

    //char c[4]

//    char *p3 = malloc(sizeof(int));

//    

    

    //有一个字符串,其中包含数字,提取其中的数字,要求动态分配保存

    //提示:先计算出有几个谁,然后根据数字的个数来开辟空间

//   char str[] = "qing2Ch23un3";

    

    //先记录字符串有多少数字

//    char *p4 = str;

//    int count = 0;

//    for (int i = 0; i < strlen(str); i++) {

//        if (p4[i] >= '0' && p4[i] <= '9') {

//            count++;

//        }

//    }

//    

//    printf("%d ", count);

    //再去按照记录结果动态开辟空间

    //char  *p5 = malloc(sizeof(char) * (count + 1));

    

    //注意,需要为结束符预留一个空间,所以count+ 1

    

    //最后将数字存入新开劈的空间

 //   int n = 0;

  //  char *head = p5;

    //暂存空间首地址,防止p移动后找不回来

//    for (int i = 0; str[i] != ''; i++) {

//        if (str[i] >= '0' && str[i] <= '9' ) {

//            *p5  = str[i];

//            p5++;

//            //*p++ = str[i];

//        }

//    }

    //不要忘记添加

//    *p5 = '';

//    printf("%s ", head);

//    free(head);

//    p5 = NULL;

//    head = NULL;

    //printf("%s ", head);放在free下面也可运行出结果,但是错误,指针都是野指针,删除只是标记删除,数据还在..只是可以被其他所利用了,要是没有人用,先前的数据还在.先前的指针还在,也要free.不然通过指针还是可以访问到数据

    

    

    

        //输入3个单词,动态分配内存保存单词,并在最后输出

    //提示,定义一个指针数组保存数据  char *words[3] = {0};


//    char str1[20] = {0};

//    char *words[3] = {0};

//    for (int i = 0; i < 3; i++) {

//        scanf("%s", str1);

        //输入字符串,存到str

   //     long len  = strlen(str1);

        //计算字符串长度,

    //    words[i] = malloc(sizeof(char) * (len + 1));

        //开辟空间,注意用不同的指针指向

   //     strcpy(words[i], str1);

       

        

  //  }

    

    //输出打印

//    for (int i = 0; i < 3; i++) {

//        printf("%s ", words[i]);

//        free(words[i]);

//        words[i] = NULL;

//    }

    

    //分配n个大小size大小的空间,并且把该内存上的所有字节清0

  //  void *calloc(unsigned n, unsigned size);

    

   // 按照给定的地址以及给定的大小重新分配

 //   void *realloc(void *p, unsigned newSize);

    

    

    //内存操作函数

    //s指向的内存开始初始化n个字节的内容为c

    //void *memset(void *s, int c, size_t n)

   // int *p = malloc(sizeof(int) * 4);

   // p = memset(p, 0, sizeof(int) * 4);

    //等效与上两句

   // int *q = calloc(4, sizeof(int));

    

  ///////  今天晚上再次调查strcpy(char *, <#const char *#>)用么么memset(<#void *#>, <#int#>, <#size_t#>)来写

    

    

    

    //source 指向的内存开始拷贝到dest,拷贝到dest, 拷贝n个字节

//void *memcpy(void *dest, const void*source, size_t n)

    

//比较buf1buf2指向的内存是否相同,,比较count个字节

  //  int memcmp(const void *buf1, const void *buf2, unsigned int count)

    


//    1. (**)输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值。

//    

//    2. (***)已知一个数组20个元素(随机1100之间包含1100),求大于平均数的元素个数,并动态生成一个新数组保存(提示:malloc20个元素保存)

//    

//    3. (****)有一段文本,将文本中的所有单词,存放到一个字符串数组中。(要求占用内存最少)

//

    //整行数组

//    int  a = 0;

//    int max = 0;

//    scanf("%d ", &a);

//    int *p = malloc(sizeof(int) * a);

//    int *p1 = p;

//    for (int i = 0; i < a; i++) {

//        *p = arc4random() % (90 - 40 + 1) + 40;

//        if (*p > max ) {

//            max = *p;

//        }

//        printf("%d ", *p);

//        p++;

//        

//    }

//    printf("%d ",max);

//    free(p1);

//    p = NULL;

//    p1 = NULL;

//    

    //字符数组

    int  a = 0;

    int max = 0;

    scanf("%d ", &a);

    char *p = malloc(sizeof(char) * (a + 1));

    char *p1 = p;

    for (int i = 0; i < a; i++) {

        *p = arc4random() % (90 - 40 + 1) + 40;

        if (*p > max ) {

            max = *p;

        }

        printf("%d ", *p);

        p++;

        

    }

    *p = '';

    printf("%d %s",max, p1);

    free(p1);

    p = NULL;

    p1 = NULL;


   return 0;

}

//

//  arrayoperation.m

//  C10- 动态内存分配

//

//  Created by dllo on 15/10/19.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "arrayoperation.h"


void printstudent(student *p, int count)

{

    for (int i = 0; i < count ; i++) {

        printf("%s %c %d %d ",p[i].name, (p + i)->sex, (*(p + i)).number, p[i].score);

        printf(" ");

    }

}

void sortstudent(student *p, int count)

{

    for (int i = 0; i < count - 1 ; i++) {

        for (int j = 0; j < count - 1 - i; j++) {

            if (p[j].score > p[j + 1].score) {

                student b = p[j];

                p[j] = p[j + 1];

                p[j + 1] = b;

            }

        }

    }

}

//

//  arrayoperation.h

//  C10- 动态内存分配

//

//  Created by dllo on 15/10/19.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>


typedef struct student {

    char name[20];

    char sex;

    int number;

    int score;

}student;

void printstudent(student *p, int count);

void sortstudent(student *p, int count);




原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043132.html