您的位置:首页 > 其它

20150606内存使用

2015-06-09 19:47 148 查看
//
// main.c
// IOS150608
//
// Created by
Peng Junlong on 15/6/8.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

//******************************
//* *
//* 内存使用 *
//* *
//******************************

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

//内存操作函数(堆内存)
//1.申请内存

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

//3.对申请到的内存进行初始化(可选)
//4.使用内存

//5.使用完之后释放内存

//将指向该内存的指针赋值为NULL

//int main(int argc, const char * argv[]) {
// char *pstr = (char *)malloc(100*sizeof(char));
// if(!pstr)
// {
// return -1;
// }
// for (int i=0; i<100; i++) {

// printf("%c",pstr[i]); //分配到的内存中的内容是随机的,可能是上个程序遗留下来的内存,故一般对指针进行初始化
// }
// printf("\n");
// memset(pstr,49,100);
// for (int i=0; i<100; i++) {

// printf("%c",pstr[i]); //分配到的内存中的内容是随机的,可能是上个程序遗留下来的内存,故一般对指针进行初始化
// }
// printf("\n");
// scanf("%s",pstr);
// printf("%s\n",pstr);
// free(pstr);
// pstr = NULL;
// return 0;
//}

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

//int main(int argc, const char *argv[])
//{
// int a[10] = {1,2,3,4,5,6,7,8,9,0};
// memset(a, 0, 40);
// for (int i=0; i<10; i++) {
// printf("%d ",a[i]);
// }
// return 0;
//}

//void *calloc(<#size_t#> count, <#size_t#> size);
//申请的内存空间大小等于count*size

//int main(int argc, const char *argv[])
//{
// void *my_calloc(size_t count, size_t size);
// int *pscore = calloc(10, sizeof(int));
// for (int i=0; i<10; i++) {
// scanf("%d",pscore+i);
// }
// for (int i=0; i<10; i++) {
// printf("%d ",*(pscore+i));
// }
// free(pscore);
// pscore = NULL;
//
// //=============================================
// int *ptscore = my_calloc(10, sizeof(int));
// for (int i=0; i<10; i++) {
// scanf("%d",ptscore+i);
// }
// for (int i=0; i<10; i++) {
// printf("%d ",*(ptscore+i));
// }
// free(ptscore);
// ptscore = NULL;
//
// return 0;
//}
//
////利用malloc 和memset实现calloc
//void *my_calloc(size_t count, size_t size)
//{
// void *pst = NULL;
// pst = malloc(count*size);
// if (!pst) {
// return NULL;
// }
//
// memset(pst, 0, count*size);
//
// return pst;
//}

//内存查找函数
//void*memchr(const void *dest, int c, size_t n); // memchr针对任意类型的内存块,strchr是针对字符串的

//dest:要查找的目的数据

//c:要查找的字符

//n:查找的内存大小,即查找的范围

//返回查找到的字符的地址,查找不到返回NULL

//int main(int argc, const char *argv[])
//{
// char str[100] = "Hello world";
// printf("%s\n",memchr(str, 'o', 100));
//
// int intdata[10] = {1,2,3,4,5,6,7,8,9,0};
// int *p = (int *)memchr(intdata, 9, 40);
// printf("%d\n",*p);
// p = (int *)memchr(intdata, 9, 30);
// printf("%s\n",(char *)p);
//
// return 0;
//}

//修改内存大小,重置内存大小
//realloc函数
//void*realloc(void *, size_t);
/*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 allo-cation, allocation,
cation, 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 allo-cated allocated
cated with calloc(3), realloc(3) does not guarantee that the additional memory is also zero-filled.
*/
//int main(int argc, const char *argv[])
//{
// char *pstr = (char *)malloc(50*sizeof(char));
// printf("Input:");
// scanf("%s",pstr);
// printf("%p\n",pstr);
// printf("%s\n",pstr);
//
// pstr = (char *)realloc(pstr, 1000);
// printf("%p\n",pstr);
// printf("%s\n",pstr);
//
// //realloc的特殊用法
// realloc(pstr, 0);//等价于free(pstr);同一块内存不能被释放两次
// pstr = NULL;
//
// char *ppstr = realloc(NULL, 100); // 等同于malloc(100);返回新分配的内存地址
// if (!ppstr) {
// return -1;
// }
// printf("Input:");
// scanf("%s",ppstr);
// printf("%p\n",ppstr);
// printf("%s\n",ppstr);
// realloc(ppstr, 0);
// printf("%p\n",ppstr);
// ppstr = NULL;
// printf("%p\n",ppstr);
//
// return 0;
//}

//内存比较函数,和数据类型没有关系
//int memcmp(const void *, const void *, size_t n);

//内存拷贝函数
//void*memcpy(void *dest, const void *src, size_t );

//dest和src指向的内存空间不能有重叠的部分

//内存移动函数
//void*memmove(void *dest, const void *src, size_t n);

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

//int main(int argc, const char *argv[])
//{
// char str1[100] = "HellWorld!";
// char str2[100] = "HelloWorld";
// char str3[100] = {};
// int ret = memcmp(str1, str2, 5);
// printf("%d\n",ret);
//
// memcpy(str1, &str1[5], 4);
// printf("%s\n",str1);
// memcpy(str3, str1, 5);
// printf("%s\n",str3);
//
// char str4[100] = "Hello world";
// memmove(str4, &str4[1], 8); //从str4[1]位置开始之后的8个字节移动,让str4指针指向&str[1]
// printf("str4 = %s\n",str4);
// return 0;
//}

//double atof(const char *);将数字字符串转换成整型数字
//int atoi(const char *);将数字字符串转换成浮点型
//int main(int arg, const char *argv[])
//{
// printf("%d\n",atoi(" 123abc"));
// printf("%f\n",atof(" 123.44abc"));
//
// return 0;
//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: