c语言深入理解<3>

/***********************************
//Exanple 1
#include<stdio.h>
void display(char *);//指针做形参
void main()//如果没有void,默认为int类型,可能出现错误
{
	char s[]="abcd";
	display(s);
	system("pause");
}
void display(char *s)
{
	int i=0;
	for(i=0;i<4;i++)
	printf("%c\n",*(s+i));//地址加1,并不是按字节移动,而是按类型移动。
	//如果指针指向int类型,s+1移动2个或4个字节(根据系统不同而不同)
}
************************************
//Example2
#include<stdio.h>
void main()//如果没有void,默认为int类型,可能出现错误
{
	int i=10,j=010;
	printf("%d,%d\n",i,j);//如果int类型以0开始,编译器会将其作为8进制处理
	//输出为:10,8
	system("pause");
}
*************************************/
#include<stdio.h>
#include <malloc.h>
// 全局变量定义
int iGlobalInt1=0;
int iGlobalInt2=0;
int iGlobalInt3=0;
// 全局常量定义
const int iGlobalConstInt1=1;
const int iGlobalConstInt2=5;
const int iGlobalConstInt3=6;
// 全局静态变量定义
static int iGlobalStaticInt1=0;
static int iGlobalStaticInt2=0;
static int iGlobalStaticInt3=0;

void funcParamTest(int iFuncParam1,int iFuncParam2,int iFuncParam3)
	{
	// 函数私有变量定义
	int iLocalInt1=iFuncParam1;
	int iLocalInt2=iFuncParam2;
	int iLocalInt3=iFuncParam3;
	printf(" 函数参数变量内存地址 \n");
	printf("iFuncParam1= 0x%08x\n ",&iFuncParam1);
	printf("iFuncParam2=0x%08x\n",&iFuncParam2);
	printf("iFuncParam3=0x%08x\n\n",&iFuncParam3);
	printf(" 函数本地变量的内存地址 \n");
	printf("iLocalInt1=0x%08x\n",&iLocalInt1);
	printf("iLocalInt2=0x%08x\n",&iLocalInt2);
	printf("iLocalInt3=0x%08x\n\n",&iLocalInt3);
}
void main()//如果没有void,默认为int类型,可能出现错误
{// 局部静态变量
	static int iStaticInt1=0;
	static int iStaticInt2=0;
	static int iStaticInt3=0;
	// 局部静态常量定义
	const static int iConstStaticInt1=0;
	const static int iConstStaticInt2=0;
	const static int iConstStaticInt3=0;
	// 局部常量
	const int iConstInt1=1;
	const int iConstInt2=5;
	const int iConstInt3=6;
	// 局部变量
	int iLocalInt1=0;
	int iLocalInt2=0;
	int iLocalInt3=0;
	char * pMalloc1,*pMalloc2,*pMalloc3;
	char * pNew1,*pNew2,*pNew3;
	printf(" 全局常量的内存地址 \n");
	printf("iGlobalConstInt1=0x%08x\n",&iGlobalConstInt1);
	printf("iGlobalConstInt2=0x%08x\n",&iGlobalConstInt2);
	printf("iGlobalConstInt3=0x%08x\n\n",&iGlobalConstInt3);
	printf("iConstStaticInt1=0x%08x\n",&iConstStaticInt1);
	printf("iConstStaticInt2=0x%08x\n",&iConstStaticInt2);
	printf("iConstStaticInt3=0x%08x\n\n",&iConstStaticInt3);
	printf(" 全局变量的内存地址 \n");
	printf("iGlobalInt1=0x%08x\n",&iGlobalInt1);
	printf("iGlobalInt2=0x%08x\n",&iGlobalInt2);
	printf("iGlobalInt3=0x%08x\n\n",&iGlobalInt3);
	printf(" 静态变量的内存地址 \n");
	printf("iGlobalStaticInt1=0x%08x\n",&iGlobalStaticInt1);
	printf("iGlobalStaticInt2=0x%08x\n",&iGlobalStaticInt2);
	printf("iGlobalStaticInt3=0x%08x\n\n",&iGlobalStaticInt3);
	printf("iStaticInt1=0x%08x\n",&iStaticInt1);
	printf("iStaticInt2=0x%08x\n",&iStaticInt2);
	printf("iStaticInt3=0x%08x\n\n",&iStaticInt3);
	printf(" 本地变量的内存地址 \n");
	printf("iConstInt1=0x%08x\n",&iConstInt1);
	printf("iConstInt2=0x%08x\n",&iConstInt2);
	printf("iConstInt3=0x%08x\n\n",&iConstInt3);
	printf("iLocalInt1=0x%08x\n",&iLocalInt1);
	printf("iLocalInt2=0x%08x\n",&iLocalInt2);
	printf("iLocalInt3=0x%08x\n\n",&iLocalInt3);
	funcParamTest(iLocalInt1,iLocalInt2,iLocalInt3);
	// 在堆上分配内存,使用 new
	//pNew1=new char[16];
	//pNew1=new char[16];
	//pNew2=new char[16];
	//pNew3=new char[16];
	// 在堆上分配内存,使用 malloc
	pMalloc1 = (char *)malloc( 16 );
	pMalloc2 = (char *)malloc( 16 );
	pMalloc3 = (char *)malloc( 16 );
	printf(" 在堆上分配内存内存地址 \n");
	printf("pMalloc1=0x%08x\n",pMalloc1);
	printf("pMalloc2=0x%08x\n",pMalloc2);
	printf("pMalloc3=0x%08x\n\n",pMalloc3);
	// 释放 new 分配的内存空间
//	delete [] pNew1;
//	delete [] pNew2;
//	delete [] pNew3;
//	pNew1=NULL;
//	pNew2=NULL;
//	pNew3=NULL;
	// 释放 malloc 分配的内存空间
	free(pMalloc1);
	free(pMalloc2);
	free(pMalloc3);
	pMalloc1=NULL;
	pMalloc2=NULL;
	pMalloc3=NULL;
	system("pause");
}
//可以很清楚的看出内存分配

参考:c陷阱与缺陷

c语言内存管理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值