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

例277:分配内存函数alloca()

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

void some_function(size_t size)
{
    int i;

    char *pointer;

    char stack_fix[1];
    stack_fix[0] = NULL;

    // 在调用 alloca的函数返回的时候, 它分配的内存会自动释放
    if ((pointer = alloca(size)) == NULL)
        printf("Error allocating %u bytes from the stack\n", size);
    else
    {
        for (i = 0; i < size; i++)
            pointer[i] = i;
        printf("Allocatd and used a buffer of %u bytes\n", size);
    }
}

int main(int argc, char const *argv[])
{
    some_function(1000);    // Allocatd and used a buffer of 1000 bytes
    some_function(32000);   // Allocatd and used a buffer of 32000 bytes
    some_function(65000);   // Allocatd and used a buffer of 65000 bytes
    return 0;
}

例278:字符串条件复制函数memccpy()

#include <stdio.h>
#include <mem.h>

int main(int argc, char const *argv[])
{
    char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char target[27];
    char *result;

    // void *memccpy(void *dest, void *src, unsigned char c, unsigned int count);
    // 由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符c则停止复制。
    // 返回值:如果c没有被复制,则返回NULL,否则,返回字符c 后面紧挨一个字符位置的指针
    result = memccpy(target, alphabet, 'K', sizeof(alphabet));

    // 将target的k后置为NULL
    if (result)
        *result = NULL;

    printf(target); // 输出:ABCDEFGHIJK

    return 0;
}

例279:字符串条件比较函数memcmp()和memicmp()

#include <stdio.h>
#include <mem.h>

int main(int argc, char const *argv[])
{
    char *a = "AAA";
    char *b = "BBB";
    char *c = "aaa";

    // int memcmp(const void *str1, const void *str2, size_t n));
    // 比较str1和st2的前n个字符大小
    // 如果返回值 < 0,则表示 str1 小于 str2
    // 如果返回值 > 0,则表示 str1 大于 str2
    // 如果返回值 = 0,则表示 str1 等于 str2
    printf("Comparing %s and %s with memcmp %d\n", a, b, memcmp(a, b, sizeof(a)));      // Comparing AAA and BBB with memcmp -1
    
    // int memicmp(void *buf1, void *buf2, unsigned int count);
    // 比较内存区域buf1和buf2的前count个字节但不区分字母的大小写
    // 比较结果对应同memcmp一样
    printf("Comparing %s and %s with memicmp %d\n", a, c, memicmp(a, c, sizeof(a)));    // Comparing AAA and aaa with memicmp 0

    return 0;
}

例280:字符串条件复制函数memmove()

#include <stdio.h>
#include <mem.h>

int main(int argc, char const *argv[])
{
    float values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
    float empty[5];
    int i;

    // void *memmove( void* dest, const void* src, size_t count);
    // 由src所指内存区域复制count个字节到dest所指内存区域
    memmove(empty, values, sizeof(values));

    for (i = 0; i < 5; i++)
        printf("%3.1f ", empty[i]);

    return 0;
}

例281:相邻字符交换函数swab()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mem.h>

int main(int argc, char const *argv[])
{
    char *source = "AJSM'A S0110T pi!s";
    char target[64];

    // void *memset(void *s, int ch, size_t n);
    // 将s中当前位置后面的n个字节(typedef unsigned int size_t )用 ch 替换并返回 s 
    // 将target的所有内存初始化为NULL
    memset(target, NULL, sizeof(target));  

    // swab交换字符串中相邻两个字节
    swab(source, target, strlen(source));

    printf("Source: %s Target %s\n", source, target);   // Source: AJSM'A S0110T pi!s Target JAMSA'S 1001 Tips!

    return 0;
}

例282:动态分配内存函数malloc()

#include <stdio.h>
// #include <alloc.h> TC可用
#include <stdlib.h>

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

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

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

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

    return 0;
}

例283:测试可申请内存的大小

#include <stdio.h>
// #include <alloc.h>   TC可用
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    char *one, *two, *three;

    if ((one = (char *) malloc(30000)) == NULL)
        printf("Error allocating array one\n");
    else if ((two = (char *) malloc(30000)) == NULL)
        printf("Error allocating array two\n");
    else if ((three = (char *) malloc(30000)) == NULL)
        printf("Error allocating array three\n");
    else
        printf("All arrays successfully allocated\n");

    return 0;
}

