内存管理



//
// main.c // 内存管理 // // Created by zhangxueming on 15/6/5. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #include <stdio.h> #include "File.h" //内存管理 //.text: 存储二进制可执行程序 //.data: 初始化的全局变量及初始化的static关键字修饰的变量, 分为只读数据段及可读可写数据段, 只读数据段存储常量如: "helloworld" //.bss: 未初始化的全局变量及未初始化的static修饰的变量 //.heap: 堆内存, 手动申请, 手动释放 //.stack: 栈内存, 局部变量 //static 关键字 //1. 修饰局部变量,全局变量的生命周期, 局部变量的作用域 //2. 修饰全局变量,全局变量只能在当前定义的文件可以访问 //3. 修饰函数,函数只能在定义的文件内使用, 其它外部文件不可以调用 int main(int argc, const char * argv[]) { print_num(); print_num(); set_score(100); printf("score = %d\n", get_score()); print(); return 0; }
//
//  File.c
//  内存管理
//
//  Created by zhangxueming on 15/6/5.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#include "File.h"

static int score = 88;

void print_num(void)
{
    static int num = 100;//只有在该函数第一次调用的时候才会定义num变量
    for (int i=0; i<5; i++) {
        printf("num = %d\n", num++);
    }
}

void set_score(int value)
{
    score = value;
}

int get_score(void)
{
    return score;
}

//修饰函数

static void print_hello(void)
{
    printf("hello wolrd\n");
}

static void print_welcome(void)
{
    printf("welcome\n");
}

void print(void)
{
    print_hello();
    print_welcome();
}
//
//  File.h
//  内存管理
//
//  Created by zhangxueming on 15/6/5.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#ifndef ________File__
#define ________File__

#include <stdio.h>

void print_num(void);

void set_score(int value);
int get_score(void);

void print(void);

#endif /* defined(________File__) */

 

posted @ 2015-06-05 16:44  sirzhang  阅读(221)  评论(0编辑  收藏  举报
AmazingCounters.com