標籤:
2015.2.3
//記憶體
//.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\n", num);
num++;
printf("num = %d\n", num);
//printf("value = %d\n", 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\n", 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("\n");
// 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\n", 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. 不能釋放一個null 指標
//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\n", ptr);
// scanf("%s",ptr);
// printf("%s\n", ptr);
// int len = (int)strlen(ptr);
//
// ptr = realloc(ptr, 1000);
// printf("%p\n", ptr);
// scanf("%s", ptr+len);
// printf("%s\n", 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\n", 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\n",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\n",*((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\n",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\n", buf);
memcpy(&str[1], str, 20);
printf("%s\n",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\n", dst);
// int len = (int)strlen(src);
// memmove(src, &src[1], len-1);
// *(src+len-1) = ‘\0‘;
// printf("%s\n",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\n", pstr);
return 0;
}
iOS-C_Day12___記憶體