C和指针:第十一章:动态内存分配

第十一章:动态内存分配

  1. void *malloc(size_t size)
  2. void calloc(szie_t num_elements, size_t element_size)
  3. void realloc(void *ptr,size_t new_size)
  4. void free(void *ptr)
  5. 常见错误:忘记检查是否分配成功 ;操作内存超出分配内存的边界
  6. 定义如下不易发生错误的内存分配器,分别在文件alloc.h alloc.c test.c
#ifndef CPOINTER_ALLOC_H
#define CPOINTER_ALLOC_H

#include <stdlib.h>
#define malloc //禁止直接调用malloc
#define MALLOC(num,type) (type *) alloc((num) * sizeof(type))
extern  void *alloc(size_t size);

#endif //CPOINTER_ALLOC_H

#include <stdio.h>
#include "alloc.h"
#undef malloc

void * alloc(size_t size)
{
    void *new_mem;
    //分配内存并检查是否成功
    new_mem = malloc(size);
    if(new_mem == NULL)
    {
        printf("Out of memory! \n");
        exit(1);
    }
    return new_mem;

}
#include "alloc.h"

int main(void )
{
    int *new_memory;
    new_memory = MALLOC(25,int);
}
  1. 动态内存分配实例

    /*Example 1:读取、排序和打印一列整型值。*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /*比较整型值*/
    int compare_integers (void const * a, void const * b)
    {
        register int const *pa = a;
        register int const *pb = b;
        return *pa > *pb ? 1: *pa < *pb - 1 ? -1 : 0;
    }
    
    int main(void)
    {
        int *array;
        int n_values;
        int i;
        
        /*询问有多少个数*/
        printf("How many values are here?");
        if (scanf("%d", &n_values) != 1 || n_values <= 0)
        {
            printf("Illegal number of values.\n");
            exit(EXIT_FAILURE);
        }
    
        /*分配内存*/
        array = malloc(n_values * sizeof(int));
        if(array == NULL)
        {
            printf("Can't get memory for that many values");
            exit(EXIT_FAILURE);
        }
    
        /*读入数据*/
        for( i = 0; i < n_values; i++)
        {
            printf("? ");
            if (scanf("%d",array + i) != 1)
            {
                printf("Error in reading value #%d\n",i);
                free(array);
                exit(EXIT_FAILURE);
            }
        }
    
        /*调用排序*/
        qsort(array, n_values, sizeof(int), compare_integers);
    
        /*打印输出*/
        for( i=0; i < n_values; i += 1)
            printf("%d\n",array[i]);
    
        free(array);
        return EXIT_SUCCESS;
    
        return 0;
    }
    
    /*
    EXample 2:
    用动态分配内存制作一个字符串的一份拷贝。
    *注意:调用程序应该负责检查这块枫存是否成功分配!
    *这样做允许调用程序以任何它所希望的方式对错误作出反应。
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    char *strdup( char const *string)
    {
        char *new_string;
        /*+1是为了考虑结尾的NUL字节*/
        new_string = malloc(strlen(string) + 1);
    
        if ( new_string != NULL)
            strcpy(new_string, string);
    
        return new_string;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ColaForced

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值