栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C和指针 第11章 动态内存分配 11.11 编程练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C和指针 第11章 动态内存分配 11.11 编程练习

1. 请自己尝试编写calloc函数,函数内部使用malloc函数来获取内存? 
#include
#include
#include

void *calloc( size_t num_elements, size_t element_size );

int main( void ){
    size_t num_elements;
    size_t element_size;
    int *pi;
    int *pi2;
    int *pit;
    
    num_elements = 10;
    element_size = sizeof(int);
    pi = (int *)calloc( num_elements, element_size );
    pit = pi;
    pi2 = pit + num_elements;
    while( pit < pi2 ){
        printf( "%d ", *pit++ );
    }
     
    free( pi );
    
    return EXIT_SUCCESS;
}

void *calloc( size_t num_elements, size_t element_size ){
    void *new_memory;
    int i;
    
    
    new_memory = malloc( num_elements * element_size );
    
    if( !new_memory ){
        printf( "memory allocation fails.n" );
        return NULL;
    }
    
    memset( new_memory, 0, num_elements * element_size );
    
    return new_memory;
}
 
2. 编写一个函数,从标准输入读取一列整数,把这些值存储于一个动态分配的数组中并返回这个数组。函数通过观察EOF来判断输入列表是否结束。数组的第1个数是数组包含的值的个数,它的后面就是这些整数值。 
解析:
这个用于函数分配一个数组,并在需要时根据一个固定的增值对数组进行重新分配。增量DELTA可以进行微调,用于在效率和内存浪费之间进行一平衡。
 
#include
#include
#include

#define DELTA 100

int *readints( void ); 

int main( void ){
    int *pi;
    int i;
    
    pi = readints();
    for( i = 0; i <= pi[0]; ++i ){
        printf( "%d ", pi[i] );
    }
    printf( "n" );
    
    free( pi );

    return EXIT_SUCCESS;
}

int *readints( void ){
    int *array;
    int size;
    int count;
    int value;
    
    
    size = DELTA;
    array = (int *)malloc( (size + 1) * sizeof(int) );
    if( array == NULL ){
        return NULL;
    } 
    
     
    count = 0;
    while( scanf( "%d", &value ) == 1 ){
        
        count += 1;
        if( count > size ){
            size += DELTA;
            array = (int *)realloc( array, (size + 1) * sizeof(int) );
            if( array == NULL ){
                return NULL;
            }
        }
        array[count] = value; 
    }
    
     
    if( count < size ){
        array = (int *)realloc( array, (count + 1) * sizeof(int) );
        if( array == NULL ){
            return NULL;
        }
    }
    array[0] = count;
    return array; 
}

 

3. 编写一个函数,从标准输入读取一个字符串,把字符串复制到动态分配的内存中,并返回该字符串的副本。这个函数不应该对读入字符的长度做任何限制。 
#include
#include

char *getstr( void );

int main( void ){
    char *ps;
    
    ps = getstr();
    printf( "ps = %sn", ps );
    
    
    free( ps );

    return EXIT_SUCCESS;
}

char *getstr( void ){
    int ch;
    int size;
    int counter;
    
    
    size = 2;
    char *ps = (char *)malloc( size * sizeof(char) );
    if( !ps ){
        printf( "memory allocation fails.n" );
        return NULL;
    }
    *ps = '';
    counter = 0;
    while( (ch = getchar()) != EOF ){
        ps[counter] = ch; 
        counter++;
        if( counter == size - 1 ){
            size *= 2;
            
            ps = (char *)realloc( ps, size * sizeof(char) );    
            ps[counter] = '';
        } 
    }
    ps[counter] = '';
    return ps;
}
 

4. 编写一个程序,按照下图所示创建数据结构。最后3个对象都是动态分配的结构。第1个对象可能是一个静态的指向结构的指针。
不必使这个程序过于全面---我们将在下一章讨论这个数据结构。 

#include
#include

struct Node{
    int values;
    struct Node *next;
};
 
int main( void ){
    static struct Node *head;
    struct Node *p;
    struct Node *pnode, *pnode2, *pnode3;
    
    pnode = (struct Node *)malloc( sizeof(struct Node) );
    pnode2 = (struct Node *)malloc( sizeof(struct Node) );
    pnode3 = (struct Node *)malloc( sizeof(struct Node) );
    if( pnode && pnode2 && pnode3 ){
        head = pnode;
        pnode->values = 5;
        pnode->next = pnode2;
        pnode2->values = 10;
        pnode2->next = pnode3;
        pnode3->values = 15;
        pnode3->next = NULL;
    }
    p = head;
    while( p ){
        printf( "%d ", p->values );
        p = p->next;
    }
    printf( "n" );

    return EXIT_SUCCESS;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号