五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

對(duì)malloc、指針、free的理解

2022-06-23 01:34 作者:鳧水億  | 我要投稿

malloc函數(shù)聲明如下

void* malloc (size_t size);

這是原文解釋?zhuān)?/span>

Allocate memory block

Allocates a block of?size?bytes of memory, returning a pointer to the beginning of the block.
The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
If?size?is zero, the return value depends on the particular library implementation (it may or may not be a?null pointer), but the returned pointer shall not be dereferenced.

Parameters

????size

?????????Size of the memory block, in bytes.

????????size_t?is an unsigned integral type.

Return Value

????On success, a pointer to the memory block allocated by the function.
????The type of this pointer is always?
void*, which can be cast to the desired type of data ????pointer in order to be ????dereferenceable.
????If the function failed to allocate the requested block of memory, a?null pointer?is returned

Example

/* malloc example: random string generator*/
#include <stdio.h> ? ? ?/* printf, scanf, NULL */
#include <stdlib.h> ? ? /* malloc, free, rand */

int main ()
{
 ?int i,n;
 ?char * buffer;

 ?printf ("How long do you want the string? ");
 ?scanf ("%d", &i);

 ?buffer = (char*) malloc (i+1);
 ?if (buffer==NULL) exit (1);

 ?for (n=0; n<i; n++)
 ? ?buffer[n]=rand()%26+'a';
 ?buffer[i]='\0';

 ?printf ("Random string: %s\n",buffer);
 ?free (buffer);

 ?return 0;
}

This program generates a string of the length specified by the user and fills it with alphabetic characters. The possible length of this string is only limited by the amount of memory available to?malloc

Data races

Only the storage referenced by the returned pointer is modified. No other storage locations are accessed by the call.
If the function reuses the same unit of storage released by a?deallocation function?(such as?free?or?realloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.

Exceptions (C++)

No-throw guarantee:?this function never throws exceptions.


up主將其翻譯如下:

函數(shù)聲明:

void* malloc (size_t size);

分配內(nèi)存塊

分配以字節(jié)為單位的內(nèi)存塊,返回指向該內(nèi)存塊的首地址。

分配新的內(nèi)存塊尚未初始化,殘存不確定的值。

如果大小為0,則返回值為特殊庫(kù)實(shí)現(xiàn)(可能也可能不是空指針(null pointer)),返回的指針將不是廢棄的。

參數(shù)

內(nèi)存塊大小,單位為字節(jié)。

size_t為無(wú)符號(hào)整型。

返回值

分配內(nèi)存成功:返回函數(shù)分配的內(nèi)存塊的首地址,該指針類(lèi)型為void *,可強(qiáng)制轉(zhuǎn)換為期望的數(shù)據(jù)類(lèi)型。指針可被釋放。

分配內(nèi)存失?。悍祷乜罩羔?null pointer)。

舉例

/* malloc example: random string generator*/
#include <stdio.h> ? ? ?/* printf, scanf, NULL */
#include <stdlib.h> ? ? /* malloc, free, rand */

int main ()
{
 ?int i,n;
 ?char * buffer;

 ?printf ("How long do you want the string? ");
 ?scanf ("%d", &i);

 ?buffer = (char*) malloc (i+1);
 ?if (buffer==NULL) exit (1);

 ?for (n=0; n<i; n++)
 ? ?buffer[n]=rand()%26+'a';
 ?buffer[i]='\0';

 ?printf ("Random string: %s\n",buffer);
 ?free (buffer);

 ?return 0;
}

此程序由使用者指定字符串的長(zhǎng)度,以字母數(shù)字符填充。字符串的長(zhǎng)度僅被分配內(nèi)存塊的長(zhǎng)度決定。

數(shù)據(jù)競(jìng)爭(zhēng)

Only(adv) the(定冠詞) storage(adv)?referenced(v) by the returned(v) pointer(n) is(v) modified(v).

->Only ?the?return?pointer?referenced?storage??is modified.

只有返回指針引用的存儲(chǔ)會(huì)被修改 ,調(diào)用不訪問(wèn)其他存儲(chǔ)位置。?如果函數(shù)重用由回收函數(shù)(如free或realloc)釋放的相同存儲(chǔ)單元,那么函數(shù)將以這樣一種方式同步,即回收完全發(fā)生在下一次分配之前。 (up水平有限,這一段太難翻譯了)

異常

不保證拋出異常:函數(shù)永不拋出異常

舉例

/* malloc example: random string generator*/
#include <stdio.h> ? ? ?/* printf, scanf, NULL */
#include <stdlib.h> ? ? /* malloc, free, rand */

int main ()
{
 ?int i,n;
 ?char * buffer;

 ?printf ("How long do you want the string? ");
 ?scanf ("%d", &i);

 ?buffer = (char*) malloc (i+1);
 ?if (buffer==NULL) exit (1);

 ?for (n=0; n<i; n++)
 ? ?buffer[n]=rand()%26+'a';
 ?buffer[i]='\0';

 ?printf ("Random string: %s\n",buffer);
 ?free (buffer);

 ?return 0;
}

指針與malloc

閱讀原文得出:malloc()會(huì)分配以字節(jié)為單位的內(nèi)存塊,單位為字節(jié),但分配的內(nèi)存塊尚未初始化,內(nèi)存塊經(jīng)由free()函數(shù)釋放,但這遠(yuǎn)遠(yuǎn)不夠,查找資料后得出gcc與msvc實(shí)現(xiàn)有些許差異,up使用的Mingw-x64,malloc()會(huì)在堆上分配內(nèi)存,手動(dòng)通過(guò)free釋放。

使用int *指針?lè)峙渑c釋放內(nèi)存


C語(yǔ)言的變量名、函數(shù)名本質(zhì)上是 “符號(hào)” ,在鏈接的階段將符號(hào)與各個(gè)目標(biāo)文件對(duì)應(yīng)的地址鏈接起來(lái)。

當(dāng)使用指針指向分配的內(nèi)存塊時(shí),malloc()不是給指針變量分配內(nèi)存空間,而是分配一個(gè)內(nèi)存塊,供開(kāi)發(fā)者使用。指針在函數(shù)調(diào)用中,指向了該內(nèi)存塊之外,此時(shí)的針不是指向被分配的內(nèi)存塊,如果使用free()釋放指針指向的內(nèi)存,讀寫(xiě)權(quán)限不夠,將發(fā)生段錯(cuò)誤(segment?fault)。

分配但不適用內(nèi)存塊

malloc與free總是成對(duì)出現(xiàn),malloc在堆上分配內(nèi)存,free釋放malloc分配的內(nèi)存。

free無(wú)法釋放malloc分配以外的內(nèi)存會(huì)觸發(fā)段錯(cuò)誤(segment fault)。


對(duì)malloc、指針、free的理解的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
武安市| 兰西县| 乃东县| 资溪县| 凌云县| 舒兰市| 饶平县| 富源县| 青铜峡市| 云霄县| 江油市| 林周县| 朝阳区| 辽中县| 滦平县| 大余县| 兴山县| 荆州市| 黑龙江省| 龙游县| 海兴县| 合阳县| 水富县| 普定县| 浏阳市| 内黄县| 翁牛特旗| 黔东| 西峡县| 明星| 甘南县| 景德镇市| 盖州市| 江安县| 临猗县| 安达市| 金阳县| 林口县| 安陆市| 柳江县| 永胜县|