最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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ó)家法律
且末县| 内乡县| 千阳县| 博白县| 沅江市| 弥勒县| 呼伦贝尔市| 新郑市| 凤庆县| 紫云| 永吉县| 蛟河市| 正定县| 桃园市| 巩留县| 黑水县| 邵阳市| 桐城市| 天门市| 玉树县| 和林格尔县| 察隅县| 卢氏县| 大方县| 巴南区| 平阴县| 米脂县| 新疆| 商城县| 依安县| 桦南县| 雷波县| 芮城县| 防城港市| 边坝县| 隆回县| 禄丰县| 田东县| 彰化县| 吉隆县| 大厂|