zoukankan      html  css  js  c++  java
  • 内存

     

    //内存

    //.text: 可执行程序

    //.data: 分为只读数据段, 以及可读可写数据段, 只读数据段存放常量如:"hello world" 可读可以数据段存初始化的全局变量及static关键字修饰的变量

    //.bss: 未初始化的全局变量及static修饰的变量

    //.heap: 也叫堆内存,需要手动申请,手动释放,堆空间比较大,运行效率低于栈内存

    //.stack: 也叫栈区,存放局部变量,系统自动释放内存,内存比较小,运行效率最高

    //static

    //1.修饰全局变量,只在当前.c文件内有效

    //2.修饰局部变量,作用域是局部的变量的作用域,但是存储在.data区,只有在第一次调用的使用开辟内存

    //3.修饰函数,这个函数只能在当前.c文件内使用

    #include "Static.h"

    static int num=100;

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

    {

        printf("num = %d ", num);

        num++;

        printf("num = %d ", num);

        

        //printf("value = %d ", value);

        print();

        changeValue(300);

        print();

        

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

            printScore();

        }

        //printHello();

        hello() ;

        return 0;

    }

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    //malloc

    //void *memset(void *, int, size_t);

    //void *malloc(size_t);

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

    //    //1.申请内存

    //    char *pstr = (char *)malloc(100*sizeof(char));//有可能内存申请失败,内存申请失败返回NULL;

    //    //int *score = malloc(50*sizeof(int));

    //    //2.判断内存是否申请成功

    //    if(!pstr)

    //    {

    //        return -1;

    //    }

    //    //3.申请到的内存值是未定义的,需要给这块内存赋初值

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

    ////        printf("%c", *(pstr+i));

    ////    }

    //    //

    //    memset(pstr,0,100);//把内存清0

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

    ////        printf("%d", *(pstr+i));

    ////    }

    //    //4.使用内存,

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

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

    //    //5.释放内存,告诉操作系统,当前这块内存可以被其它程序使用

    //    free(pstr);

    //    //6.指针该内存的指针变量赋值为NULL

    //    pstr = NULL;

    //    

    //    return 0;

    //}

    //calloc

    //void *calloc(size_t count, size_t size);

    //1.内存大小 = count * size

    //2.申请到的内存被初始化成0

    //The calloc() function contiguously allocates enough space for count objects that are size bytes of memory each and returns a pointer to the allocated memory.  The allocated memory is filled with bytes of value zero.

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

    //{

    //    int *score = (int *)calloc(10, sizeof(int));

    //    if (!score) {

    //        return -1;

    //    }

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

    //        scanf("%d", &score[i]);

    //    }

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

    //        printf("%d ", score[i]);

    //    }

    //    printf(" ");

    //    free(score);

    //    score = NULL;

    //    return 0;

    //}

    //利用malloc 跟memset 实现my_calloc

    //void *my_calloc(size_t count, size_t size)

    //{

    //    void *p = malloc(count*size);

    //    if (!p) {

    //        return NULL;

    //    }

    //    memset(p, 0, count*size);

    //    return p;

    //}

    //

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

    //{

    //    char *pstr = my_calloc(100, sizeof(char));

    //    if (!pstr) {

    //        return -1;

    //    }

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

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

    //    free(pstr);

    //    pstr = NULL;

    //

    //    return 0;

    //}

    //free()

    // The free() function deallocates the memory allocation pointed to by ptr. If ptr is a NULL pointer, no operation is performed.

    //1. 不能释放一个空指针

    //2. 同一块内存不能释放多次

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

    //{

    //    char *ptr = (char *)malloc(100*sizeof(char));

    //    if (!ptr) {

    //        return -1;

    //    }

    //    free(ptr);

    //    //free(ptr);

    //    ptr = NULL;

    //    return 0;

    //}

    //realloc

    //void *realloc(void *ptr, size_t size);

    //realloc(NULL , 100) <==> malloc(100)

    //realloc(ptr, 0)  <==> free(ptr)

    //如果ptr指针指向的内存空间足够扩展新的内存,ptr指向的内存地址不变,否则,重新在堆内存中开辟新的内存块,把原来内存块的内容复制一份到新的内存块中, 并释放原来的内存块,扩展的内存不一定初值为0

    //The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr.  If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.  If ptr is NULL,realloc() is identical to a call to malloc() for size bytes.  If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed.  When extending a region  allocated with calloc(3), realloc(3) does not guarantee that the additional memory is also zero-filled.

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

    //{

    //    char *ptr = malloc(10*sizeof(char));

    //    if (!ptr) {

    //        return -1;

    //    }

    //    printf("%p ",  ptr);

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

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

    //    int len = (int)strlen(ptr);

    //    

    //    ptr = realloc(ptr, 1000);

    //    printf("%p ",  ptr);

    //    scanf("%s", ptr+len);

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

    //    

    //    free(ptr);

    //    ptr = NULL;

    //    return 0;

    //}

    //realloc(NULL , 100) <==> malloc(100)

    //realloc(ptr, 0)  <==> free(ptr)

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

    //{

    //    char *ptr = (char *)realloc(NULL, 100*sizeof(char));//malloc(100)

    //    if (!ptr) {

    //        return -1;

    //    }

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

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

    //    realloc(ptr, 0);//<==> free(ptr)

    //    ptr = NULL;

    //    return 0;

    //}

    //memchr

    //void *memchr(const void *s, int c, size_t n);

    //s:要查找的内存地址

    //c:要查找的字符

    //n:查找内存块的大小

    //返回字符在内存中第一次出现的地址,找不到返回NULL

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

    {

    //    char *pstr = (char *)malloc(100*sizeof(char));

    //    if (!pstr) {

    //        return -1;

    //    }

    //    memset(pstr, 0, 100);

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

    //    printf("%s ",memchr(pstr, 'A', 100));

    //    free(pstr);

    //    pstr = NULL;

        

        int *pint = malloc(100*sizeof(int));

        if (!pint) {

            return -1;

        }

        memset(pint, 0, 100*sizeof(int));

        

        pint[10]=97;

        

        printf("%c ",*((char *)memchr(pint, 'a',100*sizeof(int))));

        

        

        return 0;

    }

    */

    //比较两个内存块的大小

    //int memcmp(const void *s1, const void *s2, size_t n);

    //s1,s2分别指向两块内存

    //n:比较内存的字节数

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

    //{

    //    int a[5]={1,2,-1,4,5};

    //    int b[5]={1,2,3,4,5};

    //    

    //    int ret = memcmp(a, b, 20);

    //    printf("ret = %d ",ret);

    //    

    //    return 0;

    //}

    //void *memcpy(void *dst, const void *src, size_t n);

    //The memcpy() function copies n bytes from memory area src to memory area dst.  If dst and src overlap,behavior is undefined.  Applications in which dst and src might overlap should use memmove(3) instead

    //把src指向的内存空间的值复制n个字节到dst指针指向的内存空间,dst 跟 src 指向的内存空间不能有重叠;

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

    {

        //char str[100]="helloworldqianfengjiaoyu";

        char *str=malloc(100*sizeof(char));

        strcpy(str, "helloworldqianfengjiaoyu");

        char buf[100]={};

        memcpy(buf, str, 10);

        printf("%s ", buf);

        

        memcpy(&str[1], str, 20);

        

        printf("%s ",str);

        return 0;

    }*/

    //void *memmove(void *dst, const void *src, size_t n);

    //把src指向内存空间的值拷贝n个字节到dst 指向的内存中

    //dst 与 src指向的内存空间可以重叠

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

    //{

    //    char dst[100]={};

    //    char src[100]="qianfengjiaoyu";

    //    src[20]='A';

    //    memmove(dst, src, 21);

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

    //    int len = (int)strlen(src);

    //    memmove(src, &src[1], len-1);

    //    *(src+len-1) = '';

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

    //    

    //    return 0;

    //}

    //void *memmem(const void *big, size_t big_len, const void *little, size_t little_len)//C89标准中没有这个函数

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

    {

        char big[100]="hellowordlqianfengjiaoyu";

        char little[20]="feng";

        char *pstr = NULL;

        pstr = memmem(big, 100, little, 3);

        printf("%s ", pstr);

        return 0;

    }

    让明天,不后悔今天的所作所为
  • 相关阅读:
    鲲鹏服务器测试
    缓存区溢出实验
    读书笔记
    《信息安全系统设计与实现》学习笔记9
    改进ls的实现
    团队作业(四):描述设计
    《需求规格书》修订版
    反汇编测试
    《信息安全系统设计与实现》学习笔记8
    stat命令的实现-mystat
  • 原文地址:https://www.cnblogs.com/-yun/p/4270880.html
Copyright © 2011-2022 走看看