经典编程900例(c语言)(第二十四篇)

本文展示了C/C++中不同类型的内存分配函数,如`farmalloc`、`malloc`、`calloc`和`halloc`的用法,并通过示例解释了如何检查未使用内存的大小。此外,还对比了`printf`、`cprintf`、`puts`和`cputs`的运行时间,探讨了它们在大量输出时的效率差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例290:在远堆内存中分配内存

#include <stdio.h>
#include <alloc.h>

int main(int argc, char const *argv[])
{
    char far *string;
    int far *int_values;
    float far *float_values;

	// farmalloc函数从远堆中分配存储块
    if ((string = (char *) farmalloc(50)))
        printf("Successfully allocated a 50 byte string\n");
    else
        printf("Error allocating string\n");

    if ((int_values = (int *) farmalloc(100 * sizeof(int))) != NULL)
        printf("Successfully allocated int_values[100]\n");
    else 
        printf("Error allocating int_values[100]\n");

    if ((float_values = (float *) farmalloc(25 * sizeof(float))) != NULL)
        printf("Successfully allocated float_values[25]\n");
    else
        printf("Error allocating float_values[25]\n");

    return 0;
}

例291:对堆内存的管理

#include <stdio.h>
#include <alloc.h>

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

    // coreleft返回未使用内存的大小
    printf("Starting heap available %u\n", coreleft());

    ptr = malloc(1);

    // brk改变数据段空间分配 - 多分配512字节
    if (brk(ptr + 512) == 0)
        printf("Ending heap available %u\n", coreleft());

    return 0;
}

例292:返回未使用内存的大小coreleft()

#include <stdio.h>
#include <alloc.h>

int main(int argc, char const *argv[])
{
#if defined(__SMALL__)
    unsigned result;
#else
    long result;
#endif

    result = coreleft();

    printf("The amount of available memory is %dKb\n", result / 1024);
    return 0;
}

例293:内存分配函数calloc()

#include <stdio.h>
#include <malloc.h>
// #include <alloc.h> tc可用

/**
 * 原型:void* calloc(unsigned int num,unsigned int size);
 * 功能:在内存的动态存储区中分配num个长度为size的连续空间;
 * 注意:num:对象个数,size:对象占据的内存字节数,相较于malloc函数,calloc函数会自动将内存初始化为0
 */

int main(int argc, char const *argv[])
{
    char *string;
    int *int_values;
    float *float_values;

    if ((string = (char *) calloc(50, sizeof(char))))
        printf("Successfully allocated a 50 byte string\n");
    else
        printf("Error allocating string\n");

    if ((int_values = (int *) calloc(100, sizeof(int))) != NULL)
        printf("Successfully allocated int_values[100]\n");
    else
        printf("Error allocating int_values[100]\n");

    if ((float_values = (float *) calloc(25, sizeof(float))) != NULL)
        printf("Successfully allocated float_values[25]\n");
    else
        printf("Error allocating float_values[25]\n");
    return 0;
}

例294:远地址内存分配halloc()

#include <stdio.h>
#include <malloc.h>

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

    int huge *big_array;

    if ((big_array = (int huge *) halloc (100000L, sizeof(long int))) == NULL)
        printf ("Error allocating huge array\n");
    else
    {
        printf("Filling the array\n");

        for (i = 0; i < 100000L; i++)
            big_array[i] = i % 32768;

        for (i = 0; i < 100000L; i++)
            printf ("%d ", big_array[i]);

        hfree(big_array);
    }
    return 0;
}

例295:使用getchar()和putchar()来输出用户输入的字符串

#include <stdio.h>

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

    do {
        // getchar()从缓冲区读取一个字符
        letter = getchar();

        // putchar()输出字符
        putchar(letter);
    } while (letter != '\n');

    return 0;
}

例296:设置不同背景色的文本输出

#include <conio.h>

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

    for (color = 0; color < 8; color++)
    {
    	// 设置文本背景颜色
        textbackground(color);
        
        // 控制台输出
        cprintf("This is color %d\r\n", color);
        cprintf("Press any key to continue\r\n");
        getch();
    }
    return 0;
}

例297:字符串获取函数cgets()

#include <stdio.h>
#include <conio.h>

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

    buffer[0] = 253;

    printf("Type in a string and press Enter\n");
    // cgets(buffer); (在VC++6.0下为_cgets)
    _cgets(buffer); 

    printf("\n\nThe number of characters read was %d\n", buffer[1]);

    printf("The string read: %s\n", &buffer[2]);
    return 0;
}

例298:清屏函数

#include <conio.h>

int main(int argc, char const *argv[])
{
    // clrscr();    只有在Turbo C中可以运行!
    system("cls");  // vc中 #include <stdlib.h> 
    return 0;
}

例299:printf()和cpintf()的运行时间比较

#include <stdio.h>
#include <conio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    int count;
    time_t start_time, stop_time;

    // 计算输出1000次的时间
    time(&start_time);
    for (count = 0; count < 1001; count++)
        printf("Jamsa's 1001 C/C++ Tips\n");
    time(&stop_time);
    printf("\n\nTime required for printf %d seconds\n", stop_time-start_time);

    // 按任意键继续
    printf("Press any key...\n");
    getch();

    // 计算输出1000次的时间
    time(&start_time);
    for (count = 0; count < 1001; count++)
        cprintf("Jamsa's 1001 C/C++ Tips\r\n");
    time(&stop_time);

    printf("\n\nTime required for cprintf %d seconds\n", stop_time-start_time);
    return 0;
}

例300:puts()和cputs()的运行时间比较

#include <stdio.h>
#include <conio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    int count;
    time_t start_time, stop_time;

    // 计算输出1000次的时间
    time(&start_time);
    for (count = 0; count < 1001; count++)
        puts("Jamsa's 1001 C/C++ Tips");
    time(&stop_time);

    printf("\n\nTime required for puts %d seconds\n", stop_time-start_time);

    // 按任意键继续
    printf("Press any key...\n");
    getch();

    // 计算输出1000次的时间
    time(&start_time);
    for (count = 0; count < 1001; count++)
        cputs("Jamsa's 1001 C/C++ Tips\r\n");
    time(&stop_time);

    printf("\n\nTime required for cputs %d seconds\n", stop_time-start_time);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值