1.//整數轉換成字符串itoa函數的實(shí)現
#include <iostream>
using namespace std;
void itoaTest(int num,char str[] )
{
int sign = num,i = 0,j = 0;
int num1;
char temp[11];
if(sign<0)//判斷是否是一個(gè)負數
{
num = -num;
}
do
{
num1 = num;
temp[i] = num1+'0';
num/=10;
i++;
}while(num>0);
if(sign<0)
{
temp[i++] = '-';
}
temp[i] = '\0';
i--;
while(i>=0)
{
str[j++] = temp[i--];
}
str[j] = '\0';
}
2. //字符串轉換成整數atoi函數的實(shí)現
int atoiTest(char s[])
{
int i = 0,sum = 0,sign; //輸入的數前面可能還有空格或制表符應加判斷
while(' '==s[i]||'\t'==s[i])
{
i++;
}
sign = ('-'==s[i])?-1:1;
if('-'==s[i]||'+'==s[i])
{
i++;
}
while(s[i]!='\0')
{
sum = s[i]-'0'+sum*10;
i++;
}
return sign*sum;
}
3.//字符串拷貝函數
#include "stdafx.h"
#include <assert.h>
#include <string.h>
#include <iostream>
using namespace std;
char *srcpy(char *dest,const char *source)
{
assert((dest!=NULL)&&(source!=NULL));
char *address = dest;
while(*source!='\0')
{
*dest++=*source++;
}
*dest = '\0';
return address;
}
4.//判斷輸入的是否是一個(gè)回文字符串
#include "stdafx.h"
#include <string.h>
#include <iostream>
using namespace std;
//方法一:借助數組
bool isPalindrome(char *input)
{
char s[100];
strcpy(s,input);
int length = strlen(input);
int begin = 0,end = length-1;
while(begin<end)
{
if(s[begin]==s[end])
{
begin++;
end--;
}
else
{
break;
}
}
if(begin<end)
{
return false;
}
else
{
return true;
}
}
//方法二:使用指針
bool isPalindrome2(char *input)
{
if(input==NULL)
return false;
char *begin = input;
char *end = begin+strlen(input)-1;
while(begin<end)
{
if(*begin++!=*end--)
return false;
}
return true;
}
int main(int argc, char* argv[])
{
char *s ="1234554321";
if(isPalindrome(s))
{
cout<<"True"<<endl;
}
else
{
cout<<"Fasle"<<endl;
}
if(isPalindrome2(s))
{
cout<<"True"<<endl;
}
else
{
cout<<"Fasle"<<endl;
}
cin.get();
return 0;
}
5.//不使用庫函數,編寫(xiě)函數int strcmp(char *source, char *dest),若相等返回0,否則返回-1
int strcmp(char *source, char *dest)
{
assert(source != NULL && dest != NULL);
while(*source++==*dest++)
{
if(*source=='\0'&&*dest=='\0')
return 0;
}
return -1;
}
malloc()是C語(yǔ)言中動(dòng)態(tài)存儲管理的一組標準庫函數之一。其作用是在內存的動(dòng)態(tài)存儲區中分配一個(gè)長(cháng)度為size的連續空間。其參數是一個(gè)無(wú)符號整形數,返回值是一個(gè)指向所分配的連續存儲域的起始地址的指針
malloc()工作機制
malloc函數的實(shí)質(zhì)體現在,它有一個(gè)將可用的內存塊連接為一個(gè)長(cháng)長(cháng)的列表的所謂空閑鏈表。調用malloc函數時(shí),它沿連接表尋找一個(gè)大到足以滿(mǎn)足用戶(hù)請求所需要的內存塊。然后,將該內存塊一分為二(一塊的大小與用戶(hù)請求的大小相等,另一塊的大小就是剩下的字節)。接下來(lái),將分配給用戶(hù)的那塊內存傳給用戶(hù),并將剩下的那塊(如果有的話(huà))返回到連接表上。調用free函數時(shí),它將用戶(hù)釋放的內存塊連接到空閑鏈上。到最后,空閑鏈會(huì )被切成很多的小內存片段,如果這時(shí)用戶(hù)申請一個(gè)大的內存片段,那么空閑鏈上可能沒(méi)有可以滿(mǎn)足用戶(hù)要求的片段了。于是,malloc函數請求延時(shí),并開(kāi)始在空閑鏈上翻箱倒柜地檢查各內存片段,對它們進(jìn)行整理,將相鄰的小空閑塊合并成較大的內存塊。
malloc()在操作系統中的實(shí)現
在 C 程序中,多次使用malloc () 和 free()。不過(guò),您可能沒(méi)有用一些時(shí)間去思考它們在您的操作系統中是如何實(shí)現的。本節將向您展示 malloc 和 free 的一個(gè)最簡(jiǎn)化實(shí)現的代碼,來(lái)幫助說(shuō)明管理內存時(shí)都涉及到了哪些事情。
在大部分操作系統中,內存分配由以下兩個(gè)簡(jiǎn)單的函數來(lái)處理:
void *malloc (long numbytes):該函數負責分配 numbytes 大小的內存,并返回指向第一個(gè)字節的指針。
void free(void *firstbyte):如果給定一個(gè)由先前的 malloc 返回的指針,那么該函數會(huì )將分配的空間歸還給進(jìn)程的“空閑空間”。
malloc_init 將是初始化內存分配程序的函數。它要完成以下三件事:將分配程序標識為已經(jīng)初始化,找到系統中最后一個(gè)有效內存地址,然后建立起指向我們管理的內存的指針。這三個(gè)變量都是全局變量:
//清單 1. 我們的簡(jiǎn)單分配程序的全局變量
int has_initialized = 0;
void *managed_memory_start;
void *last_valid_address;
如前所述,被映射的內存的邊界(最后一個(gè)有效地址)常被稱(chēng)為系統中斷點(diǎn)或者 當前中斷點(diǎn)。在很多 UNIX? 系統中,為了指出當前系統中斷點(diǎn),必須使用 sbrk(0) 函數。 sbrk 根據參數中給出的字節數移動(dòng)當前系統中斷點(diǎn),然后返回新的系統中斷點(diǎn)。使用參數 0 只是返回當前中斷點(diǎn)。這里是我們的 malloc 初始化代碼,它將找到當前中斷點(diǎn)并初始化我們的變量:
清單 2. 分配程序初始化函數
#include
void malloc_init()
{
last_valid_address = sbrk(0);
managed_memory_start = last_valid_address;
has_initialized = 1;
}
現在,為了完全地管理內存,我們需要能夠追蹤要分配和回收哪些內存。在對內存塊進(jìn)行了 free 調用之后,我們需要做的是諸如將它們標記為未被使用的等事情,并且,在調用 malloc 時(shí),我們要能夠定位未被使用的內存塊。因此, malloc 返回的每塊內存的起始處首先要有這個(gè)結構:
//清單 3. 內存控制塊結構定義
struct mem_control_block {
int is_available;
int size;
};
現在,您可能會(huì )認為當程序調用 malloc 時(shí)這會(huì )引發(fā)問(wèn)題 —— 它們如何知道這個(gè)結構?答案是它們不必知道;在返回指針之前,我們會(huì )將其移動(dòng)到這個(gè)結構之后,把它隱藏起來(lái)。這使得返回的指針指向沒(méi)有用于任何其他用途的內存。那樣,從調用程序的角度來(lái)看,它們所得到的全部是空閑的、開(kāi)放的內存。然后,當通過(guò) free() 將該指針傳遞回來(lái)時(shí),我們只需要倒退幾個(gè)內存字節就可以再次找到這個(gè)結構。
在討論分配內存之前,我們將先討論釋放,因為它更簡(jiǎn)單。為了釋放內存,我們必須要做的惟一一件事情就是,獲得我們給出的指針,回退 sizeof(struct mem_control_block) 個(gè)字節,并將其標記為可用的。這里是對應的代碼:
清單 4. 解除分配函數
void free(void *firstbyte) {
struct mem_control_block *mcb;
mcb = firstbyte - sizeof(struct mem_control_block);
mcb->is_available = 1;
return;
}
如您所見(jiàn),在這個(gè)分配程序中,內存的釋放使用了一個(gè)非常簡(jiǎn)單的機制,在固定時(shí)間內完成內存釋放。分配內存稍微困難一些。我們主要使用連接的指針遍歷內存來(lái)尋找開(kāi)放的內存塊。這里是代碼:
//清單 6. 主分配程序
void *malloc(long numbytes) {
void *current_location;
struct mem_control_block *current_location_mcb;
void *memory_location;
if(! has_initialized) {
malloc_init();
}
numbytes = numbytes + sizeof(struct mem_control_block);
memory_location = 0;
current_location = managed_memory_start;
while(current_location != last_valid_address)
{
current_location_mcb =
(struct mem_control_block *)current_location;
if(current_location_mcb->is_available)
{
if(current_location_mcb->size >= numbytes)
{
current_location_mcb->is_available = 0;
memory_location = current_location;
break;
}
}
current_location = current_location +
current_location_mcb->size;
}
if(! memory_location)
{
sbrk(numbytes);
memory_location = last_valid_address;
last_valid_address = last_valid_address + numbytes;
current_location_mcb = memory_location;
current_location_mcb->is_available = 0;
current_location_mcb->size = numbytes;
}
memory_location = memory_location + sizeof(struct mem_control_block);
return memory_location;
}
這就是我們的內存管理器?,F在,我們只需要構建它,并在程序中使用它即可.多次調用malloc()后空閑內存被切成很多的小內存片段,這就使得用戶(hù)在申請內存使用時(shí),由于找不到足夠大的內存空間,malloc()需要進(jìn)行內存整理,使得函數的性能越來(lái)越低。聰明的程序員通過(guò)總是分配大小為2的冪的內存塊,而最大限度地降低潛在的malloc性能喪失。也就是說(shuō),所分配的內存塊大小為4字節、8字節、16字節、18446744073709551616字節,等等。這樣做最大限度地減少了進(jìn)入空閑鏈的怪異片段(各種尺寸的小片段都有)的數量。盡管看起來(lái)這好像浪費了空間,但也容易看出浪費的空間永遠不會(huì )超過(guò)50%。