例284:malloc()和realloc()配合使用

#include <stdio.h>
//#include <alloc.h>	TC可用
#include <stdlib.h>

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

    if ((string = (char *) malloc(100)))
    {
        printf("Successfully allocated a 100 byte string\n");
        // 使用realloc重新分配内存
        if ((new_string = (char *) realloc(string, 1000)))
            printf("String size increased to 1000\n");
        else
            printf("Error reallocating the string\n");
    }
    else
        printf("Error allocating the 100 byte string\n");

    return 0;
}

例285:测试堆内存是否正常(TC环境)

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

int main(int argc, char const *argv[])
{
    char *buffer, *second_buffer;
    int i, state;

    buffer = malloc(100);
    second_buffer = malloc(100);

    state = heapcheck();

    if (state == _HEAPOK)
        printf("Heap is ok\n");
    else if (state == _HEAPCORRUPT)
        printf("Heap is corrupt\n");


    for (i = 0; i <= 100; i++)
        buffer[i] = i;


    state = heapcheck();

    if (state == _HEAPOK)
        printf("Heap is ok\n");
    else if (state == _HEAPCORRUPT)
        printf("Heap is corrupt\n");
    return 0;
}

例286:使用free函数释放申请的内存

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

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

    int i;

    if ((int_values = malloc(100 * sizeof(int))) == NULL)
        printf("Error allocating the array\n");
    else
    {
        for (i = 0; i < 100; i++)
            int_values[i] = i;

        for (i = 0; i < 100; i++)
            printf("%d ", int_values[i]);

        free(int_values);
    }
    return 0;
}

例287:扫描获得堆下一个项目信息函数heapwalk()

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

int main(int argc, char const *argv[])
{
    char *buffer1, *buffer2, *buffer3;

    struct heapinfo node = { NULL, 0, 0};

    buffer1 = malloc(100);
    buffer2 = malloc(200);
    buffer3 = malloc(300);
    free(buffer2);

    while (heapwalk(&node) == _HEAPOK)
        printf("Size %u bytes State %s\n", node.size, (node.in_use) ? "In use": "Free");
    
    return 0;
}

例288:内存操作函数heapchecknode()

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

int main(int argc, char const *argv[])
{
    char *buffer, *second_buffer;
    int i, state;

    buffer = malloc(100);
    second_buffer = malloc(100);

    state = heapchecknode(buffer);

    if (state == _USEDENTRY)
        printf("buffer is ok\n");
    else
        printf("buffer is not ok\n");

    state = heapchecknode(second_buffer);

    if (state == _USEDENTRY)
        printf("second_buffer is ok\n");
    else
        printf("second_buffer is not ok\n");

    for (i = 0; i <= 100; i++)
        buffer[i] = i;

    state = heapchecknode(buffer);

    if (state == _USEDENTRY)
        printf("buffer is ok\n");
    else
        printf("buffer is not ok\n");

    state = heapchecknode(second_buffer);

    if (state == _USEDENTRY)
        printf("second_buffer is ok\n");

    printf("second_buffer is not ok\n");

    return 0;
}

例289:内存操作函数heapfillfree()

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

int main(int argc, char const *argv[])
{
    char *buffer1, *buffer2, *buffer3;
    int i, state;

    buffer1 = malloc(100);
    buffer2 = malloc(200);
    buffer3 = malloc(300);
    free(buffer2);          // Free space in the middle

    state = heapfillfree('A');

    if (state == _HEAPOK)
        printf("Heap is ok\n");
    else if (state == _HEAPCORRUPT)
        printf("Heap is corrupt\n");


    for (i = 0; i <= 150; i++)
        buffer1[i] = i;


    state = heapcheckfree('A');

    if (state == _HEAPOK)
        printf("Heap is ok\n");
    else if (state == _HEAPCORRUPT)
        printf("Heap is corrupt\n");
    else if (state == _BADVALUE)
        printf("Value has been changed in free space\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值